Marshalling for C struct with arrays

  • Thread starter Thread starter Markus Stiller
  • Start date Start date
M

Markus Stiller

Could someone give me an hint?

I have trouble with mashaling of a struct. I can set values in struct
(byVAL) but cant read from the struct. Its an long pointer struct in a
c dll.

thats my struct where is correct i guess

'-----------------------------------
' structure declarations
' xl event
<StructLayout(LayoutKind.Sequential, Pack:=1)> Structure s_xlEvent
Public tag As Byte
Public chanIndex As Byte
Public transID As Short
Public porthandle As Short
Public reserved As Short
Public timestamp As UInt64
<FieldOffset(0)> Public tagdata_can_msg As s_xl_can_msg
<FieldOffset(0)> Public tagdata_chipstate As s_xl_chipstate
<FieldOffset(0)> Public tagdata_lin_msg As s_xl_lin_msg_api
<FieldOffset(0)> Public tagdata_sync_pulse As s_xl_sync_pulse
<FieldOffset(0)> Public tagdata_daio_data As s_xl_daio_data
<FieldOffset(0)> Public tagdata_transceiver As
s_xl_transceiver
End Structure


Structure s_xl_can_msg
Public id As UInt32
Public flags As Short
Public dlc As Short
Public res1 As UInt64
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Public
data As Byte()
Public res2 As UInt64
End Structure


' function to fill/set struct with array - this is successful

Public Declare Function xlCanTransmit Lib "vxlapi.dll" (ByVal
PortHandle As UInt32, ByVal accessmask As UInt64, ByRef messageCount
As UInt16, <MarshalAs(UnmanagedType.LPArray)> ByVal pMessages As
s_xlEvent()) As Byte

' function to read frome struct with array - this is the problem

Public Declare Function xlReceive Lib "vxlapi.dll" (ByVal PortHandle
As UInt32, ByRef pEventCount As UInt16,
<MarshalAs(UnmanagedType.LPArray)> ByRef pMessages As s_xlEvent()) As
Byte

Problem SizeParamIndex is not allowed to with byref parameter :(
Any suggestions?
 
Markus,

What does the original native function signature look like? Are you
sure that pMessages should be ByRef in the xlReceive function?



Mattias
 
the original function is that. The driver dll I ask is an c dll and
will give me a pointer back to this structure. I saw that the call is
successful and values are returned in the debug messages of dll.

#ifdef DYNAMIC_XLDRIVER_DLL
typedef XLstatus (_EXPORT_API *XLRECEIVE) (
XLportHandle portHandle,
unsigned int *pEventCount,
XLevent *pEvents);
#else
XLstatus _EXPORT_DECL xlReceive(
XLportHandle portHandle,
unsigned int *pEventCount,
XLevent *pEvents);
#endif

Markus
 
Markus,

Looks like the array should be passed ByVal after all. Try this

Public Declare Function xlReceive Lib "vxlapi.dll" (ByVal PortHandle
As UInt32, ByRef pEventCount As UInt16, <Out> ByVal pMessages As
s_xlEvent()) As Byte



Mattias
 
Back
Top