Problems with structure in structure and DLL function call in VB.NET

  • Thread starter Thread starter Falko
  • Start date Start date
F

Falko

Hi there,

I am currently translating a VB 6.0 application to .NET and have the
following problem:

The data structure I need to pass to a DLL function call has a structure
variable inside its structure:

Private Structure CstData_type
Dim Cst_AZ As DbLong
Dim Cst_DECKS As DbLong
Dim Cst_LUZ As DbSingle
Dim Cst_ZoneLen As ZoneSingleArray
End Structure

where

Private Structure ZoneSingleArray
Dim ZoneArr(20) As DbSingle
End Structure

The Function call of the DLL is declared as

Private Declare Function ShPlcDb_Rd_Data
Lib "D:\ShPLCDb\ErShPlcDb.dll"
Alias "_ShPlcDb_Rd_Data@24"
(ByVal nSubSysNode As Integer,
ByVal nDataType As Integer,
ByVal nDataIdx As Integer,
ByVal nDataCnt As Integer,
ByRef pData As "Any",
ByVal nDataLen As Integer) As Integer

Note the 5th parameter in the function call (pData) is declared
originally as Any (VB6.0).

Now, if I use the directive <StructLayout(LayoutKind.Sequential)> to
declare the struct and change the type of the 5th parameter in the
function call from Any to CstData_type, the function call works fine up
until the last variable in the struct Cst_ZoneLen. If I leave it out, it
works. If I put the last variable in the declaration, I get the error
message:

System.TypeLoadException
Additional information: Can not marshal field Cst_ZoneLen of type
CstData_type: The type definition of this field has no layout
information.

I have tried to declare the Structure ZoneSingleArray with
<StructLayout(LayoutKind.Sequential)> as well, but that didn't do the
trick. I get the same error.

Question: How should I declare the data structure so it works with my
DLL?

Looking forward to any suggestions!

Falko
 
You need to use the MarshAs attribute and utilize the SizeConst option
to create a fixed-length array.

HTH,
Tom
 
Back
Top