-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUtil.cs
More file actions
108 lines (101 loc) · 4.5 KB
/
Util.cs
File metadata and controls
108 lines (101 loc) · 4.5 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
// Copyright (c) 2013 Eugen Pechanec
using System;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace EugenPechanec.NativeWifi {
public class Util {
private Util() { }
public static string ToHexDelimited(byte[] bytes, int length = -1) {
int len = length < 0 ? bytes.Length : length;
if (len < bytes.Length) throw new ArgumentException();
string hex = "";
for (int i = 0; i < len; i++) {
hex += String.Format(":{0:x2}", bytes[i]);
}
return hex.Substring(1);
}
public static string ToHex(byte[] bytes, int length = -1) {
int len = length < 0 ? bytes.Length : length;
if (len < bytes.Length) throw new ArgumentException();
string hex = "";
for (int i = 0; i < len; i++) {
hex += String.Format("{0:x2}", bytes[i]);
}
return hex;
}
/// <summary>
/// Helper method to wrap calls to Native WiFi API methods.
/// If the method falls, throws an exception containing the error code.
/// </summary>
/// <param name="win32ErrorCode">The error code.</param>
[DebuggerStepThrough]
internal static void ThrowIfError(int win32ErrorCode) {
if (win32ErrorCode != 0)
throw new Win32Exception(win32ErrorCode);
}
/// <summary>
/// Gets a string that describes a specified reason code.
/// </summary>
/// <param name="peerStateChange">The reason code.</param>
/// <returns>The string.</returns>
internal static string GetStringForReasonCode(WlanReasonCode reasonCode) {
StringBuilder sb = new StringBuilder(1024); // the 1024 dataSize here is arbitrary; the WlanReasonCodeToString docs fail to specify a recommended dataSize
ThrowIfError(
NativeMethods.WlanReasonCodeToString(reasonCode, (uint)sb.Capacity, sb, IntPtr.Zero));
return sb.ToString();
}
//=========================================================================
internal static Version DwordToVersion(uint dword) {
int major = (int)dword & 0xffff;
int minor = (int)dword >> 16;
Version version = new Version(major, minor);
return version;
}
internal static uint VersionToDword(Version version) {
uint major = (uint)version.Major;
uint minor = (uint)version.Minor;
uint dword = minor << 16 | major;
return dword;
}
internal static PhysicalAddress Dot11MacAddressToPhysicalAddress(Dot11MacAddress macAddress) {
return Dot11MacAddress.ToPhysicalAddress(macAddress);
}
internal static Dot11MacAddress PhysicalAddressToDot11MacAddress(PhysicalAddress phyAddress) {
return Dot11MacAddress.FromPhysicalAddress(phyAddress);
}
internal static PhysicalAddress[] ConvertDot11MacAddresses(Dot11MacAddress[] macAddresses) {
int length = macAddresses.Length;
PhysicalAddress[] array = new PhysicalAddress[length];
for (int i = 0; i < length; i++) {
array[i] = Dot11MacAddress.ToPhysicalAddress(macAddresses[i]);
}
return array;
}
internal static Dot11MacAddress[] ConvertPhysicalAddresses(PhysicalAddress[] phyAddresses) {
int length = phyAddresses.Length;
Dot11MacAddress[] array = new Dot11MacAddress[length];
for (int i = 0; i < length; i++) {
array[i] = Dot11MacAddress.FromPhysicalAddress(phyAddresses[i]);
}
return array;
}
//=========================================================================
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
internal static T ParseStruct<T>(IntPtr pointer, uint size) where T : struct {
int expectedSize = Marshal.SizeOf(typeof(T));
T value = new T();
if (size >= expectedSize) {
value = (T)Marshal.PtrToStructure(pointer, typeof(T));
}
return value;
}
internal static uint ParseDword(IntPtr pointer) {
uint value = Convert.ToUInt32(Marshal.ReadInt32(pointer));
return value;
}
}
}