类型'字节的1维数组'的值不能转换为整数,任何想法?

时间:2012-06-05 17:16:52

标签: vb.net pointers readprocessmemory

我已经使用下面的代码从指针和偏移量中读取内存地址然后,现在我再次使用它并且无法弄清楚我上次如何使用它,我收到了错误“类型'字节的1维数组'的值不能转换为整数”突出显示ReadProcessMemory调用中的BytesAtAddress变量。

我已经坚持了大约25分钟,任何人都可以指出我的错误,因为我确信这很简单。

谢谢!

Public Shared Function ReadPointerFromMemory(ByVal BaseAddress As Integer, ByVal PointerOffset As Integer, ByVal BytesToRead As Integer, ByVal pHandle As IntPtr) As Integer
    Dim BytesAtAddress As Byte() = New Byte(BytesToRead - 1) {}
    Dim BytesRead As Integer
    Dim MemoryBase As Integer
    Dim ReturnVal As Integer
    ReadProcessMemory(pHandle, CType(BaseAddress, IntPtr), BytesAtAddress, BytesToRead, BytesRead)
    MemoryBase = BitConverter.ToInt32(BytesAtAddress, 0)
    MemoryBase += PointerOffset
    ReadProcessMemory(pHandle, CType(MemoryBase, IntPtr), BytesAtAddress, BytesToRead, BytesRead)
    ReturnVal = BitConverter.ToInt32(BytesAtAddress, 0)
    Return ReturnVal
End Function

1 个答案:

答案 0 :(得分:1)

我假设您使用ReadProcessMemory来自:http://msdn.microsoft.com/en-us/library/ms886794.aspx作为参考。

BOOL ReadProcessMemory( 
  HANDLE hProcess, 
  LPCVOID lpBaseAddress, 
  LPVOID lpBuffer, 
  DWORD nSize, 
  LPDWORD lpNumberOfBytesRead 
);

因此,根据错误,您需要的是缓冲区BytesAtAddress上的指针而不是数组本身。您也可以将MemoryBase As Integer更改为MemoryBase As IntPtr,将ReturnVal As Integer更改为ReturnVal As IntPtr。或者甚至更好地将所有需要的变量ByRef而不是ByVal传递给您的函数。