c# - P/Invoke 语法 - 我是否在 C# 中正确翻译了我的 C++ 函数?

标签 c# c++ windows interop pinvoke

快速免责声明:我是 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
);

关于c# - P/Invoke 语法 - 我是否在 C# 中正确翻译了我的 C++ 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12013925/

相关文章:

c# - ChangeTracker.Entries() CurrentValue 等于 EF7 中的 OriginalValue

c# - 如何在 C# 中释放桌面应用程序的虚拟机/专用字节

c++ - C++ 类内的作用域

c++ - printf 使用堆栈?

Linux shell 到 windows 批处理文件

Java 内存泄漏迹象

c++ - 如何将 DLL 注入(inject) Delphi 程序

c# - 在 ASP.NET MVC 2 中显示自定义 403 错误页面

c# - 在没有不可变字段的类中覆盖 Object.GetHashCode() 时返回什么?

c++ - 给定一个字符串,我如何才能将另一个字符串中的唯一字符添加到它?