Option Explicit
Private Declare Function NtQueryInformationProcess Lib "Ntdll.dll" (ByVal ProcessHandle As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Type PROCESS_BASIC_INFORMATION
ExitStatus As Long
PEBBaseAddress As Long
AffinityMask As Long
BasePriority As Long
UniqueProcessId As Long
InheritedFromUniqueProcessId As Long
End Type
'Depuis un hProcess
Public Function GetParentProcessIdA(ByVal hProcess As Long) As Long
Dim Pbi As PROCESS_BASIC_INFORMATION
Dim Ret As Long
NtQueryInformationProcess hProcess, 0&, VarPtr(Pbi), 24&, Ret
GetParentProcessIdA = Pbi.InheritedFromUniqueProcessId
End Function
'Depuis un Pid
Public Function GetParentProcessId(ByVal Pid As Long) As Long
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, Pid)
If hProcess Then
GetParentProcessId = GetParentProcessIdA(hProcess)
CloseHandle hProcess
End If
End Function
27/11/2006 : Modification pour pemettre de récupérer le Pid parent depuis le hprocess ou le pid et
pas seulent depuis le Pid afin de ne pas reouvrir un process quon a deja ouvert...
Seul les admins et l'auteur du code lui même peuvent modifier ce code.