我编写了一个在控制台中运行的应用程序,需要在系统关闭或用户注销之前进行快速备份。
我的测试应用程序使用信号写入一个文件,并在手动关闭控制台窗口(单击 X)时工作。但是当控制台在关机或注销时关闭时它不起作用。根据我在 MSDN 上阅读的内容,这应该可行。
程序是用cygwin64编译的,会不会是这个问题?
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
BOOL WINAPI myHandler(DWORD signal) {
switch(signal) {
case CTRL_C_EVENT:
printf("ctrl-c\n");
break;
case CTRL_BREAK_EVENT:
printf("break\n");
break;
default:
printf("Some other event\n");
}
FILE *file = fopen("windows_sig.txt", "w");
fprintf(file, "got signal: %d\n", signal);
fclose(file);
return TRUE;
}
int main(int argc, char *argv[])
{
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)myHandler,TRUE)) {
fprintf(stderr, "Unable to install handler!\n");
return EXIT_FAILURE;
}
for (;;)
; //do nothing
return EXIT_SUCCESS;
}
最佳答案
好的,因为该程序打算在后台运行,所以我将其实现为 Windows 服务。在 Windows 关闭时,该服务将收到 SERVICE_CONTROL_SHUTDOWN。 该服务可以使用 sc.exe 程序安装,甚至可以通过编程方式安装。
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
static SERVICE_STATUS sStatus;
static SERVICE_STATUS_HANDLE hServiceStatus = 0;
static int is_running = 1;
static void (*svc_main_func)();
void windows_service_control( DWORD dwControl ) {
switch (dwControl) {
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
sStatus.dwCurrentState = SERVICE_STOP_PENDING;
sStatus.dwCheckPoint = 0;
sStatus.dwWaitHint = 3000; /* Three seconds */
sStatus.dwWin32ExitCode = 0;
is_running = 0;
default:
sStatus.dwCheckPoint = 0;
}
SetServiceStatus( hServiceStatus, &sStatus );
}
void windows_service_main( int argc, char **argv ) {
hServiceStatus = RegisterServiceCtrlHandler( argv[0], (LPHANDLER_FUNCTION) windows_service_control );
if( hServiceStatus == 0 ) {
return;
}
sStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
sStatus.dwCurrentState = SERVICE_START_PENDING;
sStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
sStatus.dwWin32ExitCode = 0;
sStatus.dwServiceSpecificExitCode = 0;
sStatus.dwCheckPoint = 0;
sStatus.dwWaitHint = 3000; /* Allow us to wait three seconds */
sStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus( hServiceStatus, &sStatus );
/* The main program */
svc_main_func();
/* cleanup */
sStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus( hServiceStatus, &sStatus );
}
int windows_service_start( void (*func)() ) {
static SERVICE_TABLE_ENTRY services[] = {
{ MAIN_SRVNAME, (LPSERVICE_MAIN_FUNCTIONA) windows_service_main },
{ NULL, NULL }
};
/* Safe args for later call in windows_service_main() */
svc_main_func = func;
if( !StartServiceCtrlDispatcher( services ) ) {
printf( "Can not start service: Error %d\n", GetLastError() );
return 1;
} else {
return 0;
}
}
void run() {
while(is_running) {
//do work
}
}
int main() {
windows_service_start(run);
return 0;
}
关于windows - 关闭时不会调用 SetConsoleCtrlHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27162884/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
下面的代码在我第一次运行它时就可以正常工作:require'rubygems'require'spreadsheet'book=Spreadsheet.open'/Users/me/myruby/Mywks.xls'sheet=book.worksheet0row=sheet.row(1)putsrow[1]book.write'/Users/me/myruby/Mywks.xls'当我再次运行它时,我会收到更多消息,例如:/Library/Ruby/Gems/1.8/gems/spreadsheet-0.6.5.9/lib/spreadsheet/excel/reader.rb:11
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt