By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,241 Members | 2,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,241 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

*** Sent via Developersdex http://www.developersdex.com ***
Jun 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
TDC
You need to use the MarshAs attribute and utilize the SizeConst option
to create a fixed-length array.

HTH,
Tom

Falko wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***


Jun 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.