我試圖將一個結構從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