Vb.net ReadProcessMemory不断返回0

Vb.net ReadProcessMemory不断返回0,vb.net,winapi,readprocessmemory,Vb.net,Winapi,Readprocessmemory,我目前正在开发一个小爱好项目,通过VB.NET在我的G15键盘上的游戏中显示健康信息 当我通过API调用使用ReadProcessMemory时,它总是返回零。MSDN文档建议我使用Marshal.GetLastWin32Error()调用找出错误所在,它返回1400:无效窗口句柄 我现在不知道函数的第一个参数是要一个窗口句柄还是一个进程id。不管怎样,我已经尝试了FindWindow和在应用程序运行时硬编码进程id(从任务管理器获取) 我试过三种不同的游戏,城市恐怖,侠盗猎车手:SA和wind

我目前正在开发一个小爱好项目,通过VB.NET在我的G15键盘上的游戏中显示健康信息

当我通过API调用使用ReadProcessMemory时,它总是返回零。MSDN文档建议我使用Marshal.GetLastWin32Error()调用找出错误所在,它返回1400:无效窗口句柄

我现在不知道函数的第一个参数是要一个窗口句柄还是一个进程id。不管怎样,我已经尝试了FindWindow和在应用程序运行时硬编码进程id(从任务管理器获取)

我试过三种不同的游戏,城市恐怖,侠盗猎车手:SA和windows 3D弹球,从一个叫做作弊引擎的应用程序中获取内存地址;他们似乎都失败了

下面是我正在使用的代码:

API调用:

Private Declare Function ReadProcessMemory Lib "kernel32" ( _
ByVal hProcess As Integer, _
ByVal lpBaseAddress As Integer, _
ByRef lpBuffer As Single, _
ByVal nSize As Integer, _
ByRef lpNumberOfBytesWritten As Integer _
) As Integer
方法:

Dim address As Integer
address = &HA90C62&
Dim valueinmemory As Integer

Dim proc As Process = Process.GetCurrentProcess
For Each proc In Process.GetProcesses
    If proc.MainWindowTitle = "3D Pinball for Windows - Space Cadet" Then
        If ReadProcessMemory(proc.Handle.ToInt32, address, valueinmemory, 4, 0) = 0 Then
            MsgBox("aww")
        Else
            MsgBox(CStr(valueinmemory))
        End If
    End If
Next

Dim lastError As Integer
lastError = Marshal.GetLastWin32Error()
MessageBox.Show(CStr(lastError))

有人能给我解释一下为什么它不起作用吗?提前感谢。

首先,您的方法签名错误,Single=Float,而原始参数是LPBUF类型

使用此方法签名:

<DllImport("kernel32.dll", SetLastError=true)> _
Public Shared Function ReadProcessMemory( _
ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
<Out()>ByVal lpBuffer() As Byte, _
ByVal dwSize as Integer, _
ByRef lpNumberOfBytesRead as Integer
) As Boolean
End Function
_
公共共享函数ReadProcessMemory(_
ByVal HPROSS作为IntPtr_
ByVal lpBaseAddress作为IntPtr_
ByVal lpBuffer()作为字节_
ByVal dwSize为整数_
ByRef LPNumberOfBytes读取为整数
)作为布尔值
端函数

其次,我认为HPProcess句柄需要OpenProcess函数打开的句柄,而不是窗口句柄。

首先,您的方法签名是错误的,Single=Float,而原始参数是LPBUF类型

使用此方法签名:

<DllImport("kernel32.dll", SetLastError=true)> _
Public Shared Function ReadProcessMemory( _
ByVal hProcess As IntPtr, _
ByVal lpBaseAddress As IntPtr, _
<Out()>ByVal lpBuffer() As Byte, _
ByVal dwSize as Integer, _
ByRef lpNumberOfBytesRead as Integer
) As Boolean
End Function
_
公共共享函数ReadProcessMemory(_
ByVal HPROSS作为IntPtr_
ByVal lpBaseAddress作为IntPtr_
ByVal lpBuffer()作为字节_
ByVal dwSize为整数_
ByRef LPNumberOfBytes读取为整数
)作为布尔值
端函数

其次,我相信HPProcess句柄需要OpenProcess函数打开的句柄,而不是窗口句柄。

谢谢你,阿鲁尔,我已经解决了我的问题

Dim address As Integer
address = &HA90C62&
Dim floatvalueinmemory() As Byte

Dim proc As Process = Process.GetCurrentProcess
For Each proc In Process.GetProcesses
    If proc.MainWindowTitle = "3D Pinball for Windows - Space Cadet" Then
        Dim winhandle As IntPtr = OpenProcess(PROCESS_ACCESS.PROCESS_VM_READ, True, proc.Id)

        If ReadProcessMemory(winhandle, address, floatvalueinmemory, 4, 0) = 0 Then
            Dim lastError As Integer
            lastError = Marshal.GetLastWin32Error()
            MessageBox.Show(CStr(lastError))
            MsgBox("aww")
        Else
            MsgBox("woo")
        End If

        CloseHandle(winhandle)
    End If
Next
现在它认为句柄是有效的,并尝试读取进程内存,但我得到错误消息299:ReadProcessMemory或WriteProcessMemory请求只完成了一部分


有人对我应该如何解决这个问题有什么想法吗?

谢谢你,阿鲁尔,我已经解决了我的问题

Dim address As Integer
address = &HA90C62&
Dim floatvalueinmemory() As Byte

Dim proc As Process = Process.GetCurrentProcess
For Each proc In Process.GetProcesses
    If proc.MainWindowTitle = "3D Pinball for Windows - Space Cadet" Then
        Dim winhandle As IntPtr = OpenProcess(PROCESS_ACCESS.PROCESS_VM_READ, True, proc.Id)

        If ReadProcessMemory(winhandle, address, floatvalueinmemory, 4, 0) = 0 Then
            Dim lastError As Integer
            lastError = Marshal.GetLastWin32Error()
            MessageBox.Show(CStr(lastError))
            MsgBox("aww")
        Else
            MsgBox("woo")
        End If

        CloseHandle(winhandle)
    End If
Next
现在它认为句柄是有效的,并尝试读取进程内存,但我得到错误消息299:ReadProcessMemory或WriteProcessMemory请求只完成了一部分


有人知道我应该如何解决这个问题吗?

消息299:ReadProcessMemory或WriteProcessMemory请求仅完成了一部分,这意味着我试图读取的内存受到了保护


感谢您的帮助,我将把arul的答案标记为答案。

消息299:ReadProcessMemory或WriteProcessMemory请求仅完成了一部分,这意味着我试图读取的内存受到了保护

谢谢你的帮助,我要把阿鲁尔的答案记下来