pass complex structure to native function

  • Thread starter Thread starter Ivan
  • Start date Start date
I

Ivan

Hi,

I have a problem with passing complex structures to native code.
complex structures means that I have structure with Integers, Strings, Byte
Arrays, and also Arrays of another structures that also contains Strings,
Byte Arrays etc.

Any ideas how should I implement this?
I tried to use IntPrt, but I beleive this is not acceptable due to using of
dynamic arrays in the structure.

Note that I also need this structure to be passed ByRef and get data.


Thanks in advance,
Ivan.
 
I have tried to use AdvansedMarshaller from OpenNETCF and it did not work.

Actually - native OpenNETCF.Runtime.InteropServices.AdvansedMarshaller
crashed.
I have converted the code to VB.NET I've found that that crash was on String
type handling in GetFieldSize method (index out of bounds). I have added
error handlers there and looks that it started to work, but when I pass
bytes array to the function - it returned array with no changes. Byt this
param is byref and should return filled structure.

Below are some pieces of simplified code I used:

'----------
<DllImport("lib.dll", EntryPoint:="Input", _

CallingConvention:=CallingConvention.Winapi, SetLastError:=True)> _

Private Shared Function InputInt(ByVal filename As String, _

ByVal param() As Byte) As Integer

End Function

'----------
Public Class Struct1

Inherits CustomMarshaler 'AdvancedMarshaler '

<CustomMarshalAs(SizeConst:=ArrSize)> _

Public extDataSignature() As Byte ' Four byte character signature

Public dataSize As Integer ' Size of extended data in bytes

Public extData() As Byte ' uninterpreted bytes of data

Private Const ArrSize As Integer = 4

End Class

'----------
Public Class Struct2

Inherits CustomMarshaler 'AdvancedMarshaler '

Public struct_size As Integer ' size of the data structure passed in bytes
must be initialized

Public szAcross As Integer ' Grid size - No. of cells across

Public szDown As Integer ' Grid Size - No. of cells down



Public strTitle As String ' Puzzle Title



Public numExtData As Integer ' Number of extended data structures

Public extDataList() As Struct2 ' Pointer to array of extended data
structures

End Class

'----------
Public Shared Function Input(ByVal filename As String, _

ByRef param As Struct2) As Integer

Dim lRes As Integer

Try

ACROSS_InputPuzzle = ACROSS_InputPuzzleInt(filename,
param.ByteArray) 'pass and returns bytearray with zeros

param.Deserialize() 'crashes on bytearray from zeros only

Catch ex As Exception

Throw New Exception(ex.Message)

End Try



End Function
 
update:

when I added string size to structures as below, I made the Deserialize
method work, byt it did not helped - I have same structure (empty) as
passed. I even tried to pass that params as ByRef, but no luck...

maybe you will find some problems in my code?

------
Public Class Struct1

Inherits CustomMarshaler 'AdvancedMarshaler '

Public numClue As Integer

<CustomMarshalAs(SizeConst:=MAX_INTERFACE_NAME_LEN)> _

Public strClue As String

Private Const MAX_INTERFACE_NAME_LEN As Integer = 256

End Class
 
Back
Top