Capturing Windows with C

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   //  control signal type
);

static int startup;

int main(int argc, char *argv[])
{
    if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
    {
        // unable to install handler...
        // display message to the user
        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) {
        /* create the filename */

     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;
}
+3
source share
3 answers

, , , . , yoru, ( ).

0

:

  • ,

    main()
    {
    ...
    SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlEventHandler, TRUE );
    ...
    }
    BOOL CtrlEventHandler(DWORD eventType)
    {
    if(eventType == CTRL_C_EVENT)
    {
    printf(""Sample text");
    return FALSE
    }
    else if(eventType == CTRL_LOGOFF_EVENT)
    {
    printf(""Sample text");
    return FALSE
    }
    }
    
  • If you are using a Windows UI-based application, then you can use the virtual WindowProc function and use the code below

    LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)    
    {
    if(message == WM_ENDSESSION)
    {
    if(lParam == ENDSESSION_LOGOFF)
    {
    /*Handle event*/
    }
    }
    return CDialogEx::WindowProc(message, wParam, lParam);
    }
    

    For option 1 you can use this MSDN link

0
source

Source: https://habr.com/ru/post/1758595/


All Articles