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 86 87 88 89 90 91 92 93
|
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAcess As UInt32, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Int32) As IntPtr
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
Private Declare Function GetProcessImageFileName Lib "psapi.dll" (<[In]> ByVal hProcess As IntPtr, <[Out]> strbbuilder As StringBuilder, <[In]> <MarshalAs(UnmanagedType.U4)> ByRef nSize As Integer) As UInteger
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal handle As IntPtr, ByVal modules As IntPtr, ByVal size As Integer, ByRef needed As Integer) As Boolean
Private Declare Function GetModuleInformation Lib "psapi.dll" (<[In]> hProcess As IntPtr, <[In]> hModule As IntPtr, <[Out]> ByRef moduleInfo As MODULEINFO, <[In]> tailleModuleInfo As UInt32) As [Boolean]
Private _targetProcess As Process = Nothing
Private _targetProcessHandle As IntPtr = IntPtr.Zero
Private _hMod As IntPtr
Private Const PROCESS_ALL_ACCESS As UInt32 = &H1F0FFF
Private Const PROCESS_VM_READ As UInt32 = &H10
Private Function AttachToProcess(ByVal windowCaption As String) As Boolean
'Déclaration des variables locales
Dim _allProcesses() As Process = Process.GetProcesses
'Pour chaque processus
For Each pp As Process In _allProcesses
If pp.MainWindowTitle.ToLower.Contains(windowCaption.ToLower) Then
'Pocessus trouvé, on procède à la tentative
Return TryAttachToProcess(pp)
End If
Next
Return False
End Function
Private Function TryAttachToProcess(ByVal proc As Process) As Boolean
'Déclaration des variables locales
Dim sb As New StringBuilder(2000)
Dim nb As Integer = -1
Dim hMods As IntPtr() = New IntPtr(1023) {}
Dim gch As GCHandle
Dim pModules As IntPtr
Dim uiSize As UInteger
Dim cbNeeded As UInteger = 0
'Vérification d'un attachement existant
If Not _targetProcessHandle = IntPtr.Zero Then
Return False
End If
'Enregistrement du processus cible
_targetProcess = proc
'Tentative d'accès au processus
_targetProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, _targetProcess.Id)
If _targetProcessHandle = 0 Then
'Echec lors de la connexion
'Vérifier les droits d'admin
Return False
End If
gch = GCHandle.Alloc(hMods, GCHandleType.Pinned)
pModules = gch.AddrOfPinnedObject()
' Setting up the rest of the parameters for EnumProcessModules
uiSize = CUInt(Marshal.SizeOf(GetType(IntPtr)) * (hMods.Length))
If Not EnumProcessModules(_targetProcess.Handle, pModules, uiSize, cbNeeded) Then
Return False
End If
' Must free the GCHandle object
gch.Free()
Dim info As New MODULEINFO
GetModuleInformation(_targetProcessHandle, hMods(0), info, Marshal.SizeOf(info.GetType))
'Sauvegarde du pointeur de base
Me._hMod = hMods(0)
Return True
End Function
Private Sub DetachFromProcess()
'Vérification d'un attachement existant
If Not (_targetProcessHandle = IntPtr.Zero) Then
_targetProcess = Nothing
'Tentative de fermeture
Try
CloseHandle(_targetProcessHandle)
_targetProcessHandle = IntPtr.Zero
Catch ex As Exception
End Try
End If
End Sub |
Partager