-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathntsup.c
54 lines (49 loc) · 1.76 KB
/
ntsup.c
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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vitual File System management tool
//
// module: ntsup.c
// $Revision: 62 $
// $Date: 2012-05-20 18:48:20 +0400 (Вс, 20 май 2012) $
// description:
// Sample Virtual File System management tool. Native NT API support.
#include <main.h>
// Returns full path of the main module of the process specified by ProcessId.
BOOL GetProcessImagePath(
HANDLE ProcessId,
PWCHAR pImagePath,
ULONG PathLength
)
{
ULONG bSize;
BOOL Ret = FALSE;
NTSTATUS ntStatus;
HANDLE hProcess;
CLIENT_ID ClientId = {0};
OBJECT_ATTRIBUTES oa = {0};
ClientId.UniqueProcess = ProcessId;
InitializeObjectAttributes(&oa, NULL, OBJ_CASE_INSENSITIVE, 0, NULL);
if (NT_SUCCESS(ZwOpenProcess(&hProcess, GENERIC_READ, &oa, &ClientId)))
{
ntStatus = ZwQueryInformationProcess(hProcess, ProcessImageFileName, NULL, 0, &bSize);
if (ntStatus == STATUS_INFO_LENGTH_MISMATCH)
{
PUNICODE_STRING ProcessFullName = (PUNICODE_STRING)malloc(bSize);
if (ProcessFullName)
{
ntStatus = ZwQueryInformationProcess(hProcess, ProcessImageFileName, ProcessFullName, bSize, &bSize);
if (NT_SUCCESS(ntStatus))
{
if ((ProcessFullName->Length + sizeof(WCHAR)) <= (PathLength * sizeof(WCHAR)))
{
memcpy(pImagePath, ProcessFullName->Buffer, ProcessFullName->Length);
pImagePath[ProcessFullName->Length / sizeof(WCHAR)] = 0;
Ret = TRUE;
}
} // if (NT_SUCCESS(ntStatus))
free(ProcessFullName);
} // if (ProcessFullName)
} // if (ntStatus == STATUS_INFO_LENGTH_MISMATCH)
ZwClose(hProcess);
} // if (NT_SUCCESS(ZwOpenProcess(&hProcess, GENERIC_READ, &oa, &ClientId)))
return(Ret);
}