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!!
MarcelitoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
d003303Connect With a Mentor Commented:
OK, you have to accept this answer and grade the question. Have a look at the Expert grading help link below.

Good luck on all your projects,
Slash/d003303
0
 
MikeP090797Commented:
You can use EnumWindows
0
 
d003303Commented:
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) ?
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
MarcelitoAuthor Commented:
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!!
0
 
MikeP090797Commented:
Can you be more specific about what is the porblem with your solution
0
 
MarcelitoAuthor Commented:
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!!
0
 
MikeP090797Commented:
Post your code and we'll see why it fails

0
 
MarcelitoAuthor Commented:
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.
0
 
d003303Commented:
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
0
 
MarcelitoAuthor Commented:
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.
0
 
d003303Commented:
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.
0
 
MarcelitoAuthor Commented:
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  
0
 
MarcelitoAuthor Commented:
Thanks!! Good luck for you!!
0
All Courses

From novice to tech pro — start learning today.