Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Detect If an Application Has Stopped Responding Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
21-Jul-03
Category
System
Language
Delphi 2.x
Views
183
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ido Kanner 

In many situations you might like to detect if an application is blocked. For 
example while automating Word, you'd like to know if Word has stopped responding. 
This article describes how to detect if an application has stopped responding using 
some undocumented functions.

Answer:

1   {
2     // (c)1999 Ashot Oganesyan K, SmartLine, Inc
3     // mailto:ashot@aha.ru, http://www.protect-me.com, http://www.codepile.com
4   
5   The code doesn't use the Win32 API SendMessageTimout function to
6   determine if the target application is responding but calls
7   undocumented functions from the User32.dll.
8   
9   --> For Windows 95/98/ME we call the IsHungThread() API
10  
11  The function IsHungAppWindow retrieves the status (running or not responding)
12  of the specified application
13  
14  IsHungAppWindow(Wnd: HWND): // handle to main app's window
15  BOOL;
16  
17  --> For NT/2000/XP the IsHungAppWindow() API:
18  
19  The function IsHungThread retrieves the status (running or not responding) of
20  the specified thread
21  
22  IsHungThread(DWORD dwThreadId): // The thread's identifier of the main app's window
23  BOOL;
24  
25    Unfortunately, Microsoft doesn't provide us with the exports symbols in the
26    User32.lib for these functions, so we should load them dynamically using the 
27  GetModuleHandle and
28    GetProcAddress functions:
29  }
30  
31  // For Win9x/ME
32  
33  function IsAppRespondig9x(dwThreadId: DWORD): Boolean;
34  type
35    TIsHungThread = function(dwThreadId: DWORD): BOOL; stdcall;
36  var
37    hUser32: THandle;
38    IsHungThread: TIsHungThread;
39  begin
40    Result := True;
41    hUser32 := GetModuleHandle('user32.dll');
42    if (hUser32 > 0) then
43    begin
44      @IsHungThread := GetProcAddress(hUser32, 'IsHungThread');
45      if Assigned(IsHungThread) then
46      begin
47        Result := not IsHungThread(dwThreadId);
48      end;
49    end;
50  end;
51  
52  // For Win NT/2000/XP
53  
54  function IsAppRespondigNT(wnd: HWND): Boolean;
55  type
56    TIsHungAppWindow = function(wnd: hWnd): BOOL; stdcall;
57  var
58    hUser32: THandle;
59    IsHungAppWindow: TIsHungAppWindow;
60  begin
61    Result := True;
62    hUser32 := GetModuleHandle('user32.dll');
63    if (hKernel > 0) then
64    begin
65      @IsHungAppWindow := GetProcAddress(hUser32, 'IsHungAppWindow');
66      if Assigned(IsHungAppWindow) then
67      begin
68        Result := not IsHungAppWindow(wnd);
69      end;
70    end;
71  end;
72  
73  function IsAppRespondig(Wnd: HWND): Boolean;
74  begin
75    if not IsWindow(Wnd) then
76    begin
77      ShowMessage('Incorrect window handle');
78      Exit;
79    end;
80    if Win32Platform = VER_PLATFORM_WIN32_NT then
81      Result := IsAppRespondigNT(wnd)
82    else
83      Result := IsAppRespondig9X(GetWindowThreadProcessId(wnd, nil));
84  end;
85  
86  // Example: Check if Word is hung/responing
87  
88  procedure TForm1.Button3Click(Sender: TObject);
89  var
90    Res: DWORD;
91    h: HWND;
92  begin
93    // Find Word by classname
94    h := FindWindow(PChar('OpusApp'), nil);
95    if h <> 0 then
96    begin
97      if IsAppRespondig(h) then
98        ShowMessage('Word is responding')
99      else
100       ShowMessage('Word is not responding');
101   end
102   else
103     ShowMessage('Word is not open');
104 end;



			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC