I have an application that marshals data between C# and C++ using PInvoke. The application is a native C++ app that starts the CLR with the C# part internally.
At some point I have to marshal data from C++ to C# and I'm using Marshal.PtrToStructure
for this. The C++ part basically looks like this:
struct X {};
auto xPtr = new X; // somewhere in the application
callCSharp(xPtr); // somewhere else
and the C# part like this:
public void callCSharp(IntPtr xPtr)
{
var x = Marshal.PtrToStructure<X>(xPtr);
}
This code works on my Windows 10 machine but I'm not sure if I could run into trouble with this code. Lifetime management is all done in C++ so I don't have to allocate or free anything on C# side. However I'm not sure if the CLR can always access the native heap. xPtr
is allocated using new
and is therefore located on the native heap of the C++ application. Marshal.PtrToStructure<X>(xPtr)
then tries to read memory at that location but I don't know if this can cause problems.
I've read this which indicates that both the C++ application and the CLR use the same heap (GetProcessHeap
that is) so this seems to support my findings for Windows 10, but it might be different for other OS versions.
Is my sample code above sane? Are there any pitfalls here? Does this code work in Windows 7, 8 and 10?