怎麼實現一個:判斷指定進程有無響應的功能函數. (轉)

/* 怎麼實現一個:判斷指定進程有無響應的功能函數.
已知條件爲:一個進程ID,求這個進程有無響應;用VC平臺實現.
我在網絡查找一些資料,copy後得出如下一個程序,但不能檢測出結果,運行時會出錯.
 
接觸C++不是很長時間,但願你們能幫幫我,解決這個問題,謝謝.
若是還有其它方法,請給予提示.謝謝.  */
 
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
 
 
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.  
 
 
typedef  struct  tagWNDINFO  
{  
DWORD  dwProcessId;  
HWND  hWnd;  
}  WNDINFO,  *LPWNDINFO;  
 
BOOL CALLBACK YourEnumProc(HWND hWnd,LPARAM  lParam)  
{  
DWORD  dwProcessId;  
GetWindowThreadProcessId(hWnd,  &dwProcessId);  
LPWNDINFO  pInfo  =  (LPWNDINFO)lParam;  
if(dwProcessId  ==  pInfo->dwProcessId)  
{  
pInfo->hWnd  =  hWnd;  
return  FALSE;  
}  
return  TRUE;  
}  
 
HWND  GetProcessMainWnd(DWORD  dwProcessId)  
{  
WNDINFO  wi;  
wi.dwProcessId  =  dwProcessId;  
wi.hWnd  =  NULL;  
EnumWindows(YourEnumProc,(LPARAM)&wi);  
return  wi.hWnd;  
}  
//若是這個進程沒有窗口,函數返回NULL
 
/** /*/
//////////////////////////////////////////////////////////////////////////
int GetProcessAnswer(DWORD iProcessid)
{
HWND hwnd = GetProcessMainWnd(iProcessid);
//不存在/
if(NULL == hwnd)return(-1);
 
typedef BOOL (WINAPI *PROCISHUNGAPPWINDOW)(HWND);
typedef BOOL (WINAPI *PROCISHUNGTHREAD)(DWORD);
 
//而後定義
PROCISHUNGAPPWINDOW    m_pIsHungAppWindow;
PROCISHUNGTHREAD    m_pIsHungThread;
 
//定義一個bool型,來判斷當前操做系統是不是Windows NT/2000以上
// 由於不一樣的操做系統,判斷程序是否運行正常的方式是不同的
 
BOOL m_bIsNT;
BOOL bRetVal;
 
//獲取版本信息
OSVERSIONINFO osver = {0};
 
osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osver))
{
bRetVal = FALSE;
}
 
 
if(bRetVal == TRUE)
{
if (osver.dwPlatformId&VER_PLATFORM_WIN32_NT)
{
m_bIsNT = TRUE;
}
else
{
m_bIsNT = FALSE;
}
 
}
 
//獲取那兩個函數指針
HMODULE hUser32 = ::GetModuleHandle("user32");
 
if (!hUser32)
{
bRetVal = FALSE;
}
 
 
if(bRetVal == TRUE)
{
m_pIsHungAppWindow = (PROCISHUNGAPPWINDOW)
GetProcAddress( hUser32,
"IsHungAppWindow" );
 
m_pIsHungThread = (PROCISHUNGTHREAD) GetProcAddress( hUser32,
"IsHungThread" );
 
if (!m_pIsHungAppWindow && !m_pIsHungThread)
{
bRetVal = FALSE;
}
 
}
 
//因而判斷,窗口是不是正常運行,仍是未響應
// 代碼以下
 
if(m_bIsNT == TRUE)
{  
BOOL bIsHung = m_pIsHungAppWindow(hwnd);
if(bIsHung)
{
return(-2);//沒有響應
}
else
{
return(0);//正在運行    
}
}  
else
{
BOOL bIsHung =m_pIsHungThread(GetWindowThreadProcessId(hwnd,NULL));
if(bIsHung)
{
return(-2);//沒有響應
}
else
{
return(0);//正在運行    
}
}
 
 
}
 
//////////////////////////////////////////////////////////////////////////
 
 
int main()
{
 
// int iSum = int(GetProcessMainWnd(3616));//3616是進程ID
// printf("%d",isum);
int isum = GetProcessAnswer(3616/*這裏輸入進程ID*/);
printf("%d",isum);
 
return(0);
}
相關文章
相關標籤/搜索