I am trying to use C to capture the logout event on a Windows Server 2008 64-bit system. I am currently using the logout event code here: http://www.cplusplus.com/forum/beginner/1501/ (added the sixth commentary made by Gray Wolf, the second example of its coding) until the end of this post.
There are several issues with this code. It works perfectly autonomously when the user starts the program. When I start adding calls to some of our proprietary code, it stops capturing logout events. There is no graphical interface, this is all a console. Does anyone know anything that will prevent this from working? It seems pretty subtle.
Also, if a program is launched by another program automatically during login (it starts at the user level and in the same session identifier as if you double-click on .exe and activate it yourself), he also could not capture the logout event. Any ideas would be great.
Examples: This works fine autonomously and when started manually by the user.
#include <windows.h>
#include <tchar.h>
#include <signal.h>
BOOL WINAPI ConsoleHandler(
DWORD dwCtrlType
);
static int startup;
int main(int argc, char *argv[])
{
if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
{
printf("Unable to install handler!\n");
return -1;
}
startup=1;
while(1)
{
}
}
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
char mesg[128];
static FILE * pFile;
char FileName[32] = "ControlHandle.txt";
if(startup) {
pFile = fopen(FileName, "at");
printf("creating a file\n");
fprintf(pFile, "This file contains the message when a control character is received.\n\n\n");
fclose(pFile);
startup=0;
}
switch(CEvent)
{
case CTRL_C_EVENT:
pFile = fopen(FileName, "at");
if(pFile > 0){
printf("Got File Handle");
}
fprintf( pFile,"in handler got an CTRL_C_EVENTevent\n" );
fclose(pFile);
break;
case CTRL_BREAK_EVENT:
pFile = fopen(FileName, "at");
fprintf( pFile,"in handler got an CTRL_BREAK_EVENTevent\n" );
fclose(pFile);
break;
case CTRL_CLOSE_EVENT:
pFile = fopen(FileName, "at");
fprintf( pFile,"in handler got an CTRL_CLOSE_EVENTevent\n" );
fclose(pFile);
break;
case CTRL_LOGOFF_EVENT:
pFile = fopen(FileName, "at");
fprintf( pFile,"in handler got an CTRL_LOGOFF_EVENTevent\n" );
fclose(pFile);
break;
case CTRL_SHUTDOWN_EVENT:
pFile = fopen(FileName, "at");
fprintf( pFile,"in handler got an CTRL_SHUTDOWN_EVENTevent\n" );
fclose(pFile);
break;
}
return TRUE;
}
source
share