How to export c++ data Structure to vb.net?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
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:pre long ExchangeTime;<br/>
<span style="white-space:pre short MessageCode;<br/>
<span style="white-space:pre short Reserved1;<br/>
<span style="white-space:pre char Reserved2[9];<br/>
<span style="white-space:pre short MessageSize;<br/>
<span style="white-space:pre short Resderved3;<br/>
};<br/>
struct OrderByPrice //total size is 14 Byte<br/>
{<br/>
<span style="white-space:pre long Quantity;<br/>
<span style="white-space:pre long OrderPrice;<br/>
<span style="white-space:pre long TotalNumberOfOrders;<br/>
<span style="white-space:pre short Reserved;<br/>
};
struct MarketPicture // MessageCode = 31038<br/>
{<br/>
<span style="white-space:pre //MessageHeader Header;<br/>
<span style="white-space:pre BroadcastMessageHeader BroadcastMsg;<br/>
<span style="white-space:pre unsigned char IndicativePriceVolume;<br/>
<span style="white-space:pre long InstrumentIdentifier;<br/>
<span style="white-space:pre char Reserved1;<br/>
<span style="white-space:pre char IndexFlag;<br/>
<span style="white-space:pre long TotalQtyTraded;<br/>
<span style="white-space:pre long LastTradedPrice;<br/>
<span style="white-space:pre long LastTradedQty;<br/>
<span style="white-space:pre long LastTradedTime;<br/>
<span style="white-space:pre long AverageTradePrice;<br/>
<span style="white-space:pre OrderByPrice BestBuy[5];<br/>
<span style="white-space:pre OrderByPrice BestSell[5];<br/>
<span style="white-space:pre double TotalBuyQty;<br/>
<span style="white-space:pre double TotalSellQty;<br/>
<span style="white-space:pre char Reserved2[2];<br/>
<span style="white-space:pre long ClosePrice;<br/>
<span style="white-space:pre long OpenPrice;<br/>
<span style="white-space:pre long HighPrice;<br/>
<span style="white-space:pre long LowPrice;<br/>
<span style="white-space:pre short Reserved3;<br/>
<span style="white-space:pre long OI;<br/>
<span style="white-space:pre long TotalTrades;<br/>
<span style="white-space:pre long HighestPriceEver;<br/>
<span style="white-space:pre long LowestPriceever;<br/>
<span style="white-space:pre double TotalTradedVol;<br/>
};

The c++ code is working perfectly

Please help.....
<hr class="sig Shail

View the full article
 
Back
Top