forked from jcwalker/WiFiProfileManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddNativeWiFiFunctions.ps1
117 lines (101 loc) · 3.91 KB
/
AddNativeWiFiFunctions.ps1
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
$WlanGetProfileListSig = @'
[DllImport("wlanapi.dll")]
public static extern uint WlanOpenHandle(
[In] UInt32 clientVersion,
[In, Out] IntPtr pReserved,
[Out] out UInt32 negotiatedVersion,
[Out] out IntPtr clientHandle
);
[DllImport("Wlanapi.dll")]
public static extern uint WlanCloseHandle(
[In] IntPtr ClientHandle,
IntPtr pReserved
);
[DllImport("wlanapi.dll", SetLastError = true, CallingConvention=CallingConvention.Winapi)]
public static extern uint WlanGetProfileList(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] IntPtr pReserved,
[Out] out IntPtr profileList
);
[DllImport("wlanapi.dll")]
public static extern uint WlanGetProfile(
[In]IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In, MarshalAs(UnmanagedType.LPWStr)] string profileName,
[In, Out] IntPtr pReserved,
[Out, MarshalAs(UnmanagedType.LPWStr)] out string pstrProfileXml,
[In, Out, Optional] ref uint flags,
[Out, Optional] out uint grantedAccess
);
[DllImport("wlanapi.dll")]
public static extern uint WlanDeleteProfile(
[In]IntPtr clientHanle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In, MarshalAs(UnmanagedType.LPWStr)] string profileName,
[In, Out] IntPtr pReserved
);
[DllImport("wlanapi.dll", EntryPoint = "WlanFreeMemory")]
public static extern void WlanFreeMemory(
[In] IntPtr pMemory
);
[DllImport("Wlanapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint WlanSetProfile(
[In] IntPtr clientHanle,
[In] ref Guid interfaceGuid,
[In] uint flags,
[In] IntPtr ProfileXml,
[In, Optional] IntPtr AllUserProfileSecurity,
[In] bool Overwrite,
[In, Out] IntPtr pReserved,
[In, Out]ref IntPtr pdwReasonCode
);
[DllImport("wlanapi.dll",SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint WlanReasonCodeToString(
[In] uint reasonCode,
[In] uint bufferSize,
[In, Out] StringBuilder builder,
[In, Out] IntPtr Reserved
);
public struct WLAN_PROFILE_INFO_LIST
{
public uint dwNumberOfItems;
public uint dwIndex;
public WLAN_PROFILE_INFO[] ProfileInfo;
public WLAN_PROFILE_INFO_LIST(IntPtr ppProfileList)
{
dwNumberOfItems = (uint)Marshal.ReadInt32(ppProfileList);
dwIndex = (uint)Marshal.ReadInt32(ppProfileList, 4);
ProfileInfo = new WLAN_PROFILE_INFO[dwNumberOfItems];
IntPtr ppProfileListTemp = new IntPtr(ppProfileList.ToInt64() + 8);
for (int i = 0; i < dwNumberOfItems; i++)
{
ppProfileList = new IntPtr(ppProfileListTemp.ToInt64() + i * Marshal.SizeOf(typeof(WLAN_PROFILE_INFO)));
ProfileInfo[i] = (WLAN_PROFILE_INFO)Marshal.PtrToStructure(ppProfileList, typeof(WLAN_PROFILE_INFO));
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WLAN_PROFILE_INFO
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strProfileName;
public WlanProfileFlags ProfileFLags;
}
[Flags]
public enum WlanProfileFlags
{
AllUser = 0,
GroupPolicy = 1,
User = 2
}
public class ProfileInfo{
public string ProfileName;
public string ConnectionMode;
public string Authentication;
public string Encyption;
public string Password;
public string Xml;
}
'@
Add-Type -MemberDefinition $WlanGetProfileListSig -Name ProfileManagement -Namespace WiFi -Using System.Text -PassThru