最近參加的一個項目要求實現遠程任務管理功能,也就是"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");
}