Option Explicit
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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Sub Command1_Click()
Dim hProcess As Long, pid As Long, bb1 As String
GetWindowThreadProcessId Form1.hwnd, pid
hProcess = OpenProcess(&H1F0FFF, True, pid)
MsgBox hProcess & " " & pid & " " & WriteProcessMemory(hProcess, &H401000, &HF1234512, 4, 0&) '写内存API,加个MSG检测前两部的结果
End Sub
Private Declare Function WriteProcessMemory _
Lib "kernel32" (ByVal hProcess As Long, _
ByVal lpBaseAddress As Any, _
ByVal lpBuffer As Any, _
ByVal nSize As Long, _
lpNumberOfBytesWritten As Long) As Long
不要误人子弟!你这样声明是写入代码不成功的,需要如下声明:
Private Declare Function WriteProcessMemory _
Lib "kernel32" (ByVal hProcess As Long, _
ByVal lpBaseAddress As Any, _ lpBuffer As Any, _
ByVal nSize As Long, _
lpNumberOfBytesWritten As Long) As Long
因为byval传入的是地址!