哪位仁兄帮忙看看下面代码哪出问题,ReadProcessMemory每次读取都是0,用GetLastError()每次都是显示299。
想做一个小游戏的修改器,读取的内存地址为0043E13C。
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 Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Dim GameHwnd As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Sub Timer1_Timer()
    GameHwnd = FindWindow(vbNullString, "ywne")
    Dim GamePid As Long, GPPid As Long, a As Long
    If GameHwnd > 0 Then
       GetWindowThreadProcessId GameHwnd, GamePid
       GPPid = OpenProcess(PROCESS_ALL_ACCESS, False, GamePid)
       ReadProcessMemory GPPid, &H43E13C, a, 4, 0&
       MsgBox GetLastError()
       CloseHandle GPPid
       Label1.Caption = Str(a)
    End If
End Sub

解决方案 »

  1.   


    Option Explicit
    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 Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    'Private Declare Function ReadProcessMemory 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function GetLastError Lib "kernel32" () As Long
    Dim GameHwnd As Long
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF'修改API声明
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long _
    , lpBaseAddress As Any _
    , lpBuffer As Any _
    , ByVal nSize As Long _
    , lpNumberOfBytesWritten As Long _
    ) As LongPrivate Sub Command1_Click()
        GameHwnd = FindWindow(vbNullString, "Form1")
        Dim GamePid As Long, GPPid As Long
    Dim a(100) As Byte  '修改接收变量的类型
        If GameHwnd > 0 Then
          GetWindowThreadProcessId GameHwnd, GamePid
          GPPid = OpenProcess(PROCESS_ALL_ACCESS, False, GamePid)
        Dim i As Long
         i = ReadProcessMemory(GPPid, ByVal &H43E13C, a(0), 100, 0&)
        
          CloseHandle GPPid
          
          Label1.Caption = StrConv(a, vbUnicode)
        End If
    End Sub
      

  2.   

    1.修改了API声明.'修改API声明
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long _
    , lpBaseAddress As Any _
    , lpBuffer As Any _
    , ByVal nSize As Long _
    , lpNumberOfBytesWritten As Long _
    ) As Long2.修改了接受变量的类型.Dim a(100) As Byte  '修改接收变量的类型
    3.修改了调用i = ReadProcessMemory(GPPid, ByVal &H43E13C, a(0), 100, 0&)
      

  3.   

    我发现我生成exe后GetLastError就返回0,但是仍然无法读取,GetLastError只有在调试的时候才有效吗?
    谢谢Lost_Painting的修改,不过还是不行啊。