Private Declare Function CloseHandle Lib "kernel32" (tHandle As IntPtr)
Private Declare Function OpenProcess Lib "kernel32" (dwDesiredAccess As Integer, bInheritHandle As Boolean, dwProcessId As Integer) As IntPtr
Private Declare Function ReadProcessMemory Lib "kernel32" Alias "ReadProcessMemory" (hProcess As IntPtr, lpBaseAddress As Integer, <Out> lpBuffer As Byte(), nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Byte
Private Const PROCESS_VM_READ = &H10
Public Function ReadByte(hProcess As IntPtr, Address As Integer, Optional ByVal nsize As Integer = 4) As Byte()
Dim vBuffer(nsize - 1) As Byte
Dim lpNumberOfBytesWritten As Integer
ReadProcessMemory(hProcess, Address, vBuffer, nsize, lpNumberOfBytesWritten)
Return vBuffer
End Function
Public Function Compare(hProcess As IntPtr, Address As Long, Pattern As Byte(), Mask As String) As Boolean
Dim pTemp As Byte
For i = 0 To Mask.Length - 1 Step 1
If Mask(i) = "?" Then
Continue For
End If
pTemp = ReadByte(hProcess, (Address + i), 1)(0)
If pTemp = Nothing Then
Return False
End If
If pTemp <> Pattern(i) Then
Return False
End If
Next
Return True
End Function
Public Function FindPattern(AProcess As String, Pattern As Byte(), Mask As String) As Long
Dim proc = Process.GetProcessesByName(AProcess).FirstOrDefault()
If proc Is Nothing Then
Return -1
End If
Dim hProc = OpenProcess(PROCESS_VM_READ, False, proc.Id)
If hProc = IntPtr.Zero Then
Return -1
End If
Try
For dwCurrentAddress As Long = &H400000 To &H7FFFFFF Step 1
If Compare(hProc, dwCurrentAddress, Pattern, Mask) Then
Return dwCurrentAddress
End If
Next
Return -1
Finally
CloseHandle(hProc)
End Try
End Function