giovedì 5 agosto 2010

Recuperare l’id del processo padre

Prendendo spunto da un post apparso sul newsgroup dedicato a Visual Basic (http://social.msdn.microsoft.com/Forums/it-IT/visualbasicit/threads), vorrei proporvi un metodo di estensione per la classe Process che, utilizzando P/Invoke, recupera l’identificativo del processo padre di un processo (se esiste), cioè l’identificativo del processo che ha aperto il processo.

Il modulo che definisce il metodo di estensione è il seguente:

  1. Imports System.Runtime.InteropServices
  2. Imports System.Runtime.CompilerServices
  3.  
  4. Public Module ProcessExtension
  5.     <DllImport("kernel32.dll", SetLastError:=True)>
  6.     Private Function CreateToolhelp32Snapshot(ByVal dwFlags As SnapshotFlags, ByVal th32ProcessID As UInteger) As IntPtr
  7.     End Function
  8.  
  9.     <DllImport("kernel32.dll")> _
  10.     Private Function Process32First(ByVal hSnapshot As IntPtr, ByRef lppe As PROCESSENTRY32) As Boolean
  11.     End Function
  12.  
  13.     <DllImport("kernel32.dll")> _
  14.     Private Function Process32Next(ByVal hSnapshot As IntPtr, ByRef lppe As PROCESSENTRY32) As Boolean
  15.     End Function
  16.  
  17.  
  18.     <Flags()> _
  19.     Private Enum SnapshotFlags As Integer
  20.         HeapList = &H1
  21.         Process = &H2
  22.         Thread = &H4
  23.         [Module] = &H8
  24.         Module32 = &H10
  25.         Inherit = &H80000000
  26.         All = &H1F
  27.     End Enum
  28.  
  29.     <StructLayout(LayoutKind.Sequential)> _
  30.     Private Structure PROCESSENTRY32
  31.         Public dwSize As UInteger
  32.         Public cntUsage As UInteger
  33.         Public th32ProcessID As UInteger
  34.         Public th32DefaultHeapID As IntPtr
  35.         Public th32ModuleID As UInteger
  36.         Public cntThreads As UInteger
  37.         Public th32ParentProcessID As UInteger
  38.         Public pcPriClassBase As Integer
  39.         Public dwFlags As UInteger
  40.         <VBFixedString(260), MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szExeFile As String
  41.     End Structure
  42.  
  43.     <Extension()>
  44.     Public Function GetParentProcessID(ByVal process As Process) As UInteger?
  45.         If process Is Nothing Then Throw New NullReferenceException()
  46.         Dim retval As UInteger? = Nothing
  47.         Dim snapShot = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0)
  48.         If snapShot <> IntPtr.Zero Then
  49.             Dim procEntry As New PROCESSENTRY32
  50.             procEntry.dwSize = CUInt(Marshal.SizeOf(GetType(PROCESSENTRY32)))
  51.             Dim okFlag = Process32First(snapShot, procEntry)
  52.             While (okFlag)
  53.                 If process.Id = procEntry.th32ProcessID Then
  54.                     retval = procEntry.th32ParentProcessID
  55.                     Exit While
  56.                 Else
  57.                     okFlag = Process32Next(snapShot, procEntry)
  58.                 End If
  59.             End While
  60.         End If
  61.         Return retval
  62.     End Function
  63. End Module

Grazie al metodo di estensione possiamo semplicemente scrivere

  1. .
  2. Dim parentId = Process.GetCurrentProcess.GetParentProcessID()
  3. .
  4. .


 

Nessun commento: