Solved

Use of PROCESSENTRY32 record and process status

Posted on 2004-09-23
3
931 Views
Last Modified: 2012-08-14
I'm posting this question on behalf of my colleague. He will be posting comments on this. In the end I will be the one assigning the points.

pritaeas


The question:

Hello,

I have a challenge of the problem kind. There's this unit in
the Borland-directory: Source\Rtl\Win\TlHelp32.pas. With this
unit I compile a list of PROCESSENTRY32 records. What I would
like to add is, if the entry is running or not responding.

Can anyone help me with this?

Thanks!

Bart Thomas
0
Comment
Question by:pritaeas
3 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12132139
if you write uses TlHelp32, you use the dcu from \lib folder ...

am I right ?
0
 
LVL 1

Expert Comment

by:Bart_Thomas
ID: 12133035
I'm not sure I understand your tip, maybe some more explanation helps:

TAppState = (asRunning, asNotResponding);

TProcessInfo = class (TObject)
    dwSize: DWORD;
    cntUsage: DWORD;
    th32ProcessID: DWORD;       // this process
    th32DefaultHeapID: DWORD;
    th32ModuleID: DWORD;        // associated exe
    cntThreads: DWORD;
    th32ParentProcessID: DWORD; // this process's parent process
    pcPriClassBase: Longint;    // Base priority of process's threads
    dwFlags: DWORD;
    szExeFile: array[0..MAX_PATH - 1] of Char;// Path
    AppState: TAppState;
end;


if Process32First (SnapShot, PE32) then
begin
  repeat    
     PI := PROCESSENTRY32_As_TProcessInfo (PE32);    
     
     (* The problem is: How can I detect if the current PROCESSENTRY32 is
      * "running" or "responding" ???
      *)
     PI.AppState := ???
     
     ProcessInfoList.Add (PE32);
  until not(Process32Next (SnapShot, PE32));
end;
     
Thanks!

Bart Thomas    
0
 
LVL 2

Accepted Solution

by:
php4delphi earned 125 total points
ID: 12141183
If you can enumerate processes you already have a list of running processes. Now you have to determine if process is not responding.

You can do it using following function
function IsWindowResponding(Wnd: HWND; Timeout: Integer): Boolean;
var
  Res: DWORD;
begin
  Result := SendMessageTimeout(Wnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, Timeout, Res) <> 0;
end;

To use it you have to find main window of process

function IsMainAppWindow(Wnd: HWND): Boolean;
var
  ParentWnd: HWND;
  ExStyle: DWORD;
begin
  if IsWindowVisible(Wnd) then
  begin
    ParentWnd := GetWindowLong(Wnd, GWL_HWNDPARENT);
    ExStyle := GetWindowLong(Wnd, GWL_EXSTYLE);
    Result := ((ParentWnd = 0) or (ParentWnd = GetDesktopWindow)) and
      ((ExStyle and WS_EX_TOOLWINDOW = 0) or (ExStyle and WS_EX_APPWINDOW <> 0));
  end
  else
    Result := False;
end;

function GetMainAppWndFromPid(PID: DWORD): HWND;
type
  PSearch = ^TSearch;
  TSearch = record
    PID: DWORD;
    Wnd: HWND;
  end;
var
  SearchRec: TSearch;

  function EnumWindowsProc(Wnd: HWND; Res: PSearch): Boolean; stdcall;
  var
    WindowPid: DWORD;
  begin
    WindowPid := 0;
    GetWindowThreadProcessId(Wnd, @WindowPid);
    if (WindowPid = Res^.PID) and IsMainAppWindow(Wnd) then
    begin
      Res^.Wnd := Wnd;
      Result := False;
    end
    else
      Result := True;
  end;

begin
  SearchRec.PID := PID;
  SearchRec.Wnd := 0;
  EnumWindows(@EnumWindowsProc, Integer(@SearchRec));
  Result := SearchRec.Wnd;
end;

PID you already know, so you can determine if process is responding or not
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question