G
Guest
Does anyone know how to pass an array of structures to a DLL? Here's what
I'm doing... the VB.Net error is at the end.
*** C++ Structure Declaration:
typedef struct _SOME_STRUCT
{
char *pId;
char *pBuffer;
DWORD dwBufferLength;
DWORD dwBytesReturned;
DWORD dwStatus;
} SOME_STRUCT;
*** C++ Function Prototype:
SOME_API ULONG _stdcall GetData (char *pDevName, SOME_STRUCT *pSomeStruct,
unsigned long dwTotalStructs);
*** VB.NET Structure Declaration:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure TEST_SOME_STRUCT
<MarshalAs(UnmanagedType.LPStr)> Public URL As String
<MarshalAs(UnmanagedType.LPArray)> Public Buffer() As Byte
<MarshalAs(UnmanagedType.U4)> Public BufferLength As Int32
<MarshalAs(UnmanagedType.U4)> Public BytesReturned As Int32
<MarshalAs(UnmanagedType.U4)> Public Status As Int32
Public Sub New(ByVal URL As String, ByVal BufferLength As Integer)
Me.URL = URL
Me.BufferLength = BufferLength
ReDim Buffer(BufferLength - 1)
End Sub
End Structure
*** VB.NET DLL Prototype (declaration):
<DllImport("mtmcrapi.dll")> Protected Shared Function GetData(<[In]()> ByVal
DeviceName As String, <MarshalAs(UnmanagedType.LPArray), [In](), Out()> ByRef
Info() As TEST_SOME_STRUCT, <[In]()> ByVal StructCount As Int32) As Int32
End Function
*** VB.NET Code that produces the error:
Dim MyBuffers() As TEST_SOME_STRUCT
ReDim MyBuffers(1)
MyBuffers(0) = New TEST_SOME_STRUCT("anyUrl1", 1024)
MyBuffers(1) = New TEST_SOME_STRUCT("anyUrl2", 4096)
Dim rc as Int32 = GetData(mDeviceName, MyBuffers, 2)
*** VB.NET Error:
An unhandled exception of type 'System.TypeLoadException' occurred in
MyApp.exe
Additionaql information: Can not marshal field Buffer of type
TEST_SOME_STRUCT: This type can not be marshaled as a structure field.
I'm doing... the VB.Net error is at the end.
*** C++ Structure Declaration:
typedef struct _SOME_STRUCT
{
char *pId;
char *pBuffer;
DWORD dwBufferLength;
DWORD dwBytesReturned;
DWORD dwStatus;
} SOME_STRUCT;
*** C++ Function Prototype:
SOME_API ULONG _stdcall GetData (char *pDevName, SOME_STRUCT *pSomeStruct,
unsigned long dwTotalStructs);
*** VB.NET Structure Declaration:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure TEST_SOME_STRUCT
<MarshalAs(UnmanagedType.LPStr)> Public URL As String
<MarshalAs(UnmanagedType.LPArray)> Public Buffer() As Byte
<MarshalAs(UnmanagedType.U4)> Public BufferLength As Int32
<MarshalAs(UnmanagedType.U4)> Public BytesReturned As Int32
<MarshalAs(UnmanagedType.U4)> Public Status As Int32
Public Sub New(ByVal URL As String, ByVal BufferLength As Integer)
Me.URL = URL
Me.BufferLength = BufferLength
ReDim Buffer(BufferLength - 1)
End Sub
End Structure
*** VB.NET DLL Prototype (declaration):
<DllImport("mtmcrapi.dll")> Protected Shared Function GetData(<[In]()> ByVal
DeviceName As String, <MarshalAs(UnmanagedType.LPArray), [In](), Out()> ByRef
Info() As TEST_SOME_STRUCT, <[In]()> ByVal StructCount As Int32) As Int32
End Function
*** VB.NET Code that produces the error:
Dim MyBuffers() As TEST_SOME_STRUCT
ReDim MyBuffers(1)
MyBuffers(0) = New TEST_SOME_STRUCT("anyUrl1", 1024)
MyBuffers(1) = New TEST_SOME_STRUCT("anyUrl2", 4096)
Dim rc as Int32 = GetData(mDeviceName, MyBuffers, 2)
*** VB.NET Error:
An unhandled exception of type 'System.TypeLoadException' occurred in
MyApp.exe
Additionaql information: Can not marshal field Buffer of type
TEST_SOME_STRUCT: This type can not be marshaled as a structure field.