none
Marshaling in VB .Net (VS2008) with an array RRS feed

  • General discussion

  • Hi,
    I hope that my problem is not a realy problem:

    I will make an arry with 999 Elemnts from this Structure:

        <StructLayout(LayoutKind.Sequential)> Public Structure KundenSatz
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public KDNR As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Nachname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Vorname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Strasse As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public PLZ As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Ort As String
        End Structure

    What is the right way for this?

    Bernd
    • Changed type nobugzModerator Friday, October 2, 2009 9:11 AM won't describe problem
    Friday, October 2, 2009 8:02 AM

All replies

  • You didn't explain the problem.

    Hans Passant.
    Friday, October 2, 2009 8:39 AM
    Moderator
  • I will have an Arra with this elements (999 times)

            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public KDNR As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Nachname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Vorname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Strasse As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public PLZ As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Ort As String
    Friday, October 2, 2009 8:55 AM
  • Hello Bernd

    Are you asking how to make an array of KundenSatz with 999 elements?

    Is this what you are looking for:

    Dim sport(998) As KundenSatz

    Note that I write 998 instead of 999 in the code. All arrays in VB as zero based, meaning, the index of the first element is zero and they are numbered sequentially. You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the numder that specifies the index of the last element of the array.

    Next, you can access the elements in the array like this:

    sport(0).XXXX... -- this refers to the first element in the array
    sport(998).XXXX. -- this refers to the last element in the array

    Please let me know whether I understand your question rightly.

    Regards,
    Jialiang Ge

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, October 2, 2009 9:47 AM
    Moderator
  • I will test it shortly but it looks like good : - )))
    Friday, October 2, 2009 10:01 AM
  • You mean this?

        <StructLayout(LayoutKind.Sequential)> Public Structure KundenSatz
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public KDNR As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Nachname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Vorname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Strasse As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public PLZ As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Ort As String
        End Structure

        Dim KundenTabelle(998) As KundenSatz

        Declare Function TERMINELESEN Lib "Mydll.dll" (ByRef TerminTabelle As TerminTabelle) As Integer

    But here is a problem:
    The Visual Studio is not Accept "(ByRef TerminTabelle As TerminTabelle)"
    If i have a plant structur (only one element) then it works, now I need an arry...

    That is the Code for one Element (not an array)

        <StructLayout(LayoutKind.Sequential)> Public Structure KundenSatz
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public KDNR As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Nachname As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Vorname As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Strasse As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public PLZ As String
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Ort As String
        End Structure
        Declare Function KUNDENLESEN Lib "coboldll.dll" (ByRef KundenSatz As KundenSatz) As Integer



    Bernd

    Friday, October 2, 2009 12:15 PM
  • Her one more Sample for tahat what I will do...

    <StructLayout(LayoutKind.Sequential)>

    Public Structure TerminSatz


    ' 0:00 - 0:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT00 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT00 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT00 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT00 As String '2000 Zeichen ' 0:30 - 1:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT05 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT05 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT05 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT05 As String '2000 Zeichen ' 1:00 - 1:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT10 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT10 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT10 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT10 As String '2000 Zeichen ' 1:30 - 2:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT15 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT15 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT15 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT15 As String '2000 Zeichen ' 2:00 - 2:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT20 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT20 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT20 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT20 As String '2000 Zeichen ' 2:30 - 3:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT25 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT25 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT25 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT25 As String '2000 Zeichen ' 3:00 - 3:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT30 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT30 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT30 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT30 As String '2000 Zeichen ' 3:30 - 4:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT35 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT35 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT35 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT35 As String '2000 Zeichen ' 4:00 - 4:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT40 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT40 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT40 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT40 As String '2000 Zeichen ' 4:30 - 5:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT45 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT45 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT45 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT45 As String '2000 Zeichen ' 5:00 - 5:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT50 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT50 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT50 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT50 As String '2000 Zeichen ' 5:30 - 6:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT55 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT55 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT55 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT55 As String '2000 Zeichen ' 6:00 - 6:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT60 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT60 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT60 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT60 As String '2000 Zeichen ' 6:30 - 7:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT65 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT65 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT65 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT650 As String '2000 Zeichen ' 7:00 - 7:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT70 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT70 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT70 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT70 As String '2000 Zeichen ' 7:30 - 8:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT75 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT75 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT75 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT75 As String '2000 Zeichen ' 8:00 - 8:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT80 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT80 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT80 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT80 As String '2000 Zeichen ' 8:30 - 9:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT85 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT85 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT85 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT85 As String '2000 Zeichen ' 9:00 - 9:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT90 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT90 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT90 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT90 As String '2000 Zeichen ' 9:30 - 10:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT95 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT95 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT95 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT95 As String '2000 Zeichen ' 10:00 - 10:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT100 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT100 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT100 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT100 As String '2000 Zeichen ' 10:30 - 11:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT105 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT105 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT105 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT105 As String '2000 Zeichen ' 11:00 - 11:30 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT110 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT110 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT110 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT110 As String '2000 Zeichen ' 11:30 - 12:00 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT115 As String '10 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT115 As String '8 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT115 As String '80 Zeichen <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=2001)> Public COBTextOUT115 As String '2000 Zeichen

     

    End Structure


    I think there must be a way for bring the Marshal in a Array...

    What I found out now is:

    <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.ByValTStr, SizeConst:=10)> Public COBText As String

    This is an Text Arry with 10 Text Elements BUT I neet to Say How Long is the Text Element in the String!

    The "SizeConst" is "Normal" for the Size of the Field but in an Array the "SizeConst" is the "Occurs" for the Array

    Have any an Idea?




    Friday, October 2, 2009 5:11 PM
  •     <StructLayout(LayoutKind.Sequential)> Public Structure KundenSatz
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public KDNR As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Nachname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Vorname As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Strasse As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=6)> Public PLZ As String 
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=30)> Public Ort As String
        End Structure

        Dim KundenTabelle(998) As KundenSatz

        Declare Function TERMINELESEN Lib "Mydll.dll" (ByRef TerminTabelle As TerminTabelle) As Integer

    But here is a problem:
    The Visual Studio is not Accept "(ByRef TerminTabelle As TerminTabelle)"
    If i have a plant structur (only one element) then it works, now I need an arry...

    Could you declare the function in VB.NET as:

    Declare Function TERMINELESEN Lib "Mydll.dll" (ByVal TerminTabelle() As TerminTabelle) As Integer

    ?

    Regards,
    Jialiang Ge

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 6, 2009 6:20 AM
    Moderator
  • If I type:

    Declare Function TERMINELESEN Lib "Mydll.dll" (ByVal TerminTabelle() As TerminTabelle) As Integer

    I become the Error "Type TerminTabelle not declare"...

    Bernd
    Tuesday, October 6, 2009 9:12 AM
  • Have nobody an Idea ???
    Wednesday, October 7, 2009 12:13 PM
  • Declare Function TERMINELESEN Lib "Mydll.dll" (ByVal Mumble() As KundenSatz) As Integer
    Hans Passant.
    Wednesday, October 7, 2009 12:34 PM
    Moderator
  • you mean:
     ...
    <StructLayout(LayoutKind.Sequential)> Public Structure KundenSatz <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=11)> Public COBTerminDatumOUT0000 As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=9)> Public COBTerminZeitOUT0000 As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=81)> Public COBUerberschriftOUT0000 As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=257)> Public COBTextOUT00 As String End Structure

    Dim Mumble(48) As TerminSatz Declare Function TERMINELESEN Lib "My.dll" (ByVal Mumble() As KundenSatz) As Integer
    ...
    Then i have an array with

    COBTerminDatumOUT0000
    COBTerminZeitOUT0000
    COBUerberschriftOUT0000
    COBTextOUT00 

    48 times...!?

    Best Regards

    Bernd
    Friday, October 9, 2009 9:05 PM