Please look at the code below to retrieve the PBI (Process Basic
Information) of the parent process of a given process.
typedef NTSTATUS (*QUERY_INFO_PROCESS) (
__in HANDLE ProcessHandle,
__in PROCESSINFOCLASS ProcessInformationClass,
__out_bcount(ProcessInformationLength) PVOID ProcessInformation,
__in ULONG ProcessInformationLength,
__out_opt PULONG ReturnLength
);
QUERY_INFO_PROCESS ZwQueryInformationProcess;
/*
* Function to get the parent process Id of a given process
*/
NTSTATUS
GetParentProcessId ( __in HANDLE processId,
__out PHANDLE parentProcessId
)
{
NTSTATUS status;
PEPROCESS eProcess;
HANDLE hProcess;
PROCESS_BASIC_INFORMATION pbi;
PAGED_CODE(); // this eliminates the possibility of the IDLE
Thread/Process
if ( processId == (HANDLE) 4 ) { // if system process
*parentProcessId = 0;
return STATUS_SUCCESS;
}
status = PsLookupProcessByProcessId(processId, &eProcess);
if(NT_SUCCESS(status))
{
status = ObOpenObjectByPointer(eProcess,0, NULL,
0,0,KernelMode,&hProcess);
if( ! NT_SUCCESS(status))
{
// DbgPrint(“Error: ObOpenObjectByPointer Failed: %08x\n”,
status);
}
ObDereferenceObject(eProcess);
} else {
//DbgPrint(“Error: PsLookupProcessByProcessId Failed: %08x\n”,
status);
}
if (NULL == ZwQueryInformationProcess) {
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");
ZwQueryInformationProcess =
(QUERY_INFO_PROCESS) MmGetSystemRoutineAddress(&routineName);
if (NULL == ZwQueryInformationProcess) {
DbgPrint(“Cannot resolve ZwQueryInformationProcess\n”);
}
}
/* Retrieve the process basic information (pbi) from the handle of the
process */
status = ZwQueryInformationProcess( hProcess,
ProcessBasicInformation,
&pbi,
sizeof (PROCESS_BASIC_INFORMATION),
NULL);
if (NT_SUCCESS(status)) {
*parentProcessId = (HANDLE) pbi.InheritedFromUniqueProcessId;
}
return status;
}
You can recursively call GetParentProcessId () till the parentprocessid ==
0, to get the parent, grand parent, great grand parent, so on and so
forth…
Hope this helps!
-subbu
On Fri, Oct 24, 2008 at 12:30 PM, wrote:
> Hi Guys,
> I wanted to get information of parent’s parent process. I have
> registered create process notification and in this call back function I want
> to get information of grand parent process of current process. Can anybody
> help me how to get information of parent’s parent of my process. Thanks in
> advance…
>
> Regards
> CKT
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule debugging and file system seminars
> (including our new fs mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> You are currently subscribed to ntfsd as: xxxxx@gmail.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>