2011年9月1日 星期四

VB.net Read \ WriteProcessMemory

Imports System.Runtime.InteropServices
'==============================================
_
Private Shared Function OpenProcess(ByVal dwDesiredAccess As ProcessAccess, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
End Function
Public Enum ProcessAccess As Integer
AllAccess = CreateThread Or DuplicateHandle Or QueryInformation Or SetInformation Or Terminate Or VMOperation Or VMRead Or VMWrite Or Synchronize
CreateThread = &H2
DuplicateHandle = &H40
QueryInformation = &H400
SetInformation = &H200
Terminate = &H1
VMOperation = &H8
VMRead = &H10
VMWrite = &H20
Synchronize = &H100000
End Enum
_
Public Shared Function WriteProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As System.UInt32, ByRef lpNumberOfBytesWritten As Int32) As Boolean
End Function
_
Private Shared Function GetWindowThreadProcessId(ByVal hwnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
End Function

Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
' Form Load
Dim window As IntPtr = FindWindow(vbNullString, "踩地雷")
GetWindowThreadProcessId(window, PID)
ph = OpenProcess(ProcessAccess.AllAccess, False, PID) ' OpenProcess By AllAccess
Dim i(2) As Byte ' 寫入的記億體數值
Dim ddd As Integer ' Ref 傳回他總共寫入的值
' &H1005194 是用CE看的 它是地雷數目
If WriteProcessMemory(ph, &H1005194, i, 1, ddd) = 0 Then
MsgBox("Faied")
MsgBox("ddd = " & Str(ddd))
Else
MsgBox("寫入成功", vbInformation, "OK")
End If
'================================READMemory
Dim window As IntPtr = FindWindow(vbNullString, "踩地雷") ' GetWindowHandle
Dim ph As IntPtr
Dim PID As Integer ' GetProcessID[code] Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim window As IntPtr = FindWindow(vbNullString, "踩地雷")
Dim ID As Integer
Dim PHandle As IntPtr
GetWindowThreadProcessId(window, ID)
PHandle = OpenProcess(ProcessAccess.AllAccess, False, ID)
Dim buffer(3) As Byte
Dim rvalue As Integer
Dim bool As Boolean
bool = ReadProcessMemory(PHandle, &H1005194, buffer, 4, rvalue)
MsgBox(Str(rvalue))
MessageBox.Show(BitConverter.ToInt32(buffer, 0))

沒有留言:

張貼留言