gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:54:56 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com