Reputation: 10553
A quick disclaimer: I'm very new to P/Invoke, so I apologize in advance if this is a silly question.
Here's my function signature in C++:
HRESULT SomeFunction(
_Out_ unsigned long *count,
_Outptr_result_buffer_(*count) GUID **ids,
_In_ const PCWSTR filter
)
And I'm trying to P/Invoke it as such in C#:
[StructLayout(LayoutKind.Sequential)]
struct GUID
{
public int a;
public short b;
public short c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
public byte[] d;
}
[DllImport("MyDll.dll", EntryPoint="SomeFunction")]
[return: MarshalAs(UnmanagedType.I8)]
private static extern Int64 SomeFunction
(
out ulong count,
[MarshalAs(UnmanagedType.LPArray)]
out GUID[] ids,
string filter
);
I know my code gets reaches the C++ function (I can see this in windbg) and there is no crash, but from what I can tell, the parameters aren't being passed correctly. My guess is that I've messed up my P/Invoke translation in C#, but I have no idea how to fix this. Any help would be appreciated!
Upvotes: 0
Views: 408
Reputation: 10553
Looks like I found my solution...
[DllImport("MyDll.dll", EntryPoint="SomeFunction")]
[return: MarshalAs(UnmanagedType.I4)]
private static extern int SomeFunction
(
out uint count,
[MarshalAs(UnmanagedType.LPArray)]
out GUID[] ids,
[InAttribute()]
[MarshalAsAttribute(UnmanagedType.LPWStr)]
string filter
);
Upvotes: 3