简体   繁体   中英

C++ to C# ReadProcessMemory

Hey I am trying to convert old C++ code to C# and I got most of it working already but I am stuck on one error now.

The Code in C++ looks like this:

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;
    }
}

So I tried the following Conversion to 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;
    }
}
     

Everything works except the "vecInfo" in the ReadProcess Function. It always gives me the error message Conversion of "GameVector" to "byte[]" not possible. So how do I fix that or convert it to byte[]?

In case you need the struct of "GameVector" to help me out (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 };
    }
};

I converted it to C# like this (should be fine ig):

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 };
    }

}

I know it looks like a lot of code but its only the single "vecInfo" that gives me my last eror that I have to fix now. I'm sorry if thats too much to ask for here but would be awesome if someone could help me out since it is literally the last issue rn.

You need to declare ReadProcessMemory as accepting a pointer to GameVector

Note the use of Marshal.SizeOf rather than sizeof to get the size

[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
}

Note that your original code would never have worked anyway, because GameVector is a struct and therefore passed by value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM