Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Problem beim Vergleichen von ProcessID's (https://www.delphipraxis.net/33709-problem-beim-vergleichen-von-processids.html)

perle 11. Nov 2004 15:58


Problem beim Vergleichen von ProcessID's
 
Hallo Welt,

Ich wollte mir zu testzwecken (und später vielleicht zu mehr) mal eine kleine Klasse schreiben , die verschiedene funktionen beinhaltet, um an ein Fensterhandle zu kommen. In einem Beispiel versuche ich über verschiedene Wege über den exe namen des Programms an das Handle zu kommen. allerdings funktioniert das nicht wie gewünscht .

Code:
function TSearchWindow.GetWindowByAppName(ApplicationName: String): Cardinal;

  function GetAppProcID(ApplicationName : String):Cardinal;
  var
    bContinue: BOOL;
    aSnapshotHandle: THandle;
    aProcessEntry32: TProcessEntry32;
  begin
    result := 0;
    aSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
    bContinue := Process32First(aSnapshotHandle, aProcessEntry32);
    while Integer(bContinue) <> 0 do
    begin
      if LowerCase(aProcessEntry32.szExeFile) = LowerCase(ApplicationName) then
        result := OpenProcess(1, BOOL(0), aProcessEntry32.th32ProcessID);
      bContinue := Process32Next(aSnapshotHandle, aProcessEntry32);
    end;
    CloseHandle(aSnapshotHandle);
  end;

  function GetHandleFromProcID(aProcID : Cardinal) : Cardinal;

     function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
     begin
       TList(lParam).Add(Pointer(hWnd));
       Result := True;
     end;

  var
    List: TList;
    ProcessId: DWORD;
    I: Integer;
  begin
    Result := 0;
    List := TList.Create;
    try
      if EnumWindows(@EnumWindowsProc, Longint(List)) then
      begin
        for I := 0 to List.Count - 1 do
        begin
          GetWindowThreadProcessId(Longint(List.Items[I]), ProcessId);
          if aProcID = ProcessId then   // <-- hier werden die ausgelesene ProcessID mit allen vorhandenen ProcessID's
                                                verglichen jedoch sind diese niemals gleich
          begin
            Result := Longint(List.Items[I]);
            Break;
          end;
        end;
      end;
    finally
      List.Free;
    end;
  end;

begin
  result := GetHandleFromProcID(GetAppProcID(ApplicationName));
end;
wie oben markiert, sind die zu vergleichenden ProcessID's nie gleich, aber ich weiss nicht warum.
Außerdem hab ich festgestellt, dass wenn ich mehrmals auf einen button drücke (um die P-ID anzeigen zu lassen) , diese jedesmal anders ist (kleiner wird).

Hat jemand eine Lösung für dieses Problem oder einen anderen/besseren Lösungsweg?

Danke im Vorraus

Yvonne.

Sprint 11. Nov 2004 21:28

Re: Problem beim Vergleichen von ProcessID's
 
Mit deiner Funktion GetAppProcID willst du doch die ProcessId ermitteln.
Delphi-Quellcode:
result := OpenProcess(1, BOOL(0), aProcessEntry32.th32ProcessID);
So wird das nix. Result := aProcessEntry32.th32ProcessID wäre wohl besser.


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:16 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz