RT,
vc6.0写了一个Sniffer,想在程序结束时记录下拦截的总的数据包大小。我原打算程序在收到destory消息后记录数据包大小,可是我发现在windows退出时我的程序没有执行destory对应的操作。
要怎样实现这个功能呢?
最好不用hook。如果要用hook,有没有相关的源码?

解决方案 »

  1.   

    System Shutdown Messages
    The following messages are used with system shutdown. WM_ENDSESSION
    WM_QUERYENDSESSION 
    WM_QUERYENDSESSION
    The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message. A window receives this message through its WindowProc function. LRESULT CALLBACK WindowProc(
      HWND hwnd,       // handle to window
      UINT uMsg,       // WM_QUERYENDSESSION
      WPARAM wParam,   // not used
      LPARAM lParam    // logoff option
    );
    Return Values
    If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. Windows NT/2000: When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Windows 95/98: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated. 
      

  2.   

    BOOL CYourApp::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
    {
    AfxMessageBox("要关机了,请作好准备");
    return CWinApp::PreTranslateMessage(pMsg);
    }
      

  3.   

    CYourAPP::ExitInstance()
    {
        //Do what you want to
    }
      

  4.   

    最好拦截一下关机消息WM_QUERYENDSESSION WM_ENDSESSION
      

  5.   

    to signoft(晴天):
    我照你给的例子写了下面的程序:
    BOOL CSnifferApp::PreTranslateMessage(MSG* pMsg)
    {
    if(pMsg->message==WM_QUERYENDSESSION||pMsg->message==WM_ENDSESSION)
    {
    CString str;
    str="rrrr";
    using namespace std;
    ofstream outFile("exit.txt");
    outFile <<str;
    outFile.close();
    }
    return CWinApp::PreTranslateMessage(pMsg);
    }
    不过好像windows退出时没有执行文件写入的操作就直接关闭了。用AfxMessageBox居然根本没弹出消息框。难道有什么地方还有问题吗?
      

  6.   

    to zkxz:
    用ExitInstance()也不行呀,正常的退出可以记录不过关机就不行了