Question

I currently have it defined as

 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

and I have another declaration for each type. I'm trying to use this instead

 <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

Which is off pvinvoke.net, heres how I'm trying to use it:

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

This errors and says Attempt to read or write protected memory, but I use the old RPM declaration I have like this and it works fine.

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

What am I doing wrong?

Was it helpful?

Solution

Your p/invoke declaration is wrong. It should be:

<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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top