1

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.

Baseult
  • 31
  • 1
  • 6
  • I guess you should deserialize the struct from byte array. See https://stackoverflow.com/questions/3278827/how-to-convert-a-structure-to-a-byte-array-in-c – GibbOne Oct 02 '21 at 18:03
  • Keep in mind that a struct in C# is a value object. Where as the C++ is passing by reference. That would cause issues. Either pass a pointer or use a class. – FernandoZ Oct 03 '21 at 04:28

1 Answers1

2

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

Charlieface
  • 52,284
  • 6
  • 19
  • 43