我自己写一个程序,当关机或在任务管理器中终止进程来杀死程序时,我想写一段程序被关闭的日志。这个代码应该写在哪一部分?finalization里我写过不行。请给出代码例子!谢谢!

解决方案 »

  1.   

    >>当关机或在任务管理器中终止进程来杀死程序时
    強制弒死進程, 并不能保證系統會執行相應的清除協作!>>当关机或在任务管理器中终止进程来
    應該會發個WM_CLOSE的消息給程度, 如果程序接收處理, 就象你按右上角關閉按鈕一樣!
    如果程序沒處理, 超時就會強制結束, 那麼, 應該什麼清除工作的代碼都不會執行的所以,小心使用!
      

  2.   

    to aiirii(ari-爱的眼睛):可否详细说明一下WM_CLOSE消息?方便的话给段代码参考一下。
      

  3.   

    he WM_CLOSE message is sent as a signal that a window or an application should terminate. WM_CLOSE  
     ParametersThis message has no parameters. Return ValuesIf an application processes this message, it should return zero. Default ActionThe DefWindowProc function calls the DestroyWindow function to destroy the window. ResAn application can prompt the user for confirmation, prior to destroying a window, by processing the WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.
      

  4.   

    SENDMESSAGE(APPLICATION.HANDLE,WM_CLOSE,0,0);TRY IT;
      

  5.   

    我在form的onDestroy事件中写日志,onDestroy是不是会WM_CLOSE消息。但是我在任务管理器中终止进程,在日志中并未记录信息。如此推论程序并为受到WM_CLOSE消息。我想知道的是在任务管理器中终止进程,则这个进程会不会收到什么消息,并且该如何处理。
      

  6.   

    关机的时候是会发一个消息给所有应用程序的,
    The WM_QUERYENDSESSION message is sent when the user chooses to end the Windows session or when an application calls the ExitWindows function. If any application returns zero, the Windows session is not ended. Windows stops sending WM_QUERYENDSESSION messages as soon as one application returns zero. After processing this message, Windows sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message. WM_QUERYENDSESSION  
    nSource = (UINT) wParam;    // source of end-session request 
    fLogOff = lParam            // logoff flag 
     ParametersnSourceReserved for future use. fLogOffValue of lParam. Indicates whether the user is logging off or shutting down the system. Supported values include: ENDSESSION_LOGOFF. Return ValuesIf an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE. ResBy default, the DefWindowProc function returns TRUE for this message. 
    Windows NT: 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: After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated. See AlsoDefWindowProc, ExitWindows, WM_ENDSESSION
      

  7.   

    头晕了。outer2000(天外流星) 能否提供个代码例子来截获WM_QUERYENDSESSION 消息并处理。希望能有个完整的解决方案。分数可以追加。
      

  8.   

    to Lwg0901(伤心人) :突然掉电那就不管了。
      

  9.   

    >>能否提供个代码例子来截获WM_QUERYENDSESSION 消息并处
    private 
      procedure WMQueryEndSession (var Msg : TWMQueryEndSession); message WM_QueryEndSession; 
    end; Implementation procedure TForm1.WMQueryEndSession (var Msg : TWMQueryEndSession); 
    begin 
      if MessageDlg('Close Windows now/ Windows beenden?', 
                                mtConfirmation, 
                                [mbYes,mbNo], 0) = mrNo then 
          Msg.Result := 0 
       else 
          Msg.Result := 1; 
    end;
      

  10.   

    taskmgr是用TerminateProcess来杀进程的,一点余地都不留。
    所以建议你做个守护进程,由它来CreateProcess以执行你的程序,并且用WaitForSingleObject函数一直监视获得的进程句柄,当WaitForSingleObject return时采取措施。
      

  11.   

    to  aiirii(ari-爱的眼睛) :你确信在任务管理器中杀死进程可以收到消息么?我试了,怎么不行?
      

  12.   

    结了这个贴了。有兴趣到http://community.csdn.net/Expert/topic/3240/3240052.xml?temp=.9827692继续。
      

  13.   

    我认为有些事件你是无法handle的,比如计算机突然断电事件。我想windows shutdown事件应该有办法获得,但杀死进程我就不能确定了。
      

  14.   

    确认windows shutdown 可以收到WM_QueryEndSession并已处理成功。看来在任务管理器中杀死进程确实收不到消息。
      

  15.   

    to fei19790920(饭桶的马甲(抵制日货)) && aiirii(ari-爱的眼睛):请到
    http://community.csdn.net/Expert/topic/3240/3240265.xml?temp=.6537744另给分。谢谢帮助!