2012年9月10日 星期一

writeprocessmemory和readprocessmemory

範例1:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Integer
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Sub TmPointer_Timer()
Dim hwn As Long
Dim pid As Long
Dim pHandle As Long
Dim Hp As Long
Dim Mp As Long
Dim Add As Long
Dim cde As Long


hwn = FindWindow(vbNullString, "MapleStory")
If hwn = 0 Then
Label1.Caption = "外掛未連接"
Else
Label1.Caption = "外掛連接成功"
End If
GetWindowThreadProcessId hwn, pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)

ReadProcessMemory pHandle, ByVal &HE05D14, Add, 4, ByVal 0& '讀取Add
WriteProcessMemory pHandle, ByVal Add + &H50, 20, 1, ByVal 0& '寫入HP警告值20
WriteProcessMemory pHandle, ByVal Add + &H54, 20, 1, ByVal 0& '寫入MP警告值20
ReadProcessMemory pHandle, ByVal &HE05FB8, cde, 4, ByVal 0&
ReadProcessMemory pHandle, ByVal cde + &H214C, Hp, 4, ByVal 0& '讀取Add+HP偏移值
ReadProcessMemory pHandle, ByVal cde + &H2150, Mp, 4, ByVal 0& '讀取Add+MP偏移值
CloseHandle pHandle


lblHP.Caption = Hp
lblMP.Caption = Mp

End Sub


範例2: 參考引用
writeprocessmemory和readprocessmemory的基本用法
搞了半天总算懂了writeprocessmemory和readprocessmemory的基本用法
代码如下
Private Sub Command1_Click()
Dim StrLength As Long, Rtn As Long, hProcess As Long, WriteStr As String
Dim GetStr As String
Dim i As Long
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, CLng(Text1.Text))
    WriteStr = Text2.Text
    StrLength = LenB(WriteStr)
    i = VirtualAllocEx(hProcess, ByVal 0&, StrLength, MEM_COMMIT, PAGE_READWRITE)
    Debug.Print Rtn
    Debug.Print WriteProcessMemory(hProcess, ByVal i, ByVal StrPtr(WriteStr), LenB(WriteStr), 0)
    GetStr = Space(LenB(WriteStr) \ 2)
    Debug.Print ReadProcessMemory(hProcess, ByVal i, ByVal StrPtr(GetStr), LenB(WriteStr), 0)
    MsgBox GetStr
End Sub
writestr是要写入的字符串。
代码是写入字符串然后读取。

沒有留言:

張貼留言