我试图将一个结构从VB传递给C.将参数从VB.Net传递给C(结构体)
此结构只有2个成员。 问题是只有第一个成员保持该值。
我想这是每个成员的大小问题,但我不知道如何解决。
实施例和代码:
VB .NET代码:
<DllImport("UserMode_C.dll")> _
Shared Sub someExample(ByVal handleOfSomething As IntPtr, ByRef Filter As __Structure)
End Sub
<StructLayout(LayoutKind.Sequential)> _
Structure __Structure
<MarshalAs(UnmanagedType.U8)> Public UsbSerial As ULong
<MarshalAs(UnmanagedType.U8)> Public UsbType As ULong
End Structure
Dim Buffer As New __Structure
Buffer.UsbSerial = 123456
Buffer.UsbType = 8
Device = 123456
someExample(Device, Buffer)
的C代码:
typedef struct __Structure{
ULONG UsbSerial;
ULONG UsbType;
}__Structure, *__Structure;
#define DllExport __declspec(dllexport)
EXTERN_C
{
DllExport void someExample(HANDLE handleOfSomething, __Structure* Filter)
{
//
// Here we have
// Filter.UsbSerial = 123456
// Filter.UsbType = 0 <<<--- this is wrong! I sent 8.
/* ... */
}
}
它,当然,取决于所使用的编译器,但传统上是一个'long'用C是32位,但VB.NET中的“Long”是64位。改为使用'UInteger'和'UnManagedType.U4'。 – 2013-05-10 12:06:08
谢谢,工作! – lcssanches 2013-05-10 12:29:41
@StevenDoggart @因为修复了OP的问题,所以你应该让它成为答案,以便它可以被接受 – Mike 2013-05-10 12:32:14