【发布时间】:2014-09-04 17:01:23
【问题描述】:
结构的 C++ 代码如下:
typedef struct _a astruct;
struct _a {
BYTE fi, Sec, *D, *IIV, PV;
bool Visited;
};
以及使用它的函数:
astruct DoPDC(string *InitialData);
我想通过VB.NET使用这个函数(包含在一个DLL中),所以我为这个结构写了以下代码:
<StructLayout(LayoutKind.Sequential)> _
Public Structure astruct
Dim fi As Byte
Dim Sec As Byte
Dim D As Byte()
Dim IIV As Byte()
Dim PV As Byte
<MarshalAs(UnmanagedType.Bool)> Dim Visited As Boolean
End Structure
以及函数声明:
<DllImport("astr.dll", EntryPoint:="DoPDC", BestFitMapping:=False, CallingConvention:=CallingConvention.Cdecl, ThrowOnUnmappableChar:=True, CharSet:=CharSet.Ansi)> _
Public Function DoPDC(ByVal InitialData As String()) As <MarshalAs(UnmanagedType.Struct)> astruct
End Function
所以,我得到了悲惨的错误MarshalDirectiveError,特别是Method's type signature is not PInvoke compatible.我以前从未见过这个错误,互联网上有很多不同的解决方案,但没有人有我的情况,我作为参数传递字符串数组,返回一个包含更多数组的结构体!
结果我不知道哪里出了问题:参数,返回值,返回值里面的数组还是一起?我很困惑...因此,我的问题是:如何修改我的 VB.NET P/Invoke 代码,使其正常工作?
(请注意,我无法更改 C++ DLL,因为它不是我的,因此我没有代码...)
【问题讨论】:
标签: c++ arrays vb.net pinvoke marshalling