Hello everyone.
I'm trying to use CopyMemory API in VB.NET. I have a
module with struct defined like this:
<StructLayout(LayoutKind.Sequential)> _
Public Structure tSortKey
<VBFixedString(6),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=6)> Public CorpNum As Char()
<VBFixedString(15),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=15)> Public GlobalID As Char()
<VBFixedString(3),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=3)> Public RecType As Char()
<VBFixedString(6),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=6)> Public GPNumber As Char()
<VBFixedString(10),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=10)> Public GrantNumber As Char()
<VBFixedString(5),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=5)> Public VestOrder As Char()
<VBFixedString(10),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=10)> Public ExerNum As Char()
<VBFixedString(4),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=4)> Public Version As Char()
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure tDataStruct
Dim tSort As tSortKey
<VBFixedString(1141),
System.Runtime.InteropServices.MarshalAs
(System.Runtime.InteropServices.UnmanagedType.ByVa lTStr,
SizeConst:=1141)> Public sData As Char()
End Structure
Then I'm trying to use this code:
Dim p_objTarget As New IntPtr
p_objTarget = Marshal.AllocHGlobal(Marshal.SizeOf
(tRecord.GetType))
Marshal.StructureToPtr(tRecord, p_objTarget, True)
Call CopyMemory(p_objTarget, sRecordString,
Marshal.SizeOf(sRecordString))
Marshal.PtrToStructure(p_objTarget, tRecord)
Marshal.FreeHGlobal(p_objTarget)
I'm getting the following error:
Type tDataStruct can not be marshaled as an unmanaged
structure; no meaningful size or offset can be computed.
Please, anyone let me know what I'm doing wrong.
Thank you. YK