简体   繁体   中英

P/Invoke syntax - Am I translating my C++ function correctly in C#?

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!

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

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