-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
NativeMethods.cs
298 lines (241 loc) · 12.6 KB
/
NativeMethods.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Net.Bluetooth.Win32.NativeMethods
//
// Copyright (c) 2003-2021 In The Hand Ltd, All rights reserved.
// This source code is licensed under the MIT License
using System;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace InTheHand.Net.Bluetooth.Win32
{
internal static class NativeMethods
{
private const string bthpropsDll = "bthprops.cpl";
private const string wsDll = "ws2_32.dll";
internal const int NS_BTH = 16;
internal const int PROTOCOL_RFCOMM = 3;
internal const int BluetoothSocketAddressLength = 30;
internal const int BTH_PORT_ANY = -1;
private static bool? _isRunningOnMono;
public static bool IsRunningOnMono()
{
#if DEBUG
//return true;
#endif
if (!_isRunningOnMono.HasValue)
{
_isRunningOnMono = Type.GetType("Mono.Runtime") != null;
}
return _isRunningOnMono.Value;
}
// Authentication
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int BluetoothRegisterForAuthenticationEx(ref BLUETOOTH_DEVICE_INFO pbtdi, out IntPtr phRegHandle, BluetoothAuthenticationCallbackEx pfnCallback, IntPtr pvParam);
[return: MarshalAs(UnmanagedType.Bool)]
internal delegate bool BluetoothAuthenticationCallbackEx(IntPtr pvParam, ref BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothUnregisterAuthentication(IntPtr hRegHandle);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO pauthResponse);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__OOB_DATA_INFO pauthResponse);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern int BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO pauthResponse);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int BluetoothGetDeviceInfo(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int BluetoothAuthenticateDeviceEx(IntPtr hwndParentIn, IntPtr hRadioIn, ref BLUETOOTH_DEVICE_INFO pbtdiInout, byte[] pbtOobData, BluetoothAuthenticationRequirements authenticationRequirement);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern int BluetoothRemoveDevice(ref ulong pAddress);
// Radio
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, out IntPtr phRadio);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothFindNextRadio(IntPtr hFind, out IntPtr phRadio);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothFindRadioClose(IntPtr hFind);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern int BluetoothGetRadioInfo(IntPtr hRadio, ref BLUETOOTH_RADIO_INFO pRadioInfo);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothIsConnectable(IntPtr hRadio);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothIsDiscoverable(IntPtr hRadio);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothEnableDiscovery(IntPtr hRadio, bool fEnabled);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothEnableIncomingConnections(IntPtr hRadio, bool fEnabled);
// Discovery
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr BluetoothFindFirstDevice(ref BLUETOOTH_DEVICE_SEARCH_PARAMS pbtsp, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothFindNextDevice(IntPtr hFind, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothFindDeviceClose(IntPtr hFind);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern int BluetoothEnumerateInstalledServices(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, ref int pcServices, byte[] pGuidServices);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
internal static extern int BluetoothSetServiceState(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi, ref Guid pGuidService, uint dwServiceFlags);
[DllImport("kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr handle);
// SetService
[DllImport(wsDll, ExactSpelling = true, EntryPoint = "WSASetService", SetLastError = true)]
internal static extern int WSASetService(ref WSAQUERYSET lpqsRegInfo, WSAESETSERVICEOP essoperation, int dwControlFlags);
// Last Error
[DllImport(wsDll, ExactSpelling = true, EntryPoint = "WSAGetLastError", SetLastError = true)]
internal static extern int WSAGetLastError();
// Picker
[DllImport("user32", ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetActiveWindow();
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothSelectDevices(ref BLUETOOTH_SELECT_DEVICE_PARAMS pbtsdp);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothSelectDevicesFree(ref BLUETOOTH_SELECT_DEVICE_PARAMS pbtsdp);
internal delegate bool PFN_DEVICE_CALLBACK(IntPtr pvParam, ref BLUETOOTH_DEVICE_INFO pDevice);
[DllImport(bthpropsDll, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool BluetoothDisplayDeviceProperties(IntPtr hwndParent, ref BLUETOOTH_DEVICE_INFO pbtdi);
// Events
[DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "RegisterDeviceNotification")]
internal static extern RegisterDeviceNotificationSafeHandle RegisterDeviceNotification(
IntPtr hRecipient,
ref DEV_BROADCAST_HANDLE notificationFilter,
RegisterDeviceNotificationFlags flags
);
}
/// <summary>
/// The BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS structure contains specific configuration information about the Bluetooth device responding to an authentication request.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS
{
/// <summary>
/// A BLUETOOTH_DEVICE_INFO structure that contains information about a Bluetooth device.
/// </summary>
internal BLUETOOTH_DEVICE_INFO deviceInfo;
/// <summary>
/// A BLUETOOTH_AUTHENTICATION_METHOD enumeration that defines the authentication method utilized by the Bluetooth device.
/// </summary>
internal BluetoothAuthenticationMethod authenticationMethod;
/// <summary>
/// A BLUETOOTH_IO_CAPABILITY enumeration that defines the input/output capabilities of the Bluetooth device.
/// </summary>
internal BluetoothIoCapability ioCapability;
/// <summary>
/// A AUTHENTICATION_REQUIREMENTS specifies the 'Man in the Middle' protection required for authentication.
/// </summary>
internal BluetoothAuthenticationRequirements authenticationRequirements;
//union{
// ULONG Numeric_Value;
// ULONG Passkey;
//};
/// <summary>
/// A ULONG value used for Numeric Comparison authentication.
/// or
/// A ULONG value used as the passkey used for authentication.
/// </summary>
internal uint Numeric_Value_Passkey;
}
struct DEV_BROADCAST_HANDLE
{
// 32-bit: 3*4 + 2*4 + 16 + 4 + a = 6*4+16 = 24+16 + a = 40 + a = 44
// 64-bit: (3+1)*4 + 2*8 + 16 + 4 + a = 16+16+16+4 + a = 52 + a = 56
const int SizeWithoutFakeDataArray = 40;
const int SizeOfOneByteWithPadding = 4;
const int SizeWithFakeDataArray = SizeWithoutFakeDataArray + SizeOfOneByteWithPadding;
static int ActualSizeWithFakeDataArray;
public DEV_BROADCAST_HDR header;
//--
internal readonly IntPtr dbch_handle;
internal readonly IntPtr dbch_hdevnotify;
internal readonly Guid dbch_eventguid;
internal readonly Int32 dbch_nameoffset;
// We can't include the fake data array because we use this struct as
// the first field in other structs!
// byte dbch_data__0; //dbch_data[1];
//----
public DEV_BROADCAST_HANDLE(IntPtr deviceHandle)
{
this.header.dbch_reserved = 0;
this.dbch_hdevnotify = IntPtr.Zero;
this.dbch_eventguid = Guid.Empty;
this.dbch_nameoffset = 0;
//--
this.header.dbch_devicetype = DbtDevTyp.Handle;
this.dbch_handle = deviceHandle;
//System.Diagnostics.Debug.Assert(
// SizeWithoutFakeDataArray == System.Runtime.InteropServices.Marshal.SizeOf(typeof(DEV_BROADCAST_HANDLE)),
// "Size not as expected");
if (ActualSizeWithFakeDataArray == 0)
{
int actualSizeWithoutFakeDataArray = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DEV_BROADCAST_HANDLE));
ActualSizeWithFakeDataArray = Pad(1 + actualSizeWithoutFakeDataArray, IntPtr.Size);
}
this.header.dbch_size = ActualSizeWithFakeDataArray;
//this.header.dbch_size = actualSizeWithoutFakeDataArray;
}
private static int Pad(int size, int alignment)
{
int x = size + alignment - 1;
x /= alignment;
x *= alignment;
return x;
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct DEV_BROADCAST_HDR
{
internal int dbch_size;
internal DbtDevTyp dbch_devicetype;
internal int dbch_reserved;
}
internal enum DbtDevTyp : uint
{
/// <summary>
/// OEM-defined device type
/// </summary>
Oem = 0x00000000,
/// <summary>
/// Devnode number
/// /// </summary>
DevNode = 0x00000001,
/// <summary>
///
/// </summary>
Volume = 0x00000002,
/// <summary>
///
/// </summary>
Port = 0x00000003,
/// <summary>
/// Network resource
/// </summary>
Network = 0x00000004,
/// <summary>
/// Device interface class
/// </summary>
DeviceInterface = 0x00000005,
/// <summary>
/// File system handle
/// </summary>
Handle = 0x00000006
}
internal enum RegisterDeviceNotificationFlags
{
WINDOW_HANDLE = 0x00000000,
SERVICE_HANDLE = 0x00000001,
ALL_INTERFACE_CLASSES = 0x00000004
}
}