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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Falko

    #1

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




    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\ErS hPlcDb.dll"
    Alias "_ShPlcDb_Rd_Da ta@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(L ayoutKind.Seque ntial)> 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.TypeLoad Exception
    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(L ayoutKind.Seque ntial)> 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 ***
  • TDC

    #2
    Re: Problems with structure in structure and DLL function call in VB.NET

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

    HTH,
    Tom

    Falko wrote:[color=blue]
    > 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\ErS hPlcDb.dll"
    > Alias "_ShPlcDb_Rd_Da ta@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(L ayoutKind.Seque ntial)> 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.TypeLoad Exception
    > 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(L ayoutKind.Seque ntial)> 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 ***[/color]

    Comment

    Working...