worm 发表于 2018-10-9 12:49:45

通过进程名获取该进程的完整路径的问题~

delphi XE 10.2.3,用下面的函数: 【调用是: ShowMessage(GetPathFileofModule('explorer.exe'));   要uses TlHelp32,psapi;】在32位系统下可以获取到路径,但是在64位系统下就不行了。寻大神指点~~~~

function GetPathFileofModule(ModuleName:String):String; //枚举进程文件所在路径
var
hProcSnap: THandle;
pProcess: THandle;
pe32: TProcessEntry32;
buf:array of char;
hMod:HMODULE;
cbNeeded:DWORD;
begin
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
if hProcSnap = INVALID_HANDLE_VALUE then Exit;
pe32.dwSize := SizeOf(ProcessEntry32);
if Process32First(hProcSnap, pe32) = True then
while Process32Next(hProcSnap, pe32) = True do
begin
if uppercase(pe32.szExeFile)=uppercase(ModuleName) then
begin
pProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or
PROCESS_VM_READ,
FALSE,
pe32.th32ProcessID);
if pProcess<>0 then
begin
if EnumProcessModules( pProcess,@hMod,sizeof(hMod),cbNeeded) then
begin
ZeroMemory(@buf,MAX_PATH+1);
GetModuleFileNameEx(pProcess, hMod,buf,MAX_PATH+1);
Result:=strpas(buf);
end;
end;
end;
end;
CloseHandle(hProcSnap);
end;


worm 发表于 2018-10-9 14:15:18

查网上资料发现,可以用QueryFullProcessImageName 这个函数,最低要求VISTA系统,XP太老了,这个函数可以用! 但是不知道怎么利用~

worm 发表于 2018-10-9 15:26:47

function NazwaProcesu(const uchwyt: Thandle): string;
type
TQueryFullProcessImageName = function(hProcess: Thandle; dwFlags: DWORD; lpExeName: PChar; nSize: PDWORD): BOOL; stdcall;
var
pid: DWORD;
hProcess: Thandle;
sciezka: array of Char;
QueryFullProcessImageName: TQueryFullProcessImageName;
nSize: cardinal;
begin
Result := '';
GetWindowThreadProcessId(uchwyt, pid);
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, pid);
if hProcess <> 0 then
    try
      if GetModuleFileNameEX(hProcess, 0, sciezka, MAX_PATH) <> 0 then Result := sciezka
      else if Win32MajorVersion >= 6 then
      begin
      nSize := MAX_PATH;
      ZeroMemory(@sciezka, MAX_PATH);
      @QueryFullProcessImageName := GetProcAddress(GetModuleHandle('kernel32'), 'QueryFullProcessImageNameW');
      if Assigned(QueryFullProcessImageName) then
          if QueryFullProcessImageName(hProcess, 0, sciezka, @nSize) then Result := sciezka
      end;
    finally
      CloseHandle(hProcess);
    end;
end;

网上找的一段代码,不知道 怎么调用,比如说知道有个进程 explorer.exe 怎么调用这个函数呢? 大神指点一下~

abea008 发表于 2018-12-17 12:35:00

xe10.2.3都不支持winxp了。
页: [1]
查看完整版本: 通过进程名获取该进程的完整路径的问题~