Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Marshalling Data, Can Anyone Help?

Marshalling Data, Can Anyone Help?

Scheduled Pinned Locked Moved Visual Basic
helpcsharpc++data-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrindAZ
    wrote on last edited by
    #1

    I need to marshal a structure so that I can pass it to a C++ DLL and vice versa. The following works correctly: _ Private Structure PLCDATA Dim w1 As Int32 Dim w2 As Int32 Dim w3 As Int32 Dim b1 As Boolean Dim b2 As Boolean Dim Arr() As Int32 Sub initialize() ReDim Arr(4) End Sub End Structure Now I need to have another item in the structure be an array of a different structure. Hope that makes since. I thought this would work but it gives me an error: _ Private Structure PARCEL Dim Induct As Int32 Dim Dest As Int32 Dim Reason As Int32 End Structure _ Private Structure PLCDATA Dim w1 As Int32 Dim w2 As Int32 Dim w3 As Int32 Dim b1 As Boolean Dim b2 As Boolean Dim Arr() As Int32 Dim Items() As PARCEL Sub initialize() ReDim Arr(4) ReDim Items(2) End Sub End Structure The error I get looks like this: System.TypeLoadException: Can not marshal field Items of type PLCDATA: This type can not be marshaled as a structure field. at Test_PLCData.Form1.ReadFromPLC(PLCDATA& pRead) at Test_PLCData.Form1.Button1_Click(Object sender, EventArgs e) in C:\Esanti Code\C++\PLC_Data DLL\VB.NET Test App\Test PLCData\Test PLCData\Form1.vb:line 106 Can anyone tell me how to get this to work correctly? Thanks in advance!!!

    N 1 Reply Last reply
    0
    • G GrindAZ

      I need to marshal a structure so that I can pass it to a C++ DLL and vice versa. The following works correctly: _ Private Structure PLCDATA Dim w1 As Int32 Dim w2 As Int32 Dim w3 As Int32 Dim b1 As Boolean Dim b2 As Boolean Dim Arr() As Int32 Sub initialize() ReDim Arr(4) End Sub End Structure Now I need to have another item in the structure be an array of a different structure. Hope that makes since. I thought this would work but it gives me an error: _ Private Structure PARCEL Dim Induct As Int32 Dim Dest As Int32 Dim Reason As Int32 End Structure _ Private Structure PLCDATA Dim w1 As Int32 Dim w2 As Int32 Dim w3 As Int32 Dim b1 As Boolean Dim b2 As Boolean Dim Arr() As Int32 Dim Items() As PARCEL Sub initialize() ReDim Arr(4) ReDim Items(2) End Sub End Structure The error I get looks like this: System.TypeLoadException: Can not marshal field Items of type PLCDATA: This type can not be marshaled as a structure field. at Test_PLCData.Form1.ReadFromPLC(PLCDATA& pRead) at Test_PLCData.Form1.Button1_Click(Object sender, EventArgs e) in C:\Esanti Code\C++\PLC_Data DLL\VB.NET Test App\Test PLCData\Test PLCData\Form1.vb:line 106 Can anyone tell me how to get this to work correctly? Thanks in advance!!!

      N Offline
      N Offline
      NetPointerIN
      wrote on last edited by
      #2

      I m writing the C# version up here. try with similar in yor code. // The Person structure [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct Person { public string firstName; public string lastName; } // The Bank account structure. Note the use of an IntPtr // to hold the pointer to the account holder [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct Account { public IntPtr accountHolder; //I m using Intptr not Person. public string accountName; public long accountNumber; public double balance; }

      G 1 Reply Last reply
      0
      • N NetPointerIN

        I m writing the C# version up here. try with similar in yor code. // The Person structure [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct Person { public string firstName; public string lastName; } // The Bank account structure. Note the use of an IntPtr // to hold the pointer to the account holder [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] public struct Account { public IntPtr accountHolder; //I m using Intptr not Person. public string accountName; public long accountNumber; public double balance; }

        G Offline
        G Offline
        GrindAZ
        wrote on last edited by
        #3

        If you wanted a fixed array size of the Person structure how would you do it?

        N 1 Reply Last reply
        0
        • G GrindAZ

          If you wanted a fixed array size of the Person structure how would you do it?

          N Offline
          N Offline
          NetPointerIN
          wrote on last edited by
          #4

          [StructLayout(LayoutKind.Sequential)] public struct s1 { [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)] public long[] vals; } Is this u want???

          G 1 Reply Last reply
          0
          • N NetPointerIN

            [StructLayout(LayoutKind.Sequential)] public struct s1 { [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)] public long[] vals; } Is this u want???

            G Offline
            G Offline
            GrindAZ
            wrote on last edited by
            #5

            No, I need an array of a structure inside another structure. StructLayout(LayoutKind.Sequential)] public struct s1 { [MarshalAs(UnmanagedType.I4)] public int val; } StructLayout(LayoutKind.Sequential)] public struct s2 { [MarshalAs(UnmanagedType.Struct, SizeConst=16)] public s1[] vals; } Something like that. Take a look at my original post.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups