VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 95|回复: 3

[求助] 请问我的ReadProcessMemory哪里出错了

[复制链接]
发表于 2020-10-7 01:08:36 | 显示全部楼层 |阅读模式
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
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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Dim Dizhi As Long

Private Function ncnr(IpAddress As Long) As Integer
Dim hwnd As Long
Dim pid As Long
Dim pHandle As Long
hwnd = FindWindow(vbNullString, "三国")
GetWindowThreadProcessId hwnd, pid
pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
ReadProcessMemory pHandle, IpAddress, ByVal VarPtr(ncnr), 4, 0&
CloseHandle hProcess
End Function

Private Sub Timer1_Timer()
Dim HP As Long
Dizhi = &H1C515B8
HP = ncnr(Dizhi+160)
Print Str(HP)
End Sub

基址=1C515B8 偏移=160
游戏人物血量是 114
但是Print Str(HP) 是334543

请问哪里出错了
发表于 4 天前 | 显示全部楼层
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
注意第二个参数的声明,其实我是搞不懂,这个API第二个参数明明就是传的值,在很多编程语言中给的列子都是指针,其他语言就不说了,VB6也跟着学,ANY 会把很多新手绕晕。VB6正确声明应该是下面这样。
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long,ByVal  lpBaseAddress As long, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

如果按 你上面的声明
ReadProcessMemory pHandle, IpAddress, ByVal VarPtr(ncnr), 4, 0&
第二个参数传进去的就是 lpaddress的地址而不是lpaddress本身的值,读到别的地方当然不正确。
你要这样用才正确
ReadProcessMemory pHandle, ByVal IpAddress, ByVal VarPtr(ncnr), 4, 0&
而第三个参数需要传地址,你偏偏做了多余的操作,可以直接这样写。
ReadProcessMemory pHandle,ByVal IpAddress, ncnr, 4, 0&

如果是用我下面的声明就可以这样写
ReadProcessMemory pHandle, IpAddress, ncnr, 4, 0&



回复 支持 反对

使用道具 举报

发表于 4 天前 | 显示全部楼层
本帖最后由 dnxlwg2020 于 2020-10-17 16:16 编辑

如果你要读的是字符串
第三个参数才需要  byval strptr(接收变量)  不然编译器自动获取的安符串变量地址不正确程序会崩


回复 支持 反对

使用道具 举报

发表于 昨天 17:07 | 显示全部楼层
基址会变,你这么写死不合适吧
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2020-10-21 16:19

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表