Your C# struct WCharStringPtr
contains two data members. These are both marshalled as pointers. On the other hand the C++ fields you map this to are of type wchar_t*
, which is just a single pointer. So that's a clear mismatch.
What is not clear is how the memory allocation for these strings is meant to be handled. Does the caller allocate the memory, or does the callee?
Let is suppose that the callee allocates the memory. In that case the GraphicsAdapterDesc
struct should be declared like this:
public struct GraphicsAdapterDesc {
public IntPtr AdapterName;
public int AdapterIndex;
public IntPtr HardwareHash;
public long DedicatedVMEM;
public long DedicatedSMEM;
public long SharedSMEM;
public int NumOutputs;
};
You call Marshal.PtrToStringUni()
to convert AdapterName
and HardwareHash
to strings once the function returns. As for deallocation, that's beyond the scope of the question.
This seems the most plausible option. It seems rather clear that the callee cannot be expected to allocate the memory for the two strings since the length of the buffers is not passed in the interface. And of course the strings are declared const wchar_t*
which is further evidence.