How does Windows Task Manager determine the application "no response" (add to favorites)

Source: Internet
Author: User
A recent project requires remote Task Manager (RTM ). I compared it with the Windows NT Task Manager and found that the standard task manager shows the application status (running or not responding ). The standard task manager sends a message (via the sendmessagetimeout function) to the main application window. If the function call fails or times out, the application state is "no response ", otherwise, the status is "running ".
But I found there is a better solution. This article will be demonstrated through the instance program. This method is implemented by calling an undisclosed function in user32.dll. This function exists in Windows 9x and Windows NT/2000 systems, but the names in the two systems are different. In Windows 9x, the name is ishungthread and ishungappwindow in Windows NT/2000. The following is their prototype:
Bool ishungappwindow (
Hwnd, // main application window handle
);
And
Bool ishungthread (
DWORD dwthreadid, // thread ID of the main application window
);
Unfortunately, Microsoft does not provide the output of these two functions in user32.lib. That is to say, these two functions are undisclosed. to use them in a program, you must use the getprocaddress and getmodulehandle functions to dynamically load them:

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
// Specified application
//
// Bool ishungappwindow (
// 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
// 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;
// Sscanf (argv [1], "% lx", & 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 ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.