Marshalling Data, Can Anyone Help?
-
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!!!
-
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!!!
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; }
-
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; }
-
[StructLayout(LayoutKind.Sequential)] public struct s1 { [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)] public long[] vals; } Is this u want???
-
[StructLayout(LayoutKind.Sequential)] public struct s1 { [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)] public long[] vals; } Is this u want???
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.