有一个abc.exe的应用程序正在运行,要得知其还有无响应,需要用SendMessageTimeout函数来判断。
怎么得到abc.exe这个应用程序的进程句柄,SendMessageTimeout函数的参数怎么取。怎么样Kill这个应用程序的进程,又怎么样重新启动这个进程呢?

解决方案 »

  1.   

    LRESULT SendMessageTimeout(
      HWND hWnd,            // handle to window
      UINT Msg,             // message
      WPARAM wParam,        // first message parameter
      LPARAM lParam,        // second message parameter
      UINT fuFlags,         // send options
      UINT uTimeout,        // time-out duration
      PDWORD_PTR lpdwResult // return value for synchronous call
    );
    Parameters
    hWnd 
    [in] Handle to the window whose window procedure will receive the message. 
    If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows. The function does not return until each window has timed out. Therefore, the total wait time can be up to uTimeout times the number of top-level windows. Msg 
    [in] Specifies the message to be sent. 
    wParam 
    [in] Specifies additional message-specific information. 
    lParam 
    [in] Specifies additional message-specific information. 
    fuFlags 
    [in] Specifies how to send the message. This parameter can be one or more of the following values. Value Meaning 
    SMTO_ABORTIFHUNG Returns without waiting for the time-out period to elapse if the receiving process appears to be in a "hung" state. 
    SMTO_BLOCK Prevents the calling thread from processing any other requests until the function returns. 
    SMTO_NORMAL The calling thread is not prevented from processing other requests while waiting for the function to return. 
    SMTO_NOTIMEOUTIFNOTHUNG Windows 2000/XP: Does not return when the time-out period elapses if the receiving thread is not hung.  
    uTimeout 
    [in] Specifies the duration, in milliseconds, of the time-out period. If the message is a broadcast message, each window can us the full time-out period. For example, if you specify a 5 second time-out period and there are three top-level windows that fail to process the message, you could have up to a 15 second delay. 
    lpdwResult 
    [in] Receives the result of the message processing. This value depends on the message sent.
      

  2.   

    说得很详细了! 但是只说了每个参数的说明,比如我要查看“QQ.exe”这个进程还有没有响应,每个参数的取值是什么?具体发什么消息?
      

  3.   

    HOWTO: Use SendMessageTimeout() in a Multithreaded Application Q106716
    WindowProc for window Win1>
          ...
          case <xxx>:
             ...
             SendMessageTimeout(      hWnd2,   // window handle
                                  WM_USER+1,   // message to send
                                     wParam,   // first message parameter
                                     lParam,   // second message parameter
                                SMTO_NORMAL,   // flag *
                                        100,   // timeout (in milliseconds)
                                       &ret ); // return value
            ...
             break;      case WM_USER+2:
            <time-consuming procedure>
             break;
      

  4.   

    Q When I select the Applications tab in the Windows NT&reg; Task Manager, in the listview control, the Task Manager shows which applications are running and whether they are responding. What is the definition of "Not Responding," and how does the Task Manager determine whether an application is responding?
    Michael Schoettner
    A First of all, the Task Manager's Applications tab (see Figure 1) does not actually show applications—it shows the captions of visible, top-level windows (since this is how users typically decide which application they want to switch to). The Task Manager builds this list by walking through all of the top-level windows in the system (using something like EnumWindows), and adds to the list the windows that have the WS_VISIBLE style bit turned on.
    A window is not responding if the thread that created that window has not called GetMessage, PeekMessage, WaitMessage, or SendMessage within the past five seconds. The code in Figure 2 demonstrates this. First, build and run the application in Figure 2, then run the Task Manager. The Task Manager will indicate that the application is Running. Next, click the right mouse button over the message box. This causes the thread to sleep for 10 seconds, not calling the message box's GetMessage loop. If you now watch the Task Manager, it will update its display (in about five seconds) to show that the application is Not Responding. If you wait another five seconds or so, the Task Manager will again update its display to show that the application is Responding.
           Every few seconds, the Task Manager walks through its list of displayed windows. For each window, the Task Manager makes the following call:   DWORD dwResult;
     BOOL fResponding = SendMessageTimeout(hwndInQuestion,
          WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 5000, &dwResult);
     // fResponding is TRUE if the thread is responding and
     // FALSE if not.This call attempts to send a WM_NULL message (what should be a benign message) to a window. If the thread that created that window has not called GetMessage, PeekMessage, WaitMessage, or SendMessage within the past five seconds, SendMessageTimeout returns immediately because the SMTO_ABORTIFHUNG flag was specified. If the system doesn't think the thread that created the window is hung, then it tries to send the message to the window. If the thread isn't busy, it will process the message immediately and return. If the thread is busy, the Task Manager's thread waits for up to 5,000 milliseconds for the message to be processed. If the message cannot be processed, SendMessageTimeout returns FALSE, which indicates the window—more correctly, the thread—is not responding.
     
      

  5.   

    下面是来自微软的例子,这个枚举所有的窗口,然后向窗口发送关闭的消息。 
         #include <windows.h> 
         
         BOOL CALLBACK EnumWindowsProc( 
         HWND hwnd, 
         DWORD lParam 
         ); 
         
         // 
         // EnumWindowsProc must be called from a Windows 
         // application on Windows 95. 
         // 
         int WINAPI WinMain( 
         HINSTANCE hInstance, 
         HINSTANCE hPrevInstance, 
         LPSTR lpCmdLine, 
         int nCmdShow 
         ) 
         { 
         // 
         // Close all open applications. 
         // 
         EnumWindows(EnumWindowsProc, 0); 
         
         // Now do a regular logoff. 
         ExitWindowsEx(EWX_LOGOFF , 0); 
         
         } 
         
         BOOL CALLBACK EnumWindowsProc( 
         HWND hwnd, 
         DWORD lParam 
         ) 
         { 
         DWORD pid = 0; 
         LRESULT lResult; 
         HANDLE hProcess; 
         DWORD dwResult; 
         
         lResult = SendMessageTimeout( 
         hwnd, 
         WM_QUERYENDSESSION, 
         0, 
         ENDSESSION_LOGOFF, 
         SMTO_ABORTIFHUNG, 
         2000, 
         &dwResult); 
         
         if( lResult ) 
         { 
         // 
         // Application will terminate nicely, so let it. 
         // 
         lResult = SendMessageTimeout( 
         hwnd, 
         WM_ENDSESSION, 
         TRUE, 
         ENDSESSION_LOGOFF, 
         SMTO_ABORTIFHUNG, 
         2000, 
         &dwResult); 
         
         } 
         else // You have to take more forceful measures. 
         { 
         // 
         // Get the ProcessId for this window. 
         // 
         GetWindowThreadProcessId( hwnd, &pid ); 
         // 
         // Open the process with all access. 
         // 
         hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); 
         // 
         // Terminate the process. 
         // 
         TerminateProcess(hProcess, 0); 
         
         } 
         // 
         // Continue the enumeration. 
         // 
         return TRUE; 
         } 
      

  6.   

    大家请看下面是我通过文件名获得进程句柄并将之关闭的代码,现在看了前面多人说明的用SendMessageTimeout函数来侦察进程是否还有响应,我还是晕晕的,有人能针对下面的程序给出使用SendMessageTimeout函数来侦察进程的详细代码吗?HANDLE hSnapshot;
    hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    PROCESSENTRY32 pe;
    Process32First(hSnapshot,&pe);
    do
    {
    if(strcmp(pe.szExeFile,"QQ.exe")==0)
    {
    HANDLE hProcess;
    hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,pe.th32ProcessID);
    if (hProcess)
    {
    TerminateProcess(hProcess,0);//关闭进程
    }
    }

    while(Process32Next(hSnapshot,&pe));
    CloseHandle(hSnapshot);