Solved

Use of PROCESSENTRY32 record and process status

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tidtcpserver listening on multiports? 1 45
Delphi: sending SMS on android platform 1 65
Delphi Seattle StructureView color 1 21
Browsing a TTreeView in Delphi 5 32
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 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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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