Windows 任务管理器如何确定应用程序"没有响应"(收藏)
2004-12-29 16:18
471 查看
最近参加的一个项目要求实现远程任务管理功能,也就是"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");
}
但我发现还有一个更好的解决方法。本文将通过实例程序进行示范。这个方法的思路是通过调用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");
}
相关文章推荐
- Windows 任务管理器如何确定应用程序"没有响应"
- Windows 任务管理器如何确定应用程序"没有响应"
- Windows中的任务管理器是如何决定应用程序“没有响应”的?
- "没有找到MFC80UD.DLL,因此这个程序未能启动.重新安装应用程序可能会修复此问题"?
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- windows 7 如何关闭 程序崩溃时弹出的"windows正在检查该问题的解决方案"
- 如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 如何实现"设为首页","加入收藏"的功能
- "没有OledbDataReader.Close()又能如何?"……?!
- "在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误"的解决方法
- 实战:IIS错误提示:"服务没有及时响应启动或控制请求"
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- [SQL Server] 如何查询windows认证的用户连接数据库时的"权限/对应的登陆账号"(该账号是一个windows组)
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 出现"会话状态已创建一个会话ID,但由于响应已被应用程序刷新而无法保存它" 解决方案
- "MtkWriteTool.exe Flash tool.exe应用程序正常初始化(0xc0150002)失败。请单击“确定”,终止应用程序。"解决方案
- 如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】