Windows注销C [英] Windows Logoff Capture with C

查看:198
本文介绍了Windows注销C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C在Windows Server 2008 64位系统上捕获注销事件。目前我使用的控制台注销事件代码如下所示: http://www.cplusplus.com/论坛/初学者/ 1501 / (Gray Wolf的第6个评论,他的第二个编码示例)将附在这篇文章的末尾。



此代码有几个问题。它独立工作,用户启动程序。当我开始添加一些我们专有代码的电话时,它会停止捕捉注销事件。没有GUI代码,这是所有的控制台。任何人都知道有什么可以阻止这个工作?似乎相当精彩。



此外,如果程序在登录期间自动启动(在用户级别启动并且具有相同的会话ID,就像双击.exe并激活它一样)自己)也无法捕获注销事件。任何想法都会很棒。



示例:
这可以很好的独立工作,如果用户手动启动。

  #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(无法安装处理程序!\\\
);
return -1;
}

startup = 1;

while(1)
{

}
}

BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
char mesg [128];
static FILE * pFile;

char FileName [32] =ControlHandle.txt;
如果(启动){
/ *创建文件名* /

pFile = fopen(FileName,at);
printf(创建文件\);

fprintf(pFile,此文件包含接收到控件字符时的消息。\\\
\\\
\\\
);
fclose(pFile);
startup = 0;
}

switch(CEvent)
{
case CTRL_C_EVENT:
pFile = fopen(FileName,at);
if(pFile> 0){
printf(Got File Handle);
}
fprintf(pFile,在处理程序中有一个CTRL_C_EVENTevent\\\
);
fclose(pFile);
break;
case CTRL_BREAK_EVENT:
pFile = fopen(FileName,at);
fprintf(pFile,在处理程序中有一个CTRL_BREAK_EVENTevent\\\
);
fclose(pFile);
break;
case CTRL_CLOSE_EVENT:
pFile = fopen(FileName,at);
fprintf(pFile,in handler get a CTRL_CLOSE_EVENTevent\\\
);
fclose(pFile);
break;
case CTRL_LOGOFF_EVENT:
pFile = fopen(FileName,at);
fprintf(pFile,在处理程序中有一个CTRL_LOGOFF_EVENTevent\\\
);
fclose(pFile);
break;
case CTRL_SHUTDOWN_EVENT:
pFile = fopen(FileName,at);
fprintf(pFile,in handler get a CTRL_SHUTDOWN_EVENTevent\\\
);
fclose(pFile);
break;

}
返回TRUE;
}


解决方案

有两种方法




  • 如果您使用控制台应用程序,则可以使用

      main()
    {
    ...
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlEventHandler,TRUE);
    ...
    }
    BOOL CtrlEventHandler(DWORD eventType)
    {
    if(eventType == CTRL_C_EVENT)
    {
    printf( 样本文本);
    返回FALSE
    }
    else if(eventType == CTRL_LOGOFF_EVENT)
    {
    printf(Sample text);
    返回FALSE
    }
    }


  • 正在使用基于Windows UI的应用程序,那么您可以使用虚拟函数WindowProc并使用以下代码

      LRESULT CMainDlg :: WindowProc(UINT消息,WPARAM wParam,LPARAM lParam)
    {
    if(message == WM_ENDSESSION)
    {
    if(lParam == ENDSESSION_LOGOFF)
    {
    / *处理事件* /
    }
    }
    返回CDialogEx :: WindowProc(message,wParam,lParam);
    }

    对于第一个选项,您可以使用此MSDN链接



I am attempting to use C to capture a logoff event on a Windows Server 2008 64 bit system. Currently I am using the console logoff event code illustrated here: http://www.cplusplus.com/forum/beginner/1501/ (6th comment down by Grey Wolf, his second coding example) will be attached to the end of this post.

There are several issues with this code. It works fine standalone and when the user starts the program. When I begin to add calls to some of our proprietary code it stops catching logoff events. No GUI code, this is all console. Anyone know anything that would stop this from working? It seems rather finicky.

Also if the program is started by another program automatically during logon (started at the user level and same session ID as if you were to double click the .exe and activate it yourself) it also fails to capture the logoff event. Any ideas would be great.

Examples: This works fine standalone and if manually started 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;
}

解决方案

There are two ways

  • If you are using console application, then you can use

    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 Windows UI based application,then you can use virtual function WindowProc and use below code

    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 1st option, you can use This MSDN link

这篇关于Windows注销C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆