-
Notifications
You must be signed in to change notification settings - Fork 5
/
GetProcessName_Method2_Remote.pas
95 lines (79 loc) · 2.37 KB
/
GetProcessName_Method2_Remote.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Jean-Pierre LESUEUR (@DarkCoderSc)
// ...
uses psAPI;
// ...
function PhysicalToVirtualPath(APath : String) : String;
var i : integer;
ADrive : String;
ABuffer : array[0..MAX_PATH-1] of Char;
ACandidate : String;
begin
{$I-}
for I := 0 to 25 do begin
ADrive := Format('%s:', [Chr(Ord('A') + i)]);
///
if (QueryDosDevice(PWideChar(ADrive), ABuffer, MAX_PATH) = 0) then
continue;
ACandidate := String(ABuffer).ToLower();
if String(Copy(APath, 1, Length(ACandidate))).ToLower() = ACandidate then begin
Delete(APath, 1, Length(ACandidate));
result := Format('%s%s', [ADrive, APath]);
end;
end;
{$I+}
end;
function GetProcessImagePath(const AProcessId : Cardinal) : String;
var hProc : THandle;
pGetModuleHandle : Pointer;
AFlags : Cardinal;
AThreadId : Cardinal;
hThread : THandle;
hRemoteInstance : Cardinal;
AFileName : Array[0..MAX_PATH -1] of Char;
const PROCESS_QUERY_LIMITED_INFORMATION = $00001000;
begin
result := '';
///
{
Alternatively in the case of Kernel32.dll we could simply call GetModuleHandle('Kernel32.dll')
}
pGetModuleHandle := GetProcAddress(LoadLibrary('Kernel32.dll'), 'GetModuleHandleW');
///
if NOT Assigned(pGetModuleHandle) then
Exit();
AFlags := PROCESS_CREATE_THREAD or
PROCESS_QUERY_LIMITED_INFORMATION;
hProc := OpenProcess(AFlags, false, AProcessId);
if (hProc = 0) then
Exit();
try
hThread := CreateRemoteThread(
hProc,
nil,
0,
pGetModuleHandle,
Pointer(nil),
0,
AThreadId
);
if (hThread = 0) then
Exit();
try
WaitForSingleObject(hThread, INFINITE);
GetExitCodeThread(hThread, hRemoteInstance);
ZeroMemory(@AFileName, MAX_PATH);
///
GetMappedFileName(
hProc,
Pointer(hRemoteInstance),
AFileName,
MAX_PATH
);
result := PhysicalToVirtualPath(UnicodeString(AFileName));
finally
CloseHandle(hThread);
end;
finally
CloseHandle(hProc);
end;
end;