I wrote an application that runs in the console and must perform a quick backup before the system shuts down or the user logs out.
My test application writes a signal file and works when the console window is closed manually (click on X). But this does not work when the console closes when shutting down or exiting the system. From what I read on MSDN, this should work.
The program was compiled using cygwin64, could this be a problem?
#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 (;;)
;
return EXIT_SUCCESS;
}
source
share