Solved

Use of PROCESSENTRY32 record and process status

Posted on 2004-09-23
3
926 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 118
Strange behavior when a form is closed 6 50
Reconfigure Delphi Install? 2 45
tvirtualstringtree freeze when load too manny images 10 52
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now