Board index » delphi » Window Name

Window Name

Hi,

I use this code to get a WindowName, but I get the wrong names back. Can anybody tell me what is
wrong.
                                Source Code
**********************************************************************************************************
var
  WindowName : string;
  FindProcessID : Cardinal;

function EnumWindowsProc(hwnd : HWND; lParam : LParam) : boolean; stdcall;
var
  ProcessID : cardinal;
  WName : PChar;
begin
  GetWindowThreadProcessID(hwnd,@ProcessID);
  if ProcessID = FindProcessID then begin
    GetMem(WName,GetWindowTextLength(hwnd)+1);
    GetWindowText(hwnd,WName,GetWindowTextLength(hwnd));
    WindowName := String(WName);
    FreeMem(WName);
    result := false;
  end else
    result := true;
end;

function GetWindowName(ProcessID : cardinal) : string;
begin
  FindProcessID := ProcessID;
  EnumWindows(@EnumWindowsProc,0);
  result := WindowName;
end;

procedure TfrmSelectProcess.GetProcess;
var
  hSnap : hwnd;
  Found : Boolean;
  pe : ProcessEntry32;
begin
  hSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
  if hSnap = NULL then begin
    ShowMessage('Error creating snapshot.');
    Exit;
  end;
  Found := Process32First(hSnap,pe);
  while found do begin
    StringGrid1.RowCount := StringGrid1.RowCount + 1;
    StringGrid1.Cells[0,StringGrid1.RowCount-2] := GetWindowName(pe.th32ProcessID);
    StringGrid1.Cells[1,StringGrid1.RowCount-2] := IntToHex(pe.th32ProcessID,6);
    Found := Process32Next(hSnap,pe);
  end;
  StringGrid1.RowCount := StringGrid1.RowCount - 1;
  CloseHandle(hSnap);
end;
**********************************************************************************************************

Thanks in advance.

--
Regards,
Richard

 

Re:Window Name


Hi,

Im not sure but I think there is a problem with the string and pchar values
because both are pointers to some memory block. You only cast the pchar
value to a string value but you dont copy the underlying memory block. And
as you free the pchar value after its use the string is not defined. See
below.

Quote
Richard <robinh...@hotpop.com> wrote:
> Hi,
> I use this code to get a WindowName, but I get the wrong names back. Can anybody tell me what is
> wrong.
>                            Source Code
> **********************************************************************************************************
> var
>   WindowName : string;
>   FindProcessID : Cardinal;
> function EnumWindowsProc(hwnd : HWND; lParam : LParam) : boolean; stdcall;
> var
>   ProcessID : cardinal;
>   WName : PChar;
> begin
>   GetWindowThreadProcessID(hwnd,@ProcessID);
>   if ProcessID = FindProcessID then begin
>     GetMem(WName,GetWindowTextLength(hwnd)+1);
>     GetWindowText(hwnd,WName,GetWindowTextLength(hwnd));

  At this point I would use either the StrPas function which copies the
memory block to WindowName or the SetString function where you can allocate
a new string with a given length and afterwards copy a given string or pchar
into the new allocated memory. It should work.

- Show quoted text -

Quote
>     WindowName := String(WName);
>     FreeMem(WName);
>     result := false;
>   end else
>     result := true;
> end;
> function GetWindowName(ProcessID : cardinal) : string;
> begin
>   FindProcessID := ProcessID;
>   EnumWindows(@EnumWindowsProc,0);
>   result := WindowName;
> end;
> procedure TfrmSelectProcess.GetProcess;
> var
>   hSnap : hwnd;
>   Found : Boolean;
>   pe : ProcessEntry32;
> begin
>   hSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
>   if hSnap = NULL then begin
>     ShowMessage('Error creating snapshot.');
>     Exit;
>   end;
>   Found := Process32First(hSnap,pe);
>   while found do begin
>     StringGrid1.RowCount := StringGrid1.RowCount + 1;
>     StringGrid1.Cells[0,StringGrid1.RowCount-2] := GetWindowName(pe.th32ProcessID);
>     StringGrid1.Cells[1,StringGrid1.RowCount-2] := IntToHex(pe.th32ProcessID,6);
>     Found := Process32Next(hSnap,pe);
>   end;
>   StringGrid1.RowCount := StringGrid1.RowCount - 1;
>   CloseHandle(hSnap);
> end;
> **********************************************************************************************************
> Thanks in advance.
> --
> Regards,
> Richard

Hope it helps.

cu,
Toralf

Other Threads