forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPInvokeUtility.cs
99 lines (86 loc) · 3.45 KB
/
PInvokeUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace QuantBox.XAPI
{
public class PInvokeUtility
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);
static Encoding encodingGB2312 = Encoding.GetEncoding(936);
public static string GetUnicodeString(byte[] str)
{
if(str == null)
{
return string.Empty;
}
int bytecount = 0;
foreach(byte b in str)
{
if (0 == b)
break;
++bytecount;
}
if (0 == bytecount)
return string.Empty;
//比TrimEnd('\0');快,减少了内存的复制
return encodingGB2312.GetString(str, 0, bytecount);
}
public static T GetObjectFromIntPtr<T>(IntPtr handler)
{
if (handler == IntPtr.Zero)
{
return default(T);
}
else
{
return (T)Marshal.PtrToStructure(handler, typeof(T));
}
}
public static DepthMarketDataNClass GetDepthMarketDataNClass(IntPtr ptr)
{
DepthMarketDataNField obj = (DepthMarketDataNField)Marshal.PtrToStructure(ptr, typeof(DepthMarketDataNField));
DepthMarketDataNClass cls = new DepthMarketDataNClass();
//obj.Size;
cls.TradingDay = obj.TradingDay;
cls.ActionDay = obj.ActionDay;
cls.UpdateTime = obj.UpdateTime;
cls.UpdateMillisec = obj.UpdateMillisec;
cls.Exchange = obj.Exchange;
cls.Symbol = obj.Symbol;
cls.InstrumentID = obj.InstrumentID;
cls.LastPrice = obj.LastPrice;
cls.Volume = obj.Volume;
cls.Turnover = obj.Turnover;
cls.OpenInterest = obj.OpenInterest;
cls.AveragePrice = obj.AveragePrice;
cls.OpenPrice = obj.OpenPrice;
cls.HighestPrice = obj.HighestPrice;
cls.LowestPrice = obj.LowestPrice;
cls.ClosePrice = obj.ClosePrice;
cls.SettlementPrice = obj.SettlementPrice;
cls.UpperLimitPrice = obj.UpperLimitPrice;
cls.LowerLimitPrice = obj.LowerLimitPrice;
cls.PreSettlementPrice = obj.PreSettlementPrice;
cls.PreOpenInterest = obj.PreOpenInterest;
//obj.BidCount;
int size = Marshal.SizeOf(typeof(DepthField));
IntPtr pBid = new IntPtr(ptr.ToInt64() + Marshal.SizeOf(typeof(DepthMarketDataNField)));
int AskCount = (obj.Size - Marshal.SizeOf(typeof(DepthMarketDataNField))) / size - obj.BidCount;
IntPtr pAsk = new IntPtr(ptr.ToInt64() + Marshal.SizeOf(typeof(DepthMarketDataNField)) + obj.BidCount * size);
cls.Bids = new DepthField[obj.BidCount];
cls.Asks = new DepthField[AskCount];
for (int i = 0; i < obj.BidCount; ++i)
{
cls.Bids[i] = (DepthField)Marshal.PtrToStructure(new IntPtr(pBid.ToInt64() + i * size), typeof(DepthField));
}
for (int i = 0; i < AskCount; ++i)
{
cls.Asks[i] = (DepthField)Marshal.PtrToStructure(new IntPtr(pAsk.ToInt64() + i * size), typeof(DepthField));
}
return cls;
}
}
}