快速免责声明:我是P / Invoke的新手,所以如果这是一个愚蠢的问题,我先向您道歉。

这是我在C ++中的函数签名:

HRESULT SomeFunction(
    _Out_ unsigned long *count,
    _Outptr_result_buffer_(*count) GUID **ids,
    _In_ const PCWSTR filter
)


我试图在C#中像这样P /调用它:

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


我知道我的代码到达了C ++函数(我可以在windbg中看到它)并且没有崩溃,但是据我所知,参数没有正确传递。我的猜测是我搞砸了C#中的P / Invoke翻译,但我不知道如何解决此问题。任何帮助,将不胜感激!

最佳答案

看起来我找到了解决方案...

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

09-28 09:55