forked from jperson2000/GPS.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMethods.cs
1284 lines (1106 loc) · 44.7 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security;
#if DEBUG
using System.Diagnostics;
#endif
namespace GeoFramework.Gps.IO
{
#if !PocketPC
[SuppressUnmanagedCodeSecurity]
#endif
internal static class NativeMethods
{
#region Cross-Platform Members
/*
* Many API calls are identical on either framework. The only thing different is
* the dll name in the DllImport atribute, which can be a string constant. The
* API calls that are used by both frameworks can be moved here to lessen the
* code footprint for the sake of maintainability. Others, like the GPSID and
* Serial Comm can be kept seprarated.
*/
#if !PocketPC
private const string Kernel32 = "kernel32.dll";
#else
private const string Kernel32 = "coredll.dll";
#endif
#region Kernel32
public static readonly IntPtr INVALID_HANDLE = new IntPtr(-1);
[DllImport(Kernel32, SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
#region IOControl
/// <summary>
/// Interacts with a device driver
/// </summary>
/// <param name="hDevice"> A handle to a device opened with the CreateFile function. </param>
/// <param name="dwIoControlCode"> A control code specific to the device driver. </param>
/// <param name="lpInBuffer"></param>
/// <param name="nInBufferSize"></param>
/// <param name="lpOutBuffer"></param>
/// <param name="nOutBufferSize"></param>
/// <param name="lpBytesReturned"> The number of bytes returned in the out buffer. </param>
/// <param name="lpOverlapped"> A pointer to a callback for async commands. </param>
/// <returns></returns>
[DllImport(Kernel32, SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
/// <summary>
/// Opens an IO channel with a physical device.
/// </summary>
/// <param name="lpFileName"> The device path </param>
/// <param name="dwDesiredAccess"></param>
/// <param name="dwShareMode"></param>
/// <param name="lpSecurityAttributes"></param>
/// <param name="dwCreationDisposition"></param>
/// <param name="dwFlagsAndAttributes"></param>
/// <param name="hTemplateFile"></param>
/// <returns> A handle to the device. </returns>
[DllImport(Kernel32, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile(
string lpFileName, // file name
FileAccess dwDesiredAccess, // access mode
FileShare dwShareMode, // share mode
uint lpSecurityAttributes, // SD
FileMode dwCreationDisposition, // how to create
FileAttributes dwFlagsAndAttributes, // file attributes
IntPtr hTemplateFile // handle to template file
);
/// <summary>
/// Performs synchronous reads on an IO channel
/// </summary>
/// <param name="hDevice"> A handle to a device opened with the CreateFile function. </param>
/// <param name="lpBuffer"></param>
/// <param name="nNumberOfBytesToRead"></param>
/// <param name="lpNumberOfBytesRead"></param>
/// <param name="lpOverlapped"></param>
/// <returns></returns>
[DllImport(Kernel32, SetLastError = true)]
public static extern bool ReadFile(
IntPtr handle,
byte[] lpBuffer,
uint nNumberOfBytesToRead,
out uint lpNumberOfBytesRead,
IntPtr lpOverlapped);
/// <summary>
/// Performs synchronous writes on an IO channel
/// </summary>
/// <param name="hDevice"> A handle to a device opened with the CreateFile function. </param>
/// <param name="lpBuffer"></param>
/// <param name="nNumberOfBytesToWrite"></param>
/// <param name="lpNumberOfBytesWritten"></param>
/// <param name="lpOverlapped"></param>
/// <returns></returns>
[DllImport(Kernel32, SetLastError = true)]
public static extern bool WriteFile(
IntPtr handle,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
IntPtr lpOverlapped);
#endregion
#region System Clock
// http://msdn.microsoft.com/en-us/library/ms724942(VS.85).aspx
[DllImport(Kernel32, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetLocalTime(ref SYSTEMTIME time);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetSystemTime(ref SYSTEMTIME time);
#endregion
#endregion
// These structures are used on both desktop and mobile device platforms with NO changes
#region Microsoft Bluetooth device discovery
public const int BTHNS_RESULT_DEVICE_CONNECTED = 0x00010000;
public const int BTHNS_RESULT_DEVICE_REMEMBERED = 0x00020000;
public const int BTHNS_RESULT_DEVICE_AUTHENTICATED = 0x00040000;
// The namespace of Bluetooth, used by the WSAQUERYSET structure
public const int NS_BTH = 16;
public const int ERROR_SUCCESS = 1306;
// http://msdn.microsoft.com/en-us/library/ms737551(VS.85).aspx
[StructLayout(LayoutKind.Sequential)]
public class BLOB
{
public uint cbSize;
public IntPtr pInfo;
}
// Represents information about a single Bluetooth device
// http://msdn.microsoft.com/en-us/library/aa362934(VS.85).aspx
[StructLayout(LayoutKind.Sequential)]
public class BTH_DEVICE_INFO
{
public uint flags;
public ulong address;
public uint classOfDevice;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 246)]
public byte[] name;
/// <summary>
/// Returns the address of the device.
/// </summary>
public BluetoothAddress Address
{
get { return new BluetoothAddress(address); }
}
/// <summary>
/// Returns the friendly nameof the device.
/// </summary>
public string Name
{
get
{
string value = UTF8Encoding.UTF8.GetString(name, 0, name.Length).Trim('\0');
if (String.IsNullOrEmpty(value))
return Address.ToString();
else
return value;
}
}
/* FxCop says this is unused.
*
/// <summary>
/// Returns the primary purpose of the device.
/// </summary>
public DeviceClass MajorDeviceClass
{
get
{
return (DeviceClass)(classOfDevice & 0x1F00);
}
}
/// <summary>
/// Returns a sub-category describing the purpose of the device.
/// </summary>
public DeviceClass MinorDeviceClass
{
get
{
return (DeviceClass)(classOfDevice & 0xFC);
}
}
*/
/* FxCop says this is unused
*
/// <summary>
/// Returns the major type of device.
/// </summary>
public ServiceClass ServiceClass
{
get
{
return (ServiceClass)(classOfDevice & 0xFFE000); ;
}
}
*/
/* FxCop says this is unused.
*
/// <summary>
/// Returns information describing what information is present in this record.
/// </summary>
public BluetoothDeviceFlags Flags
{
get
{
return (BluetoothDeviceFlags)flags;
}
}
*/
/* FxCop says this is unused.
*
/// <summary>
/// Returns whether the device is paired with the local machine's radio.
/// </summary>
public bool IsPaired
{
get
{
return (Flags & BluetoothDeviceFlags.Paired) != 0;
}
}
*/
/* FxCop says this is unused.
*
/// <summary>
/// Returns whether the device has previously been found.
/// </summary>
public bool IsRemembered
{
get
{
return (Flags & BluetoothDeviceFlags.Remembered) != 0;
}
}
*/
/* FxCop says this is unused.
*
/// <summary>
/// Returns whether the device is currently connected.
/// </summary>
public bool IsConnected
{
get
{
return (Flags & BluetoothDeviceFlags.Connected) != 0;
}
}
*/
/// <summary>
/// Converts the object into a BluetoothDevice object.
/// </summary>
/// <returns></returns>
public BluetoothDevice ToDevice()
{
return new BluetoothDevice(Address, Name);
}
}
[Flags]
public enum BluetoothDeviceFlags
{
AddressIsPresent = 1,
ClassOfDeviceIsPresent = 2,
NameIsPresent = 4,
Paired = 8,
Remembered = 16,
Connected = 32
}
// http://msdn.microsoft.com/en-us/library/aa362926(VS.85).aspx
[StructLayout(LayoutKind.Sequential)]
public class BLUETOOTH_FIND_RADIO_PARAMS
{
public uint dwSize = 4;
}
// http://msdn.microsoft.com/en-us/library/aa362924(VS.85).aspx
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLUETOOTH_DEVICE_INFO
{
public int dwSize;
public long Address;
public uint ulClassofDevice;
[MarshalAs(UnmanagedType.Bool)]
public bool fConnected;
[MarshalAs(UnmanagedType.Bool)]
public bool fRemembered;
[MarshalAs(UnmanagedType.Bool)]
public bool fAuthenticated;
public SYSTEMTIME stLastSeen;
public SYSTEMTIME stLastUsed;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248)]
public string szName;
public BLUETOOTH_DEVICE_INFO(long address)
{
dwSize = 560;
Address = address;
ulClassofDevice = 0;
fConnected = false;
fRemembered = false;
fAuthenticated = false;
stLastSeen = new SYSTEMTIME();
stLastUsed = new SYSTEMTIME();
#if DEBUG
// The size is much smaller on CE (no times and string not inline) it
// appears to ignore the bad dwSize value. So don't check this on CF.
System.Diagnostics.Debug.Assert(Marshal.SizeOf(typeof(BLUETOOTH_DEVICE_INFO)) == dwSize, "BLUETOOTH_DEVICE_INFO SizeOf == dwSize");
#endif
szName = "";
}
}
// http://msdn.microsoft.com/en-us/library/aa362929(VS.85).aspx
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BLUETOOTH_RADIO_INFO
{
public int dwSize;
public long address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 248)]
public string szName;
public uint classOfDevice;
public ushort lmpSubversion;
public short manufacturer;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public class BTH_QUERY_DEVICE
{
public uint LAP;
[MarshalAs(UnmanagedType.U1)]
public byte length;
public BTH_QUERY_DEVICE()
{
// Set the default value
LAP = 0x9E8B33;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
public DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
}
public static SYSTEMTIME FromDateTime(DateTime value)
{
SYSTEMTIME result = new SYSTEMTIME();
result.wYear = (short)value.Year;
result.wMonth = (short)value.Month;
result.wDayOfWeek = (short)value.DayOfWeek;
result.wDay = (short)value.Day;
result.wHour = (short)value.Hour;
result.wMinute = (short)value.Minute;
result.wSecond = (short)value.Second;
result.wMilliseconds = (short)value.Millisecond;
return result;
}
}
[StructLayout(LayoutKind.Sequential)]
public class WSAData
{
public Int16 wVersion = 36; // 2.2
public Int16 wHighVersion = 36; // 2.2
public IntPtr szDescription;
public IntPtr szSystemStatus;
public Int16 iMaxSockets;
public Int16 iMaxUdpDg;
public IntPtr lpVendorInfo;
/* FxCop says this is unused
*
public string Description
{
get { return Marshal.PtrToStringUni(szDescription); }
}
public string SystemStatus
{
get { return Marshal.PtrToStringUni(szSystemStatus); }
}
*/
}
[Flags()]
internal enum LookupFlags : uint
{
None = 0,
Containers = 0x0002,
ReturnName = 0x0010,
ReturnType = 0x0020,
ReturnVersion = 0x0040,
ReturnComment = 0x0080,
ReturnAddr = 0x0100,
ReturnBlob = 0x0200,
FlushCache = 0x1000,
ResService = 0x8000,
}
// http://msdn.microsoft.com/en-us/library/ms898762.aspx
[StructLayout(LayoutKind.Sequential)]
public class WSAQUERYSET
{
public uint dwSize;
public IntPtr szServiceInstanceName;
public IntPtr lpServiceClassId;
public IntPtr lpVersion;
public IntPtr lpszComment;
public uint dwNameSpace = NS_BTH;
public IntPtr lpNSProviderId;
public IntPtr lpszContext;
public uint dwNumberOfProtocols;
public IntPtr lpafpProtocols;
public IntPtr lpszQueryString;
public uint dwNumberOfCsAddrs;
public IntPtr lpcsaBuffer;
public uint dwOutputFlags;
public IntPtr lpBlob;
/* The code below was necessary during early development to ensure that parameters
* were byte-aligned properly. This is no longer necessary.
*
#if DEBUG && !PocketPC
static WSAQUERYSET()
{
// All structures passed to unmanaged methods must be precise. These
// methods can be used to ensure that fields are properly aligned.
// The namespace should be at byte 20
Debug.Assert(Marshal.OffsetOf(typeof(WSAQUERYSET), "dwNameSpace").ToInt32() == 20, "offset dwNameSpace");
// The address buffer should live at byte 48
Debug.Assert(Marshal.OffsetOf(typeof(WSAQUERYSET), "lpcsaBuffer").ToInt32() == 48, "offset lpcsaBuffer");
// The address of output flags should be byte 52
Debug.Assert(Marshal.OffsetOf(typeof(WSAQUERYSET), "dwOutputFlags").ToInt32() == 52, "offset dwOutputFlags");
// The address of the BLOB should be byte 56
Debug.Assert(Marshal.OffsetOf(typeof(WSAQUERYSET), "lpBlob").ToInt32() == 56, "offset lpBlob");
// The total size should be 60 bytes
Debug.Assert(Marshal.SizeOf(typeof(WSAQUERYSET)) == 60, "StructLength");
}
#endif
*/
public WSAQUERYSET()
{
// The size will vary on 32-bit vs. 64-bit systems!
dwSize = (uint)Marshal.SizeOf(this);
}
#if PocketPC
public string FriendlyName
{
get
{
return Marshal.PtrToStringUni(szServiceInstanceName);
}
}
#endif
/* FxCop says this is unused
*
//public CSADDR_INFO AddressInfo
//{
// get
// {
// CSADDR_INFO info = new CSADDR_INFO();
// Marshal.PtrToStructure(this.lpcsaBuffer, info);
// return info;
// }
//}
*/
/* FxCop says this is unused
*
public string Comment
{
get
{
return Marshal.PtrToStringUni(lpszComment);
}
}
public string Context
{
get
{
return Marshal.PtrToStringUni(lpszContext);
}
}
*/
/* FxCop says this is unused
*
//public string QueryString
//{
// get
// {
// return Marshal.PtrToStringUni(lpszQueryString);
// }
//}
/* FxCop says this is unused
*
//public Guid ServiceGuid
//{
// get
// {
// /* The lpServiceClassId parameter is a 16-byte GUID. Let's convert
// * this pointer into a GUID, shall we?
// */
// byte[] result = new byte[16];
// for (int index = 0; index < 16; index++)
// result[index] = Marshal.ReadByte(lpServiceClassId, index);
// return new Guid(result);
// }
//}
}
#endregion
#endregion
#region USB
public const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_ALLCLASSES = 0x00000004;
public const int DIGCF_PROFILE = 0x00000008;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVINFO_DATA
{
public uint cbSize;
public Guid ClassGuid;
public uint DevInst;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential)]
public struct SP_DEVICE_INTERFACE_DATA
{
public uint cbSize;
public Guid InterfaceClassGuid;
public uint Flags;
public IntPtr Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public UInt32 cbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string DevicePath;
}
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiClassGuidsFromName(
StringBuilder ClassName,
//[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
IntPtr ClassGuidList,
int ClassGuidListSize,
out int RequiredSize);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern int CM_Get_Parent(
out UInt32 pdnDevInst,
UInt32 dnDevInst,
int ulFlags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int CM_Get_Device_ID(
UInt32 dnDevInst,
IntPtr Buffer,
int BufferLen,
int ulFlags
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList(IntPtr hDevInfo);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid,
IntPtr Enumerator,
IntPtr hwndParent,
int Flags
);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiEnumDeviceInfo(
IntPtr hDevInfo,
UInt32 memberIndex,
ref SP_DEVINFO_DATA devInfo);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiEnumDeviceInterfaces(
IntPtr hDevInfo,
IntPtr devInfo,
ref Guid interfaceClassGuid,
UInt32 memberIndex,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Boolean SetupDiGetDeviceInterfaceDetail(
IntPtr hDevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
UInt32 deviceInterfaceDetailDataSize,
out UInt32 requiredSize,
ref SP_DEVINFO_DATA deviceInfoData
);
#endregion
#if !PocketPC
#region Desktop Members
#region Bluetooth
// http://msdn.microsoft.com/en-us/library/aa362795.aspx
[DllImport("Irprops.cpl", SetLastError = true)]
public static extern int BluetoothGetDeviceInfo(IntPtr hRadio, ref BLUETOOTH_DEVICE_INFO pbtdi);
[DllImport("Irprops.cpl", SetLastError = true)]
public static extern int BluetoothGetRadioInfo(IntPtr hRadio, ref BLUETOOTH_RADIO_INFO pbtdi);
// http://msdn.microsoft.com/en-us/library/aa362786(VS.85).aspx
[DllImport("Irprops.cpl", SetLastError = true)]
public static extern IntPtr BluetoothFindFirstRadio(BLUETOOTH_FIND_RADIO_PARAMS pbtfrp, ref IntPtr phRadio);
/* FxCops says this is unused
*
[DllImport("Irprops.cpl", SetLastError = true)]
public static extern int BluetoothFindNextRadio(IntPtr hFind, ref IntPtr phRadio);
*/
// http://msdn.microsoft.com/en-us/library/aa362792(VS.85).aspx
[DllImport("Irprops.cpl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothFindRadioClose(IntPtr hFind);
[DllImport("Irprops.cpl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothIsConnectable(IntPtr hRadio);
[DllImport("Irprops.cpl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothIsDiscoverable(IntPtr hRadio);
[DllImport("Irprops.cpl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothEnableDiscovery(IntPtr hRadio,
[MarshalAs(UnmanagedType.Bool)]
bool fEnabled);
[DllImport("Irprops.cpl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BluetoothEnableIncomingConnections(IntPtr hRadio,
[MarshalAs(UnmanagedType.Bool)]
bool fEnabled);
[DllImport("Ws2_32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int WSAStartup(
UInt16 wVersionRequested,
WSAData wsaData);
[DllImport("Ws2_32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int WSACleanup();
// http://msdn.microsoft.com/en-us/library/ms898747.aspx
[DllImport("Ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int WSALookupServiceBegin(
WSAQUERYSET qsRestrictions,
LookupFlags dwControlFlags,
ref IntPtr lphLookup);
[DllImport("Ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int WSALookupServiceNext(
IntPtr hLookup,
LookupFlags dwControlFlags,
ref int lpdwBufferLength,
byte[] pqsResults);
[DllImport("Ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int WSALookupServiceEnd(IntPtr hLookup);
#endregion
#endregion
#else
#region Mobile Device Members
#region GPS Intermediate Driver
// This method is used to tell the GPSID to reload its settings from the registry.
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer,
uint nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
uint lpBytesReturned,
IntPtr lpOverlapped);
public const uint IOCTL_SERVICE_REFRESH = 0x4100000C;
#endregion
#region Serial Port
// http://msdn.microsoft.com/en-us/library/aa915369.aspx
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool GetCommState(IntPtr handle, ref DCB dcb);
// http://msdn.microsoft.com/en-us/library/aa908949.aspx
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool SetCommState(IntPtr handle, [In] ref DCB dcb);
// http://msdn.microsoft.com/en-us/library/aa363428(VS.85).aspx
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool PurgeComm(IntPtr handle, uint flags);
// http://msdn.microsoft.com/en-us/library/aa363439(VS.85).aspx
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool SetupComm(IntPtr handle, uint dwInQueue, uint dwOutQueue);
// http://msdn.microsoft.com/en-us/library/bb202767.aspx
[StructLayout(LayoutKind.Sequential)]
public struct DCB
{
internal uint DCBLength;
internal uint BaudRate;
private BitVector32 Flags;
private uint wReserved; // not currently used
internal uint XonLim; // transmit XON threshold
internal uint XoffLim; // transmit XOFF threshold
internal byte ByteSize;
internal byte Parity;
internal byte StopBits;
//...and some more
internal char XonChar; // Tx and Rx XON character
internal char XoffChar; // Tx and Rx XOFF character
internal char ErrorChar; // error replacement character
internal char EofChar; // end of input character
internal char EvtChar; // received event character
private uint wReserved1; // reserved; do not use
private static readonly int fBinary;
private static readonly int fParity;
private static readonly int fOutxCtsFlow;
private static readonly int fOutxDsrFlow;
private static readonly BitVector32.Section fDtrControl;
private static readonly int fDsrSensitivity;
private static readonly int fTXContinueOnXoff;
private static readonly int fOutX;
private static readonly int fInX;
private static readonly int fErrorChar;
private static readonly int fNull;
private static readonly BitVector32.Section fRtsControl;
private static readonly int fAbortOnError;
static DCB()
{
// Create Boolean Mask
int previousMask;
fBinary = BitVector32.CreateMask();
fParity = BitVector32.CreateMask(fBinary);
fOutxCtsFlow = BitVector32.CreateMask(fParity);
fOutxDsrFlow = BitVector32.CreateMask(fOutxCtsFlow);
previousMask = BitVector32.CreateMask(fOutxDsrFlow);
previousMask = BitVector32.CreateMask(previousMask);
fDsrSensitivity = BitVector32.CreateMask(previousMask);
fTXContinueOnXoff = BitVector32.CreateMask(fDsrSensitivity);
fOutX = BitVector32.CreateMask(fTXContinueOnXoff);
fInX = BitVector32.CreateMask(fOutX);
fErrorChar = BitVector32.CreateMask(fInX);
fNull = BitVector32.CreateMask(fErrorChar);
previousMask = BitVector32.CreateMask(fNull);
previousMask = BitVector32.CreateMask(previousMask);
fAbortOnError = BitVector32.CreateMask(previousMask);
// Create section Mask
BitVector32.Section previousSection;
previousSection = BitVector32.CreateSection(1);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
fDtrControl = BitVector32.CreateSection(2, previousSection);
previousSection = BitVector32.CreateSection(1, fDtrControl);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
previousSection = BitVector32.CreateSection(1, previousSection);
fRtsControl = BitVector32.CreateSection(3, previousSection);
previousSection = BitVector32.CreateSection(1, fRtsControl);
}
public bool Binary
{
get { return Flags[fBinary]; }
set { Flags[fBinary] = value; }
}
public bool CheckParity
{
get { return Flags[fParity]; }
set { Flags[fParity] = value; }
}
public bool OutxCtsFlow
{
get { return Flags[fOutxCtsFlow]; }
set { Flags[fOutxCtsFlow] = value; }
}
public bool OutxDsrFlow
{
get { return Flags[fOutxDsrFlow]; }
set { Flags[fOutxDsrFlow] = value; }
}
public DtrControl DtrControl
{
get { return (DtrControl)Flags[fDtrControl]; }
set { Flags[fDtrControl] = (int)value; }
}
public bool DsrSensitivity
{
get { return Flags[fDsrSensitivity]; }
set { Flags[fDsrSensitivity] = value; }
}
public bool TxContinueOnXoff
{
get { return Flags[fTXContinueOnXoff]; }
set { Flags[fTXContinueOnXoff] = value; }
}
public bool OutX
{
get { return Flags[fOutX]; }
set { Flags[fOutX] = value; }
}
public bool InX
{
get { return Flags[fInX]; }
set { Flags[fInX] = value; }
}
public bool ReplaceErrorChar
{
get { return Flags[fErrorChar]; }
set { Flags[fErrorChar] = value; }
}
public bool Null
{
get { return Flags[fNull]; }
set { Flags[fNull] = value; }
}
public RtsControl RtsControl
{
get { return (RtsControl)Flags[fRtsControl]; }
set { Flags[fRtsControl] = (int)value; }
}
public bool AbortOnError
{
get { return Flags[fAbortOnError]; }
set { Flags[fAbortOnError] = value; }
}
}
// http://msdn.microsoft.com/en-us/library/aa363180(VS.85).aspx
[DllImport("coredll.dll", SetLastError = true)]
public static extern bool ClearCommError(IntPtr handle, ref uint lpErrors, COMMSTAT lpStat);
// http://msdn.microsoft.com/en-us/library/aa363200(VS.85).aspx
[StructLayout(LayoutKind.Sequential)]
public struct COMMSTAT
{
private BitVector32 Flags;
internal uint dbInQue;
internal uint dbOutQueue;
private static readonly int fCtsHold;
private static readonly int fDsrHold;
private static readonly int fRlsdHold;
private static readonly int fXoffHold;
private static readonly int fXoffSent;
private static readonly int fEof;
private static readonly int fTrim;
static COMMSTAT()
{
// Create Boolean Mask
fCtsHold = BitVector32.CreateMask();
fDsrHold = BitVector32.CreateMask(fCtsHold);
fRlsdHold = BitVector32.CreateMask(fDsrHold);
fXoffHold = BitVector32.CreateMask(fRlsdHold);
fXoffSent = BitVector32.CreateMask(fXoffHold);
fEof = BitVector32.CreateMask(fXoffSent);
fTrim = BitVector32.CreateMask(fEof);
}
public bool CtsHold
{
get { return Flags[fCtsHold]; }
set { Flags[fCtsHold] = value; }
}
public bool DsrHold
{
get { return Flags[fDsrHold]; }
set { Flags[fDsrHold] = value; }
}
public bool RlsdHold
{
get { return Flags[fRlsdHold]; }
set { Flags[fRlsdHold] = value; }
}
public bool XoffHold
{
get { return Flags[fXoffHold]; }
set { Flags[fXoffHold] = value; }
}