Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#19

AW: In welchem Pfad läuft ein Prozess?

  Alt 20. Okt 2018, 21:21
Und nochmal, Wenn man in Michaels Code
Michaels Code ist die KillProcess() Funktion.
external kernel32 name 'QueryFullProcessImageNameA';
Da ich nun weiß worum es Dir mehr oder weniger geht und ich die ANSI Version gelesen habe, war ich so frei es noch mal zu überarbeiten, etwas gezielter für Deine Bedürfnisse:

Delphi-Quellcode:
function PidToFilename(const TargetPID: THandle): string;
type
  TQueryFullProcessImageName = function(hProcess: THandle; dwFlags: DWORD; lpExeName: PChar; nSize: PDWORD): BOOL; stdcall;
var
  hProcess: THandle;
  TargetName: array [0 .. MAX_PATH - 1] of Char;
  QueryFullProcessImageName: TQueryFullProcessImageName;
  nSize: cardinal;
begin
  Result := '';
  hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, TargetPID);
  if hProcess <> 0 then
    try
      if GetModuleFileNameEX(hProcess, 0, TargetName, MAX_PATH) <> 0 then Result := TargetName
      else if Win32MajorVersion >= 6 then
      begin
        nSize := MAX_PATH;
        ZeroMemory(@TargetName, MAX_PATH);
        @QueryFullProcessImageName := GetProcAddress(GetModuleHandle('kernel32'), 'QueryFullProcessImageNameW');
        if Assigned(QueryFullProcessImageName) then
          if QueryFullProcessImageName(hProcess, 0, TargetName, @nSize) then Result := TargetName
      end;
    finally
      CloseHandle(hProcess);
    end;
end;

function ProcessExists(const AFileName: string; var FoundFiles: TStringList; out HostIndex: Integer): Boolean;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
  fullPath: string;
  myHandle: THandle;
  myPID: DWORD;
  OwnPID: Cardinal;
begin
  HostIndex := -1;
  FoundFiles := TStringList.Create;
  OwnPID := GetCurrentProcessId;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  Result := False;
  while Integer(ContinueLoop) <> 0 do
  begin
    if UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExtractFileName(AFileName)) then
    begin
      myPID := FProcessEntry32.th32ProcessID;
      fullPath := PidToFilename(myPID);
      FoundFiles.Add(fullPath);
      if (myPID = OwnPID) then HostIndex := FoundFiles.Count-1;
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: TStringList;
  i, idx: Integer;
begin
  Memo1.Clear;
  processExists(Application.ExeName,S, idx);
  for i := 0 to S.Count -1 do
    Memo1.Lines.Add(S.Strings[i]);
  if (idx >= 0) then Memo1.Lines.Add('Host is '+S.Strings[idx]);
end;
So sollte es auch mit Unicode hoffentlich zurecht kommen.
Als Rückgabe hast Du nach wie vor die Liste plus den Index vom Host, vielleicht hilft es Dir.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat