EDN Admin
Well-known member
Hi,
I have some work related with data decompression and memory mapping.The decompression and memory mapping in c++ code is working fine . Now i want to convert the code in vb.net.
In vb.net i am able to decompress the data the moment i try to unmarshal the data to structure is gives error -
"Could not load type MARKETPIC from assembly McxRecv, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null because it contains an object field at offset 118 that is incorrectly aligned or overlapped by a non-object field."
Vb.net Structures
_________________
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=21, CharSet:=CharSet.Ansi, pack:=1)> _<br/>
Structure BROADCASTMessageHeader Message Header Total Length is 21<br/>
<br/>
<FieldOffset(0)> Dim ExchangeTime As Int32<br/>
<FieldOffset(4)> Dim MessageCode As Int16<br/>
<FieldOffset(6)> Dim Reserved1 As Int16<br/>
<FieldOffset(8)> Dim reservedT1 As Double<br/>
<FieldOffset(16)> Dim reservedT2 As Byte<br/>
<FieldOffset(17)> Dim MessageSize As Int16<br/>
<FieldOffset(19)> Dim Reserved3 As Int16<br/>
<br/>
End Structure
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=14, CharSet:=CharSet.Ansi, pack:=1)> _<br/>
Structure ORDERBYPRICE<br/>
<FieldOffset(0)> Dim qty As Int32<br/>
<FieldOffset(4)> Dim OrderPrice As Int32<br/>
<FieldOffset(8)> Dim TotalNoOFOrder As Int32<br/>
<FieldOffset(12)> Dim Reserved As Int16<br/>
End Structure
<br/>
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=248, CharSet:=CharSet.Ansi)> _<br/>
Private Structure MARKETPIC<br/>
<FieldOffset(0)> Dim BroadcastmsgHeader As BROADCASTMessageHeader 21<br/>
<FieldOffset(21)> Dim IndicativePriceVolume As Byte<br/>
<FieldOffset(22)> Dim InstrumentIdentifier As Int32<br/>
<FieldOffset(26)> Dim Reserved2 As Byte<br/>
<FieldOffset(27)> Dim IndexFlag As Byte<br/>
<FieldOffset(28)> Dim TotalQtyTraded As Int32<br/>
<FieldOffset(32)> Dim LastTradedPrice As Int32<br/>
<FieldOffset(36)> Dim LastTradedQty As Int32<br/>
<FieldOffset(40)> Dim LastTradTime As Int32<br/>
<FieldOffset(44)> Dim AvgTradePrice As Int32<br/>
<FieldOffset(48)> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst:=70)> Dim BuyOrderPrice() As ORDERBYPRICE<br/>
<FieldOffset(118)> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst:=70)> Dim SellOrderPrice() As ORDERBYPRICE<br/>
<br/>
<FieldOffset(188)> Dim TotalBuyQty As Double<br/>
<FieldOffset(196)> Dim TotalSellQty As Double<br/>
<FieldOffset(204)> Dim reserved As Int16<br/>
<FieldOffset(199)> <VBFixedString(2)> Dim reserved As String<br/>
<FieldOffset(206)> Dim ClosePrice As Int32<br/>
<FieldOffset(210)> Dim OPrice As Int32<br/>
<FieldOffset(214)> Dim High As Int32<br/>
<FieldOffset(218)> Dim Low As Int32<br/>
<FieldOffset(222)> Dim Reserved32 As Int16<br/>
<FieldOffset(224)> Dim OPENINTEREST As Int32<br/>
<FieldOffset(228)> Dim TotalTrades As Int32<br/>
<FieldOffset(232)> Dim HighestPriceEver As Int32<br/>
<FieldOffset(236)> Dim LowestPriceEver As Int32<br/>
<FieldOffset(240)> Dim TotalTradedValue As Double<br/>
<br/>
End Structure
The Code for memory mapping in vb.net
Public Shared Function RawDeserialize(ByVal rawdatas As Byte(), ByVal anytype As Type) As Object<br/>
Dim rawsize As Integer = Marshal.SizeOf(anytype)<br/>
If rawsize > rawdatas.Length Then<br/>
Return Nothing<br/>
End If<br/>
Dim buffer As IntPtr = Marshal.AllocHGlobal(rawsize)<br/>
Marshal.Copy(rawdatas, 0, buffer, rawsize)<br/>
Dim retobj As Object = Marshal.PtrToStructure(buffer, anytype)<br/>
Marshal.FreeHGlobal(buffer)<br/>
Return retobj<br/>
End Function<br/>
c++ Structure
________________
struct BroadcastMessageHeader // Total Size is 21<br/>
{<br/>
<span style="white-space
re long ExchangeTime;<br/>
<span style="white-space
re short MessageCode;<br/>
<span style="white-space
re short Reserved1;<br/>
<span style="white-space
re char Reserved2[9];<br/>
<span style="white-space
re short MessageSize;<br/>
<span style="white-space
re short Resderved3;<br/>
};<br/>
struct OrderByPrice //total size is 14 Byte<br/>
{<br/>
<span style="white-space
re long Quantity;<br/>
<span style="white-space
re long OrderPrice;<br/>
<span style="white-space
re long TotalNumberOfOrders;<br/>
<span style="white-space
re short Reserved;<br/>
};
struct MarketPicture // MessageCode = 31038<br/>
{<br/>
<span style="white-space
re //MessageHeader Header;<br/>
<span style="white-space
re BroadcastMessageHeader BroadcastMsg;<br/>
<span style="white-space
re unsigned char IndicativePriceVolume;<br/>
<span style="white-space
re long InstrumentIdentifier;<br/>
<span style="white-space
re char Reserved1;<br/>
<span style="white-space
re char IndexFlag;<br/>
<span style="white-space
re long TotalQtyTraded;<br/>
<span style="white-space
re long LastTradedPrice;<br/>
<span style="white-space
re long LastTradedQty;<br/>
<span style="white-space
re long LastTradedTime;<br/>
<span style="white-space
re long AverageTradePrice;<br/>
<span style="white-space
re OrderByPrice BestBuy[5];<br/>
<span style="white-space
re OrderByPrice BestSell[5];<br/>
<span style="white-space
re double TotalBuyQty;<br/>
<span style="white-space
re double TotalSellQty;<br/>
<span style="white-space
re char Reserved2[2];<br/>
<span style="white-space
re long ClosePrice;<br/>
<span style="white-space
re long OpenPrice;<br/>
<span style="white-space
re long HighPrice;<br/>
<span style="white-space
re long LowPrice;<br/>
<span style="white-space
re short Reserved3;<br/>
<span style="white-space
re long OI;<br/>
<span style="white-space
re long TotalTrades;<br/>
<span style="white-space
re long HighestPriceEver;<br/>
<span style="white-space
re long LowestPriceever;<br/>
<span style="white-space
re double TotalTradedVol;<br/>
};
The c++ code is working perfectly
Please help.....
<hr class="sig Shail
View the full article
I have some work related with data decompression and memory mapping.The decompression and memory mapping in c++ code is working fine . Now i want to convert the code in vb.net.
In vb.net i am able to decompress the data the moment i try to unmarshal the data to structure is gives error -
"Could not load type MARKETPIC from assembly McxRecv, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null because it contains an object field at offset 118 that is incorrectly aligned or overlapped by a non-object field."
Vb.net Structures
_________________
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=21, CharSet:=CharSet.Ansi, pack:=1)> _<br/>
Structure BROADCASTMessageHeader Message Header Total Length is 21<br/>
<br/>
<FieldOffset(0)> Dim ExchangeTime As Int32<br/>
<FieldOffset(4)> Dim MessageCode As Int16<br/>
<FieldOffset(6)> Dim Reserved1 As Int16<br/>
<FieldOffset(8)> Dim reservedT1 As Double<br/>
<FieldOffset(16)> Dim reservedT2 As Byte<br/>
<FieldOffset(17)> Dim MessageSize As Int16<br/>
<FieldOffset(19)> Dim Reserved3 As Int16<br/>
<br/>
End Structure
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=14, CharSet:=CharSet.Ansi, pack:=1)> _<br/>
Structure ORDERBYPRICE<br/>
<FieldOffset(0)> Dim qty As Int32<br/>
<FieldOffset(4)> Dim OrderPrice As Int32<br/>
<FieldOffset(8)> Dim TotalNoOFOrder As Int32<br/>
<FieldOffset(12)> Dim Reserved As Int16<br/>
End Structure
<br/>
<StructLayout(Runtime.InteropServices.LayoutKind.Explicit, Size:=248, CharSet:=CharSet.Ansi)> _<br/>
Private Structure MARKETPIC<br/>
<FieldOffset(0)> Dim BroadcastmsgHeader As BROADCASTMessageHeader 21<br/>
<FieldOffset(21)> Dim IndicativePriceVolume As Byte<br/>
<FieldOffset(22)> Dim InstrumentIdentifier As Int32<br/>
<FieldOffset(26)> Dim Reserved2 As Byte<br/>
<FieldOffset(27)> Dim IndexFlag As Byte<br/>
<FieldOffset(28)> Dim TotalQtyTraded As Int32<br/>
<FieldOffset(32)> Dim LastTradedPrice As Int32<br/>
<FieldOffset(36)> Dim LastTradedQty As Int32<br/>
<FieldOffset(40)> Dim LastTradTime As Int32<br/>
<FieldOffset(44)> Dim AvgTradePrice As Int32<br/>
<FieldOffset(48)> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst:=70)> Dim BuyOrderPrice() As ORDERBYPRICE<br/>
<FieldOffset(118)> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst:=70)> Dim SellOrderPrice() As ORDERBYPRICE<br/>
<br/>
<FieldOffset(188)> Dim TotalBuyQty As Double<br/>
<FieldOffset(196)> Dim TotalSellQty As Double<br/>
<FieldOffset(204)> Dim reserved As Int16<br/>
<FieldOffset(199)> <VBFixedString(2)> Dim reserved As String<br/>
<FieldOffset(206)> Dim ClosePrice As Int32<br/>
<FieldOffset(210)> Dim OPrice As Int32<br/>
<FieldOffset(214)> Dim High As Int32<br/>
<FieldOffset(218)> Dim Low As Int32<br/>
<FieldOffset(222)> Dim Reserved32 As Int16<br/>
<FieldOffset(224)> Dim OPENINTEREST As Int32<br/>
<FieldOffset(228)> Dim TotalTrades As Int32<br/>
<FieldOffset(232)> Dim HighestPriceEver As Int32<br/>
<FieldOffset(236)> Dim LowestPriceEver As Int32<br/>
<FieldOffset(240)> Dim TotalTradedValue As Double<br/>
<br/>
End Structure
The Code for memory mapping in vb.net
Public Shared Function RawDeserialize(ByVal rawdatas As Byte(), ByVal anytype As Type) As Object<br/>
Dim rawsize As Integer = Marshal.SizeOf(anytype)<br/>
If rawsize > rawdatas.Length Then<br/>
Return Nothing<br/>
End If<br/>
Dim buffer As IntPtr = Marshal.AllocHGlobal(rawsize)<br/>
Marshal.Copy(rawdatas, 0, buffer, rawsize)<br/>
Dim retobj As Object = Marshal.PtrToStructure(buffer, anytype)<br/>
Marshal.FreeHGlobal(buffer)<br/>
Return retobj<br/>
End Function<br/>
c++ Structure
________________
struct BroadcastMessageHeader // Total Size is 21<br/>
{<br/>
<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

};<br/>
struct OrderByPrice //total size is 14 Byte<br/>
{<br/>
<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

};
struct MarketPicture // MessageCode = 31038<br/>
{<br/>
<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

<span style="white-space

};
The c++ code is working perfectly
Please help.....
<hr class="sig Shail
View the full article