-
Notifications
You must be signed in to change notification settings - Fork 5
/
GetProcessName_Method3.pas
85 lines (72 loc) · 2.47 KB
/
GetProcessName_Method3.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
// Jean-Pierre LESUEUR (@DarkCoderSc)
function PhysicalToVirtualPath(APath : String) : String;
var i : integer;Ge
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;
type PUnicodeString = ^TUnicodeString;
TUnicodeString = record
Length : USHORT;
MaximumLength : USHORT;
Buffer : PWideChar;
end;
// https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntqueryinformationprocess
var _NtQueryInformationProcess : function(
ProcessHandle : THandle;
ProcessInformationClass : DWORD;
ProcessInformation : Pointer;
ProcessInformationLength : ULONG;
ReturnLength : PULONG
) : LongInt; stdcall;
hNTDLL : THandle;
hProc : THandle;
ALength : ULONG;
pImagePath : PUnicodeString;
const PROCESS_QUERY_LIMITED_INFORMATION = $00001000;
ProcessImageFileName = 27;
begin
hProc := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, AProcessId);
if (hProc = 0) then
Exit();
try
hNTDLL := LoadLibrary('NTDLL.DLL');
if (hNTDLL = 0) then
Exit();
try
@_NtQueryInformationProcess := GetProcAddress(hNTDLL, 'NtQueryInformationProcess');
if NOT Assigned(_NtQueryInformationProcess) then
Exit();
///
ALength := (MAX_PATH + SizeOf(TUnicodeString)); // Should be enough :)
GetMem(pImagePath, ALength);
try
if (_NtQueryInformationProcess(hProc, ProcessImageFileName, pImagePath, ALength, @ALength) <> 0) then
Exit();
///
result := PhysicalToVirtualPath(String(pImagePath^.Buffer));
finally
FreeMem(pImagePath, ALength);
end;
finally
FreeLibrary(hNTDLL);
end;
finally
CloseHandle(hProc);
end;
end;