forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPInvokeUtility.cs
48 lines (44 loc) · 1.24 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
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));
}
}
}
}