?
Solved

Use of PROCESSENTRY32 record and process status

Posted on 2004-09-23
3
Medium Priority
?
944 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 375 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

Technology Partners: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

770 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