Solved

Use of PROCESSENTRY32 record and process status

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

22 Experts available now in Live!

Get 1:1 Help Now