-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinAPI.cs
More file actions
151 lines (125 loc) · 5.42 KB
/
Copy pathWinAPI.cs
File metadata and controls
151 lines (125 loc) · 5.42 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Runtime.InteropServices;
namespace Utils
{
public static class WinAPI
{
//https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
public const int AF_BTH = 32; // Bluetooth Address Family
public const int SOCK_STREAM = 1; // Stream socket
public const int IPPROTO_GGP = 3; // Protocol placeholder
//Address Family, Socket Type, Protocol
[DllImport("Ws2_32.dll", SetLastError = true)]
public static extern int socket(int af, int type, int protocol);
[DllImport("Ws2_32.dll")]
public static extern int connect(int s, [MarshalAs(UnmanagedType.LPArray)] byte[] name, int namelen);
[DllImport("ws2_32.dll")]
public static extern int getpeername(int s, byte[] addr, ref int addrlen);
[DllImport("ws2_32.dll")]
public static extern int send(int s, byte[] buf, int len, int flags);
[DllImport("ws2_32.dll")]
public static extern int closesocket(int s);
//---------------------------------------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
private ushort year;
private short month;
private short dayOfWeek;
private short day;
private short hour;
private short minute;
private short second;
private short millisecond;
public static SYSTEMTIME FromByteArray(byte[] array, int offset)
{
SYSTEMTIME st = new SYSTEMTIME
{
year = BitConverter.ToUInt16(array, offset),
month = BitConverter.ToInt16(array, offset + 2),
day = BitConverter.ToInt16(array, offset + 6),
hour = BitConverter.ToInt16(array, offset + 8),
minute = BitConverter.ToInt16(array, offset + 10),
second = BitConverter.ToInt16(array, offset + 12)
};
return st;
}
public static SYSTEMTIME FromDateTime(DateTime dt)
{
SYSTEMTIME st = new SYSTEMTIME
{
year = (ushort)dt.Year,
month = (short)dt.Month,
dayOfWeek = (short)dt.DayOfWeek,
day = (short)dt.Day,
hour = (short)dt.Hour,
minute = (short)dt.Minute,
second = (short)dt.Second,
millisecond = (short)dt.Millisecond
};
return st;
}
public DateTime ToDateTime(DateTimeKind kind)
{
if (year == 0 && month == 0 && day == 0 && hour == 0 && minute == 0 && second == 0)
{
return DateTime.MinValue;
}
return new DateTime(year, month, day, hour, minute, second, millisecond, kind);
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLUETOOTH_DEVICE_INFO
{
internal int dwSize;
internal ulong Address;
internal uint ulClassofDevice;
[MarshalAs(UnmanagedType.Bool)]
internal bool fConnected;
[MarshalAs(UnmanagedType.Bool)]
internal bool fRemembered;
[MarshalAs(UnmanagedType.Bool)]
internal bool fAuthenticated;
internal SYSTEMTIME stLastSeen;
internal SYSTEMTIME stLastUsed;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248)]
internal string szName;
public static BLUETOOTH_DEVICE_INFO Create()
{
BLUETOOTH_DEVICE_INFO info = new BLUETOOTH_DEVICE_INFO();
info.dwSize = Marshal.SizeOf(info);
return info;
}
public BLUETOOTH_DEVICE_INFO(ulong address)
{
dwSize = 560;
Address = address;
ulClassofDevice = 0;
fConnected = false;
fRemembered = false;
fAuthenticated = false;
stLastSeen = new SYSTEMTIME();
stLastUsed = new SYSTEMTIME();
szName = "";
}
}
[DllImport("BluetoothAPIs.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int BluetoothGetDeviceInfo(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO deviceInfo);
[StructLayout(LayoutKind.Sequential)]
public struct WSADATA
{
public short wVersion;
public short wHighVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
public string szDescription;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
public string szSystemStatus;
public short iMaxSockets;
public short iMaxUdpDg;
public IntPtr lpVendorInfo;
}
[DllImport("Ws2_32.dll", SetLastError = true)]
public static extern int WSAStartup(ushort wVersionRequested, out WSADATA lpWSAData);
//---------------------------------------------------------------------------------------
}
}