ReadProcessMemory声明

时间:2014-08-01 13:27:01

标签: vb.net pinvoke readprocessmemory

我目前将其定义为

 Private Declare Function ReadProcessMemory1 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer

我对每种类型都有另一个声明。 我正在尝试使用它

 <DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function ReadProcessMemory( _
    ByVal hProcess As IntPtr, _
    ByVal lpBaseAddress As Integer, _
    <Out()> ByRef lpBuffer As Byte(), _
    ByVal dwSize As Integer, _
    ByRef lpNumberOfBytesRead As Integer) As Boolean
End Function

这是关于pvinvoke.net的,以下是我试图使用它的方法:

Public Shared Function Int(address As Integer)
    Dim buffer(3) As Byte
    ReadProcessMemory(pHandle, address, buffer, 4, 0)
    Return BitConverter.ToInt32(buffer, 0)
End Function

这个错误并说试图读取或写入受保护的内存,但是我使用旧的RPM声明我喜欢这个并且它工作正常。

Public Shared Function Int(address As Integer)
    Dim buffer As Integer
    ReadProcessMemory(pHandle, address, buffer, 4, 0)
    Return buffer
End Function

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你的p / invoke声明是错误的。它应该是:

<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 IntPtr, _
    ByRef lpNumberOfBytesRead As IntPtr) As Boolean
End Function