Hello everyone.
I'm trying to use CopyMemory API in VB.NET. I have a
module with struct defined like this:
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure tSortKey
<VBFixedString( 6),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=6)> Public CorpNum As Char()
<VBFixedString( 15),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=15)> Public GlobalID As Char()
<VBFixedString( 3),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=3)> Public RecType As Char()
<VBFixedString( 6),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=6)> Public GPNumber As Char()
<VBFixedString( 10),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=10)> Public GrantNumber As Char()
<VBFixedString( 5),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=5)> Public VestOrder As Char()
<VBFixedString( 10),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=10)> Public ExerNum As Char()
<VBFixedString( 4),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=4)> Public Version As Char()
End Structure
<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure tDataStruct
Dim tSort As tSortKey
<VBFixedString( 1141),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
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.AllocHG lobal(Marshal.S izeOf
(tRecord.GetTyp e))
Marshal.Structu reToPtr(tRecord , p_objTarget, True)
Call CopyMemory(p_ob jTarget, sRecordString,
Marshal.SizeOf( sRecordString))
Marshal.PtrToSt ructure(p_objTa rget, tRecord)
Marshal.FreeHGl obal(p_objTarge t)
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
I'm trying to use CopyMemory API in VB.NET. I have a
module with struct defined like this:
<StructLayout(L ayoutKind.Seque ntial)> _
Public Structure tSortKey
<VBFixedString( 6),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=6)> Public CorpNum As Char()
<VBFixedString( 15),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=15)> Public GlobalID As Char()
<VBFixedString( 3),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=3)> Public RecType As Char()
<VBFixedString( 6),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=6)> Public GPNumber As Char()
<VBFixedString( 10),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=10)> Public GrantNumber As Char()
<VBFixedString( 5),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=5)> Public VestOrder As Char()
<VBFixedString( 10),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=10)> Public ExerNum As Char()
<VBFixedString( 4),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
SizeConst:=4)> Public Version As Char()
End Structure
<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure tDataStruct
Dim tSort As tSortKey
<VBFixedString( 1141),
System.Runtime. InteropServices .MarshalAs
(System.Runtime .InteropService s.UnmanagedType .ByValTStr,
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.AllocHG lobal(Marshal.S izeOf
(tRecord.GetTyp e))
Marshal.Structu reToPtr(tRecord , p_objTarget, True)
Call CopyMemory(p_ob jTarget, sRecordString,
Marshal.SizeOf( sRecordString))
Marshal.PtrToSt ructure(p_objTa rget, tRecord)
Marshal.FreeHGl obal(p_objTarge t)
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