【发布时间】:2016-06-15 05:15:36
【问题描述】:
我有以下结构声明:
[StructLayout(LayoutKind.Sequential)]
public struct MyDLLInput
{
...
public fixed char PathtoData[256];
};
PathtoData 按原样显示错误:
"Pointers and fixed-size buffers may only be used in an unsafe context."
MyDLLInput 被传递给 C++ DLL:
public class MyDLL
{
[DllImport("MyDLL.dll",
EntryPoint = "?Unit@@YA?AUOutput@@UInput@@@Z",
CallingConvention = CallingConvention.Cdecl)]
public static extern MyDLLOutput Unit(MyDLLInput UnitInput);
}
MyDLL.h 将成员定义为:
char PathtoData[256];
如何在我的 C# 代码中正确地进行成员声明?
【问题讨论】:
-
你能用成员 PathToData 展示完整的 C++ 类吗?另外为什么不使用Exporting C++ Functions for Use 而不是像 "?Unit@@YA?AUOutput@@UInput@@@Z" 这样的错位函数名称?
-
@ArtavazdBalayan C++ DLL 是由另一位开发人员提供给我的。我没有源代码,虽然我可以得到它。我不太了解 C++,无法按照您的建议进行操作。通过 Dependency Walker 向我提供了损坏的名称。
-
@ArtavazdBalayan 但我会将您的链接转发给该 C++ DLL 的开发人员。错位的名字并没有真正困扰我。一旦它到位,它就看不见了,但我确实喜欢你的建议。
-
我为什么要询问 C++ 代码,因为我不确定您在这里需要不安全。这只是 P/Invoke。