【问题标题】:C++ to C# ReadProcessMemoryC++ 到 C# ReadProcessMemory
【发布时间】:2021-11-23 20:35:27
【问题描述】:

嘿,我正在尝试将旧的 C++ 代码转换为 C#,并且我已经完成了大部分工作,但我现在遇到了一个错误。

C++ 中的代码如下所示:

ReadProcessMemory
(
    _In_ HANDLE hProcess,
    _In_ LPCVOID lpBaseAddress,
    _Out_writes_bytes_to_(nSize,*lpNumberOfBytesRead) LPVOID lpBuffer,
    _In_ SIZE_T nSize,
    _Out_opt_ SIZE_T* lpNumberOfBytesRead
);

void GetSpawnPosition(GameInfo gInfo, GameVector &vecInfo)
{
    if (!ReadProcessMemory(gInfo.hProc, (LPVOID)(gInfo.dwUnityPlayer + Offset::m_vecSpawn), &vecInfo, sizeof(GameVector), 0))
    {
        wprintf(L"Couldnt read cords!\n");
        break;
    }
}

所以我尝试了以下转换为 C#:

[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory
(
    IntPtr hProcess, 
    IntPtr lpBaseAddress, 
    [Out] byte[] lpBuffer, 
    int nSize, 
    out IntPtr lpNumberOfBytesRead
);

 private unsafe void GetSpawnPosition(GameInfo gInfo, GameVector vecInfo)
 {
    if (!ReadProcessMemory(gInfo.hProc, (IntPtr)(gInfo.dwUnityPlayer + m_vecSpawn), vecInfo, sizeof(GameVector), IntPtr.Zero))
    {
        Console.Write("Couldnt read spawn cords!\n");
        break;
    }
}
     

除了 ReadProcess 函数中的“vecInfo”之外,一切都有效。它总是给我错误消息“无法将“GameVector”转换为“byte []”。那么如何解决这个问题或将其转换为 byte[]?

如果您需要“GameVector”的结构来帮助我(C++):

struct GameVector
{
    float x, z, y;

    GameVector operator +(GameVector &vSome)
    {
        return { this->x + vSome.x, vSome.z, this->y + vSome.y };
    }

    GameVector operator -(GameVector& vSome)
    {
        return { this->x - vSome.x, this->z, this->y - vSome.y };
    }
};

我像这样将它转换为 C#(应该没问题):

struct GameVector
{
    float x, z, y;

    public static GameVector operator +(GameVector ImpliedObject, GameVector vSome)
    {
        return new GameVector() { x = ImpliedObject.x + vSome.x, z = vSome.z, y = ImpliedObject.y + vSome.y };
    }

    public static GameVector operator -(GameVector ImpliedObject, GameVector vSome)
    {
        return new GameVector() { x = ImpliedObject.x - vSome.x, z = ImpliedObject.z, y = ImpliedObject.y - vSome.y };
    }

}

我知道它看起来像很多代码,但它只有一个“vecInfo”给了我我现在必须修复的最后一个错误。如果这里要求太多,我很抱歉,但如果有人可以帮助我,那就太棒了,因为它实际上是最后一期 rn。

【问题讨论】:

  • 我猜你应该反序列化字节数组中的结构。见stackoverflow.com/questions/3278827/…
  • 请记住,C# 中的结构是一个值对象。 C++ 是通过引用传递的。那会引起问题。要么传递一个指针,要么使用一个类。

标签: c# c++


【解决方案1】:

您需要将ReadProcessMemory 声明为接受指向GameVector 的指针

注意使用Marshal.SizeOf 而不是sizeof 来获取大小

[DllImport("kernel32.dll")]
static extern bool ReadProcessMemory
(
    IntPtr hProcess, 
    IntPtr lpBaseAddress, 
    [Out] out GameVector lpBuffer, 
    int nSize, 
    out IntPtr lpNumberOfBytesRead
);

private GameVector GetSpawnPosition(GameInfo gInfo)
{
    GameVector vecInfo;
    if (!ReadProcessMemory(gInfo.hProc, (IntPtr)(gInfo.dwUnityPlayer + m_vecSpawn), out vecInfo, Marshal.SizeOf<GameVector>(), IntPtr.Zero))
    {
        throw new Win32Exception(Marshal.GetLastWin32Error(), "Couldnt read spawn cords!\n");
    }
    return vecInfo
}

请注意,您的原始代码无论如何都不会工作,因为GameVector 是一个结构体,因此按值传递

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多