Link to home
Start Free TrialLog in
Avatar of Marcelito
Marcelito

asked on

Apps running...

I´m developing an application will execute in Win95 and WindowsNT. I need some function (or component) to identify all programs running in the computer. This function should be able in both platforms. Thanks!!
PD: My english is not good, sorry!!
Avatar of MikeP090797
MikeP090797

You can use EnumWindows
EnumWindows will return all windowed applications. Do you want to identify all running programs or all running processes (like the applications/processes page on NT task manager) ?
Avatar of Marcelito

ASKER

Ok! I need identify all programs running, but if I can identify all processes also is better. I have a primary solution, but not work properly in some cases. Thanks!!
Can you be more specific about what is the porblem with your solution
When I execute the program in IDE, it´s work (sometimes I have an Access Violation error when close the main form). When I execute the .exe file, the program perform an invalid operation, and Windows close the task. I´m confused!!

PD: I´m not using components, only API functions. THANKS!!
Post your code and we'll see why it fails

Here is the code. Good Luck MikeP!!

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

var hWnd, hWndAux:integer;
    spBuffer:pChar;
    sWndTitle, sAux:string;
    encontre: boolean;
    nTitleSize,comp: integer;

begin
    nTitleSize:=0;
    hWnd:= 0;
    encontre:= false;
    spBuffer:='';
    hWnd:=GetTopWindow(0);
    nTitleSize:= GetWindowTextLength(hWnd);
    GetWindowText(hWnd,spBuffer,nTitleSize);
    sWndTitle:= StrPas(spBuffer);
    ShowMessage(sWndTitle);
    while hWnd <> 0 do
      begin
         hWndAux:=hWnd;
         hWnd:= GetNextWindow(hWndAux,GW_HWNDNEXT);
         if IsWindow(hWnd) then
           begin
             nTitleSize:= GetWindowTextLength(hWnd);
             GetWindowText(hWnd,spBuffer,nTitleSize+1);
             sWndTitle:= StrPas(spBuffer);
             sWndTitle:=Trim(sWndTitle);
             ShowMessage(sWndTitle);
           end;

      end;
end;

end.
you have to init/uninit the spBuffer with GetMem and FreeMem before each call to GetWindowText. PChar is only a pointer to a character chain. You always have to allocate the memory before and free memory after you used it.

hWnd:=GetTopWindow(0);
nTitleSize:= GetWindowTextLength(hWnd);
// here you allocate the memory
GetMem(spBuffer, nTitleSize + 1);
// there was the + 1 missing
GetWindowText(hWnd,spBuffer,nTitleSize + 1);
sWndTitle:= StrPas(spBuffer);
// here you free it
FreeMem(spBuffer, nTitleSize + 1);
ShowMessage(sWndTitle);
while hWnd <> 0 do
begin
hWndAux:=hWnd;
hWnd:= GetNextWindow(hWndAux,GW_HWNDNEXT);
if IsWindow(hWnd) then
begin
nTitleSize:= GetWindowTextLength(hWnd);
// here you allocate the memory
GetMem(spBuffer, nTitleSize + 1);
GetWindowText(hWnd,spBuffer,nTitleSize+1);
sWndTitle:= StrPas(spBuffer);
// here you free it
FreeMem(spBuffer, nTitleSize + 1);
sWndTitle:=Trim(sWndTitle);
ShowMessage(sWndTitle);
end;

Slash/d003303
Well, I think that I have a cloud in my mind. How I can forget it? Thanks, d003303!! My solution is working now!! Thanks to MikeP also! See you.
Your question has to be graded if you got a working answer. Please let us know if the main part of the question was solved by MikeP or me, that he / I can give an answer to put the question to the PAQ area.
Sorry. Really the main part of question was resolved by you, d003303. I was not know the way to grade the answer. Please, tell me and I will do! Thanks  
ASKER CERTIFIED SOLUTION
Avatar of d003303
d003303

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!! Good luck for you!!