Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Pfad zur Exe von hProcess (https://www.delphipraxis.net/83473-pfad-zur-exe-von-hprocess.html)

Neutral General 3. Jan 2007 12:26


Pfad zur Exe von hProcess
 
Hi,

Also den Dateinamen eines Prozesses habe ich jetzt herausgefunden... Aber ich bekomme halt nur sowas wie delphi32.exe... Ich bräuchte aber den ganzen Pfad.. :|
Kann ich den irgendwie bekommen?

Gruß
Neutral General

brechi 3. Jan 2007 12:34

Re: Pfad zur Exe von hProcess
 
das geht mit Module32First und szExeName (oder so)
funktioniert laut NicoDE aber nicht auf allen VMs

Oder meinst du nun wirklich einen Prozesshandle (hProcess) und nicht die ProzessId? Wie hast du denn vom Handle her den Dateinamen erhalten?

ErazerZ 3. Jan 2007 12:35

Re: Pfad zur Exe von hProcess
 
mittels TModuleEntry32 kannst du den Pfad auslesen (erstes Modul ist immer die *.exe Datei)
Delphi-Quellcode:
uses TlHelp32;

const
  ProcessId = 1684;
var
  ModuleEntry: TModuleEntry32;
  hModuleEntry: THandle;
  sPfad: String;
begin
  hModuleEntry := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);
  ModuleEntry.dwSize := SizeOf(TModuleEntry32);
  Module32First(hModuleEntry, ModuleEntry);
  sPfad := ModuleEntry.szExePath;
  CloseHandle(hModuleEntry);

Neutral General 3. Jan 2007 12:44

Re: Pfad zur Exe von hProcess
 
Habe diese Funktion aus einer anderen aus der DP abgeleitet:

Delphi-Quellcode:
function GetProcessExename(hProcess: THandle): String;
var hProcSnap: THandle;
    pe32: TProcessEntry32;
begin
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap <> INVALID_HANDLE_VALUE then
  begin
    pe32.dwSize := SizeOf(TProcessEntry32);
    if Process32First(hProcSnap, pe32) = true then
    begin
      while Process32Next(hProcSnap, pe32) = true do
      begin
        if pe32.th32ProcessID = hProcess then
        begin
         Result := pe32.szExeFile;
         exit;
        end;
      end;
    end;
    CloseHandle(hProcSnap);
  end;
end;
Habe jetzt mal TModuleEntry32 genommen statt TProcessEntry32... Aber da bekomme ich nix zurück...

Die ID bekomm ich so:

Delphi-Quellcode:
GetWindowThreadProcessId(wnd,hProcess);
Aber wie gesagt... Es funktioniert noch nicht so wies soll :mrgreen:

Gruß
Neutral General

ErazerZ 3. Jan 2007 12:53

Re: Pfad zur Exe von hProcess
 
wie wäre es damit?
Delphi-Quellcode:
function GetProcessExeName(hProcess: THandle): String;
var
  ModuleEntry: TModuleEntry32;
  hModuleEntry: THandle;
  ProcessId: Cardinal;
begin
  if hProcess = INVALID_HANDLE_VALUE then Exit;
  ProcessId := GetWindowThreadProcessId(hProcess);
  hModuleEntry := CreateToolHelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);
  ModuleEntry.dwSize := SizeOf(TModuleEntry32);
  Module32First(hModuleEntry, ModuleEntry);
  Result := MOduleEntry.szExePath;
  CloseHandle(hModuleEntry);
end;
Edit: ah ne, diese funktion ließt nur aus dem eigenen Prozess aus

ErazerZ 3. Jan 2007 13:52

Re: Pfad zur Exe von hProcess
 
Delphi-Quellcode:
uses PsApi;

function GetProcessExeName(hProcess: THandle): String;
var
  lpBuffer: array[0..MAX_PATH -1] of Char;
begin
  ZeroMemory(@lpBuffer, SizeOf(lpBuffer));
  GetModuleFileNameEx(hProcess, 0, lpBuffer, SizeOf(lpBuffer));
  Result := String(lpBuffer);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  lpStartupInfo: TStartupInfo;
  lpProcessInformation: TProcessInformation;
begin
  ZeroMemory(@lpStartupInfo, sizeof(TStartupInfo));
  lpStartupInfo.cb := sizeof(TStartupInfo);
  lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  lpStartupInfo.wShowWindow := SW_SHOW;
  ZeroMemory(@lpProcessInformation, sizeof(TProcessInformation));
  CreateProcess(nil, 'notepad.exe', nil, nil, False, CREATE_NEW_CONSOLE, nil, nil, lpStartupInfo, lpProcessInformation);
  Application.MessageBox(PAnsiChar(GetProcessExeName(lpprocessinformation.hProcess)), 'Path', MB_ICONINFORMATION);
  TerminateProcess(lpProcessInformation.hProcess, 0);
  Application.Terminate;
end;
So, das funktioniert bei mir, aber halt nur mit PsApi und GetModuleFileNameEx (d.h. Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation 4.0.)


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:17 Uhr.

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