Windows 工作管理員如何確定應用程式”沒有響應”(收藏)

來源:互聯網
上載者:User
最近參加的一個項目要求實現遠程任務管理功能,也就是"Remote Task Manager"(RTM)。我把它與Windows NT的工作管理員進行了比較,發現標準的工作管理員顯示應用程式的狀態(正在運行或者沒有響應)。標準的工作管理員發送(通過SendMessageTimeout函數)一個訊息到主應用視窗,如果函數調用失敗或者逾時--則應用程式的狀態就是"沒有響應",否則狀態為"正在運行"。
   但我發現還有一個更好的解決方案。本文將通過執行個體程式進行示範。這個方法的思路是通過調用User32.dll中一個未公開的函數來實現的。這個函數存在於Windows 9x和Windows NT/2000系統中,但在兩個系統中的名字是不同的。Windows 9x系統中的名字為:IsHungThread,在Windows NT/2000系統中的名字為IsHungAppWindow。下面是它們的原型:
BOOL IsHungAppWindow (
HWND hWnd, // 主應用視窗控制代碼
);

BOOL IsHungThread (
DWORD dwThreadId, // 主應用視窗的線程ID
);
   不幸的是,微軟在User32.lib中沒有提供這兩個函數的輸出。也就是說,這兩個函數是未公開函數,如果要在程式中使用它們,則必須通過GetProcAddress和GetModuleHandle函數動態載入:

typedef BOOL (WINAPI *PROCISHUNGAPPWINDOW) (HWND);
typedef BOOL (WINAPI *PROCISHUNGTHREAD) (DWORD);

PROCISHUNGAPPWINDOW IsHungAppWindow;
PROCISHUNGTHREAD IsHungThread;

HMODULE hUser32 = GetModuleHandle("user32");

IsHungAppWindow = (PROCISHUNGAPPWINDOW)
GetProcAddress(hUser32,"IsHungAppWindow");

IsHungThread = (PROCISHUNGTHREAD)
GetProcAddress(hUser32,"IsHungThread");

//////////////////

// ishung.cpp (Windows 95/98/NT/2000)
//
// This example will show you how you can obtain the current status
// of the application.
// 
//
// (c)1999 Ashot Oganesyan K, SmartLine, Inc
// mailto:ashot@aha.ru, http://www.protect-me.com, http://www.codepile.com

#include <windows.h>
#include <stdio.h>

// User32!IsHungAppWindow (NT specific!)
//
// The function retrieves the status (running or not responding) of the
// specified application
//
// BOOL IsHungAppWindow(
//   HWND hWnd,        // handle to main app's window
// );
typedef BOOL    (WINAPI *PROCISHUNGAPPWINDOW)(HWND);

// User32!IsHungThread (95/98 specific!)
//
// The function retrieves the status (running or not responding) of the
// specified thread
//
// BOOL IsHungThread(
//   DWORD dwThreadId, // The identifier of the main app's window thread 
// );
typedef BOOL    (WINAPI *PROCISHUNGTHREAD)(DWORD);

PROCISHUNGAPPWINDOW            IsHungAppWindow;
PROCISHUNGTHREAD            IsHungThread;

void main(int argc, char* argv[])
{
/*    if (argc<2)
    {
        printf("Usage:/n/nishung.exe hWnd/n");
        return;
    }
*/
//    HWND hWnd;
//    sscanf(argv[1],"%lx",&hWnd);

    HWND hWnd = ::FindWindow(NULL, "CLENT"); 
    if (hWnd == NULL) 
    {
        printf("Incorrect window handle(handle is NULL)/n");
        return;

    }
    
    if (!IsWindow(hWnd))
    {
        printf("Incorrect window handle/n");
        return;
    }

    HMODULE hUser32 = GetModuleHandle("user32");
    if (!hUser32)
        return;

    IsHungAppWindow = (PROCISHUNGAPPWINDOW)
                                         GetProcAddress( hUser32,
                                                         "IsHungAppWindow" );

    IsHungThread = (PROCISHUNGTHREAD) GetProcAddress( hUser32,
                                                          "IsHungThread" );

    if (!IsHungAppWindow && !IsHungThread)
        return;

    OSVERSIONINFO osver;
    osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    if (!GetVersionEx(&osver))
        return;

    BOOL IsHung;

    if (osver.dwPlatformId&VER_PLATFORM_WIN32_NT)
        IsHung = IsHungAppWindow(hWnd);
    else
        IsHung = IsHungThread(GetWindowThreadProcessId(hWnd,NULL));

    if (IsHung)
        printf("Not Responding/n");
    else
        printf("Running/n");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.