passing a struct pointer containing arrays to/from C

Messias80

New member
Joined
Sep 23, 2014
Messages
3
Programming Experience
Beginner
Im having some problems due to vb.net not being able to accept static arrays inside a structure.
my vb.net application is talking to a DLL coded in C. the DLL is calling a function inside my vb.net application and it is passing a pointer of a structure.

PHP:
typedef struct {
  uint8_t index;
  uint8_t value;
  uint8_t leds[4];
  struct {
    int8_t x;
    int8_t y;
    int8_t z;
  } input[10];
} REPORT;

i have declared the structure in vb.net as

PHP:
<StructLayout(LayoutKind.Sequential)> _
Public Structure INPUTS
  Public x As SByte
  Public y As SByte
  Public z as SByte
End Structure

<StructLayout(LayoutKind.Sequential)> _
Public Structure REPORT
  Public index as Byte
  Public value As Byte
  Public leds() As Byte
  Public input() As INPUTS

  Sub New(numleds As Integer, numinputs As Integer)
    ReDim Me.leds(numleds)
    ReDim Me.input(numinputs)
  End Sub
End Structure

the vb.net delegate and function are declared like this.
PHP:
Public Delegate Function Del_Read(ByRef localReport As REPORT) As Byte
PHP:
Public Function Read(ByRef localReport As REPORT) As Byte

the DLL in C calls the vb.net Read function correctly and passes a REPORT pointer to vb.net
the problem comes when i try to work with localReport
only localReport.index and localReport.value show the data.

localReport.leds and localReport.input are Nothing (vs express debugger shows Nothing for those arrays) because i think they where not initialized.

i have tried to use also Marshal.PtrToStructure and same problem, there is no way to tell vb.net hey!! i want to redim the arrays first!!!

the only way to redim the arrays is
PHP:
dim extraReport as new Report(3, 9)
but then how to copy the values from the localReport to the extraReport?
anyone know how to do this?

it would be easier if i could do the following but that is not allowed in vb.net
PHP:
<StructLayout(LayoutKind.Sequential)> _
Public Structure REPORT
  Public index as Byte
  Public value As Byte
  Public leds(3) As Byte
  Public input(9) As INPUTS
End Structure

vb.net application will modify the values, so the DLL in C can do its job with the new values.
thank you.
 
i already fix it. i had to declare that structure field using MarshalAs.

PHP:
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure REPORT
        Public index As Byte
        Public value As Byte

        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=4)>
        Public leds() As Byte

        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)>
        Public input() As INPUTS

        Sub New(numleds As Integer, numinputs As Integer)
            ReDim Me.leds(numleds)
            ReDim Me.input(numinputs)
        End Sub
    End Structure
 

Latest posts

Back
Top