microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/AutoShell_Win32.cs
501lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Linq; |
| 7 | using System.Runtime.InteropServices; |
| 8 | using System.Text; |
| 9 | using System.Threading.Tasks; |
| 10 | using System.Windows; |
| 11 | |
| 12 | namespace autoShell |
| 13 | { |
| 14 | internal unsafe partial class AutoShell |
| 15 | { |
| 16 | private const int SPI_SETDESKWALLPAPER = 20; |
| 17 | private const int SPIF_UPDATEINIFILE = 0x01; |
| 18 | private const int SPIF_SENDCHANGE = 0x02; |
| 19 | private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002; |
| 20 | |
| 21 | // window rect structure |
| 22 | internal struct RECT |
| 23 | { |
| 24 | public int Left; // x position of upper-left corner |
| 25 | public int Top; // y position of upper-left corner |
| 26 | public int Right; // x position of lower-right corner |
| 27 | public int Bottom; // y position of lower-right corner |
| 28 | } |
| 29 | |
| 30 | internal struct Size |
| 31 | { |
| 32 | public int x; |
| 33 | public int y; |
| 34 | } |
| 35 | |
| 36 | // import GetWindowRect |
| 37 | [DllImport("user32.dll")] |
| 38 | static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect); |
| 39 | |
| 40 | // import GetShellWindow |
| 41 | [DllImport("user32.dll")] |
| 42 | static extern IntPtr GetShellWindow(); |
| 43 | |
| 44 | // import GetDesktopWindow |
| 45 | [DllImport("user32.dll")] |
| 46 | static extern IntPtr GetDesktopWindow(); |
| 47 | |
| 48 | // import SetForegroundWindow |
| 49 | [System.Runtime.InteropServices.DllImport("user32.dll")] |
| 50 | private static extern bool SetForegroundWindow(IntPtr hWnd); |
| 51 | |
| 52 | [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)] |
| 53 | static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, IntPtr lParam); |
| 54 | |
| 55 | // import SetWindowPos |
| 56 | [DllImport("user32.dll")] |
| 57 | static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); |
| 58 | |
| 59 | // import FindWindowEx |
| 60 | [DllImport("user32.dll")] |
| 61 | static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName); |
| 62 | |
| 63 | [DllImport("user32.dll", CharSet = CharSet.Auto)] |
| 64 | private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); |
| 65 | |
| 66 | [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] |
| 67 | private static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags); |
| 68 | |
| 69 | [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] |
| 70 | private static extern bool FreeLibrary(IntPtr hModule); |
| 71 | |
| 72 | [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] |
| 73 | private static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax); |
| 74 | |
| 75 | #region Virtual Desktop APIs |
| 76 | |
| 77 | public enum APPLICATION_VIEW_CLOAK_TYPE : int |
| 78 | { |
| 79 | AVCT_NONE = 0, |
| 80 | AVCT_DEFAULT = 1, |
| 81 | AVCT_VIRTUAL_DESKTOP = 2 |
| 82 | } |
| 83 | |
| 84 | public enum APPLICATION_VIEW_COMPATIBILITY_POLICY : int |
| 85 | { |
| 86 | AVCP_NONE = 0, |
| 87 | AVCP_SMALL_SCREEN = 1, |
| 88 | AVCP_TABLET_SMALL_SCREEN = 2, |
| 89 | AVCP_VERY_SMALL_SCREEN = 3, |
| 90 | AVCP_HIGH_SCALE_FACTOR = 4 |
| 91 | } |
| 92 | |
| 93 | // Virtual Desktop COM Interface GUIDs |
| 94 | public static readonly Guid CLSID_ImmersiveShell = new Guid("C2F03A33-21F5-47FA-B4BB-156362A2F239"); |
| 95 | public static readonly Guid CLSID_VirtualDesktopManagerInternal = new Guid("C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B"); |
| 96 | public static readonly Guid CLSID_VirtualDesktopManager = new Guid("AA509086-5CA9-4C25-8F95-589D3C07B48A"); |
| 97 | public static readonly Guid CLSID_VirtualDesktopPinnedApps = new Guid("B5A399E7-1C87-46B8-88E9-FC5747B171BD"); |
| 98 | |
| 99 | // IServiceProvider COM Interface |
| 100 | [ComImport] |
| 101 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 102 | [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")] |
| 103 | private interface IServiceProvider |
| 104 | { |
| 105 | [return: MarshalAs(UnmanagedType.IUnknown)] |
| 106 | void QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObject); |
| 107 | } |
| 108 | |
| 109 | // IVirtualDesktopManager COM Interface |
| 110 | [ComImport] |
| 111 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 112 | [Guid("A5CD92FF-29BE-454C-8D04-D82879FB3F1B")] |
| 113 | internal interface IVirtualDesktopManager |
| 114 | { |
| 115 | bool IsWindowOnCurrentVirtualDesktop(IntPtr topLevelWindow); |
| 116 | Guid GetWindowDesktopId(IntPtr topLevelWindow); |
| 117 | void MoveWindowToDesktop(IntPtr topLevelWindow, ref Guid desktopId); |
| 118 | } |
| 119 | |
| 120 | // IVirtualDesktop COM Interface (Windows 10/11) |
| 121 | [ComImport] |
| 122 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 123 | [Guid("3F07F4BE-B107-441A-AF0F-39D82529072C")] |
| 124 | internal interface IVirtualDesktop |
| 125 | { |
| 126 | bool IsViewVisible(IApplicationView view); |
| 127 | Guid GetId(); |
| 128 | // TODO: proper HSTRING custom marshaling |
| 129 | [return: MarshalAs(UnmanagedType.HString)] |
| 130 | string GetName(); |
| 131 | [return: MarshalAs(UnmanagedType.HString)] |
| 132 | string GetWallpaperPath(); |
| 133 | bool IsRemote(); |
| 134 | } |
| 135 | |
| 136 | // IVirtualDesktopManagerInternal COM Interface |
| 137 | [ComImport] |
| 138 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 139 | [Guid("53F5CA0B-158F-4124-900C-057158060B27")] |
| 140 | internal interface IVirtualDesktopManagerInternal_BUGBUG |
| 141 | { |
| 142 | int GetCount(); |
| 143 | void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); |
| 144 | bool CanViewMoveDesktops(IApplicationView view); |
| 145 | IVirtualDesktop GetCurrentDesktop(); |
| 146 | void GetDesktops(out IObjectArray desktops); |
| 147 | [PreserveSig] |
| 148 | int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); |
| 149 | void SwitchDesktop(IVirtualDesktop desktop); |
| 150 | IVirtualDesktop CreateDesktop(); |
| 151 | void MoveDesktop(IVirtualDesktop desktop, int nIndex); |
| 152 | void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); |
| 153 | IVirtualDesktop FindDesktop(ref Guid desktopid); |
| 154 | void GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop desktop, out IObjectArray unknown1, out IObjectArray unknown2); |
| 155 | void SetDesktopName(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string name); |
| 156 | void SetDesktopWallpaper(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string path); |
| 157 | void UpdateWallpaperPathForAllDesktops([MarshalAs(UnmanagedType.HString)] string path); |
| 158 | void CopyDesktopState(IApplicationView pView0, IApplicationView pView1); |
| 159 | void CreateRemoteDesktop([MarshalAs(UnmanagedType.HString)] string path, out IVirtualDesktop desktop); |
| 160 | void SwitchRemoteDesktop(IVirtualDesktop desktop, IntPtr switchtype); |
| 161 | void SwitchDesktopWithAnimation(IVirtualDesktop desktop); |
| 162 | void GetLastActiveDesktop(out IVirtualDesktop desktop); |
| 163 | void WaitForAnimationToComplete(); |
| 164 | } |
| 165 | |
| 166 | // IVirtualDesktopManagerInternal COM Interface |
| 167 | [ComImport] |
| 168 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 169 | [Guid("53F5CA0B-158F-4124-900C-057158060B27")] |
| 170 | internal interface IVirtualDesktopManagerInternal |
| 171 | { |
| 172 | int GetCount(); |
| 173 | void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); |
| 174 | bool CanViewMoveDesktops(IApplicationView view); |
| 175 | IVirtualDesktop GetCurrentDesktop(); |
| 176 | void GetDesktops(out IObjectArray desktops); |
| 177 | [PreserveSig] |
| 178 | int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); |
| 179 | void SwitchDesktop(IVirtualDesktop desktop); |
| 180 | void SwitchDesktopAndMoveForegroundView(IVirtualDesktop desktop); |
| 181 | IVirtualDesktop CreateDesktop(); |
| 182 | void MoveDesktop(IVirtualDesktop desktop, int nIndex); |
| 183 | void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); |
| 184 | IVirtualDesktop FindDesktop(ref Guid desktopid); |
| 185 | void GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop desktop, out IObjectArray unknown1, out IObjectArray unknown2); |
| 186 | void SetDesktopName(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string name); |
| 187 | void SetDesktopWallpaper(IVirtualDesktop desktop, [MarshalAs(UnmanagedType.HString)] string path); |
| 188 | void UpdateWallpaperPathForAllDesktops([MarshalAs(UnmanagedType.HString)] string path); |
| 189 | void CopyDesktopState(IApplicationView pView0, IApplicationView pView1); |
| 190 | void CreateRemoteDesktop([MarshalAs(UnmanagedType.HString)] string path, out IVirtualDesktop desktop); |
| 191 | void SwitchRemoteDesktop(IVirtualDesktop desktop, IntPtr switchtype); |
| 192 | void SwitchDesktopWithAnimation(IVirtualDesktop desktop); |
| 193 | void GetLastActiveDesktop(out IVirtualDesktop desktop); |
| 194 | void WaitForAnimationToComplete(); |
| 195 | } |
| 196 | |
| 197 | // IObjectArray COM Interface |
| 198 | [ComImport] |
| 199 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 200 | [Guid("92CA9DCD-5622-4BBA-A805-5E9F541BD8C9")] |
| 201 | internal interface IObjectArray |
| 202 | { |
| 203 | void GetCount(out int pcObjects); |
| 204 | void GetAt(int uiIndex, ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppv); |
| 205 | } |
| 206 | |
| 207 | [ComImport] |
| 208 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 209 | [Guid("372E1D3B-38D3-42E4-A15B-8AB2B178F513")] |
| 210 | internal interface IApplicationView |
| 211 | { |
| 212 | int SetFocus(); |
| 213 | int SwitchTo(); |
| 214 | int TryInvokeBack(IntPtr /* IAsyncCallback* */ callback); |
| 215 | int GetThumbnailWindow(out IntPtr hwnd); |
| 216 | int GetMonitor(out IntPtr /* IImmersiveMonitor */ immersiveMonitor); |
| 217 | int GetVisibility(out int visibility); |
| 218 | int SetCloak(APPLICATION_VIEW_CLOAK_TYPE cloakType, int unknown); |
| 219 | int GetPosition(ref Guid guid /* GUID for IApplicationViewPosition */, out IntPtr /* IApplicationViewPosition** */ position); |
| 220 | int SetPosition(ref IntPtr /* IApplicationViewPosition* */ position); |
| 221 | int InsertAfterWindow(IntPtr hwnd); |
| 222 | int GetExtendedFramePosition(out Rect rect); |
| 223 | int GetAppUserModelId([MarshalAs(UnmanagedType.LPWStr)] out string id); |
| 224 | int SetAppUserModelId(string id); |
| 225 | int IsEqualByAppUserModelId(string id, out int result); |
| 226 | int GetViewState(out uint state); |
| 227 | int SetViewState(uint state); |
| 228 | int GetNeediness(out int neediness); |
| 229 | int GetLastActivationTimestamp(out ulong timestamp); |
| 230 | int SetLastActivationTimestamp(ulong timestamp); |
| 231 | int GetVirtualDesktopId(out Guid guid); |
| 232 | int SetVirtualDesktopId(ref Guid guid); |
| 233 | int GetShowInSwitchers(out int flag); |
| 234 | int SetShowInSwitchers(int flag); |
| 235 | int GetScaleFactor(out int factor); |
| 236 | int CanReceiveInput(out bool canReceiveInput); |
| 237 | int GetCompatibilityPolicyType(out APPLICATION_VIEW_COMPATIBILITY_POLICY flags); |
| 238 | int SetCompatibilityPolicyType(APPLICATION_VIEW_COMPATIBILITY_POLICY flags); |
| 239 | int GetSizeConstraints(IntPtr /* IImmersiveMonitor* */ monitor, out Size size1, out Size size2); |
| 240 | int GetSizeConstraintsForDpi(uint uint1, out Size size1, out Size size2); |
| 241 | int SetSizeConstraintsForDpi(ref uint uint1, ref Size size1, ref Size size2); |
| 242 | int OnMinSizePreferencesUpdated(IntPtr hwnd); |
| 243 | int ApplyOperation(IntPtr /* IApplicationViewOperation* */ operation); |
| 244 | int IsTray(out bool isTray); |
| 245 | int IsInHighZOrderBand(out bool isInHighZOrderBand); |
| 246 | int IsSplashScreenPresented(out bool isSplashScreenPresented); |
| 247 | int Flash(); |
| 248 | int GetRootSwitchableOwner(out IApplicationView rootSwitchableOwner); |
| 249 | int EnumerateOwnershipTree(out IObjectArray ownershipTree); |
| 250 | int GetEnterpriseId([MarshalAs(UnmanagedType.LPWStr)] out string enterpriseId); |
| 251 | int IsMirrored(out bool isMirrored); |
| 252 | int Unknown1(out int unknown); |
| 253 | int Unknown2(out int unknown); |
| 254 | int Unknown3(out int unknown); |
| 255 | int Unknown4(out int unknown); |
| 256 | int Unknown5(out int unknown); |
| 257 | int Unknown6(int unknown); |
| 258 | int Unknown7(); |
| 259 | int Unknown8(out int unknown); |
| 260 | int Unknown9(int unknown); |
| 261 | int Unknown10(int unknownX, int unknownY); |
| 262 | int Unknown11(int unknown); |
| 263 | int Unknown12(out Size size1); |
| 264 | } |
| 265 | |
| 266 | [ComImport] |
| 267 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 268 | [Guid("1841C6D7-4F9D-42C0-AF41-8747538F10E5")] |
| 269 | internal interface IApplicationViewCollection |
| 270 | { |
| 271 | int GetViews(out IObjectArray array); |
| 272 | int GetViewsByZOrder(out IObjectArray array); |
| 273 | int GetViewsByAppUserModelId(string id, out IObjectArray array); |
| 274 | int GetViewForHwnd(IntPtr hwnd, out IApplicationView view); |
| 275 | int GetViewForApplication(object application, out IApplicationView view); |
| 276 | int GetViewForAppUserModelId(string id, out IApplicationView view); |
| 277 | int GetViewInFocus(out IntPtr view); |
| 278 | int Unknown1(out IntPtr view); |
| 279 | void RefreshCollection(); |
| 280 | int RegisterForApplicationViewChanges(object listener, out int cookie); |
| 281 | int UnregisterForApplicationViewChanges(int cookie); |
| 282 | } |
| 283 | |
| 284 | [ComImport] |
| 285 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 286 | [Guid("4CE81583-1E4C-4632-A621-07A53543148F")] |
| 287 | internal interface IVirtualDesktopPinnedApps |
| 288 | { |
| 289 | bool IsAppIdPinned(string appId); |
| 290 | void PinAppID(string appId); |
| 291 | void UnpinAppID(string appId); |
| 292 | bool IsViewPinned(IApplicationView applicationView); |
| 293 | void PinView(IApplicationView applicationView); |
| 294 | void UnpinView(IApplicationView applicationView); |
| 295 | } |
| 296 | |
| 297 | [ComImport] |
| 298 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 299 | [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")] |
| 300 | internal interface IServiceProvider10 |
| 301 | { |
| 302 | [return: MarshalAs(UnmanagedType.IUnknown)] |
| 303 | object QueryService(ref Guid service, ref Guid riid); |
| 304 | } |
| 305 | |
| 306 | #endregion Virtual Desktop APIs |
| 307 | |
| 308 | [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] |
| 309 | private static extern IntPtr GetCommandLineW(); |
| 310 | |
| 311 | |
| 312 | #region Window Functions |
| 313 | |
| 314 | // Delegate for EnumWindows callback |
| 315 | internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); |
| 316 | |
| 317 | [DllImport("user32.dll")] |
| 318 | [return: MarshalAs(UnmanagedType.Bool)] |
| 319 | static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); |
| 320 | |
| 321 | [DllImport("user32.dll", CharSet = CharSet.Unicode)] |
| 322 | static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); |
| 323 | |
| 324 | [DllImport("user32.dll")] |
| 325 | [return: MarshalAs(UnmanagedType.Bool)] |
| 326 | static extern bool IsWindowVisible(IntPtr hWnd); |
| 327 | |
| 328 | [DllImport("user32.dll")] |
| 329 | static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); |
| 330 | |
| 331 | // get handle of active window |
| 332 | [DllImport("user32.dll")] |
| 333 | private static extern IntPtr GetForegroundWindow(); |
| 334 | |
| 335 | #endregion Window Functions |
| 336 | |
| 337 | [DllImport("shell32.dll", CharSet = CharSet.Unicode)] |
| 338 | private static extern IntPtr ShellExecute( |
| 339 | IntPtr hwnd, |
| 340 | string lpOperation, |
| 341 | string lpFile, |
| 342 | string lpParameters, |
| 343 | string lpDirectory, |
| 344 | int nShowCmd); |
| 345 | |
| 346 | |
| 347 | [DllImport("combase.dll")] |
| 348 | internal static extern int WindowsCreateString(char* sourceString, int length, out IntPtr hstring); |
| 349 | |
| 350 | [DllImport("combase.dll")] |
| 351 | internal static extern int WindowsDeleteString(IntPtr hstring); |
| 352 | |
| 353 | [DllImport("combase.dll")] |
| 354 | internal static extern char* WindowsGetStringRawBuffer(IntPtr hstring, out uint length); |
| 355 | |
| 356 | // Add these COM interface definitions for Radio Management API |
| 357 | |
| 358 | // GUIDs for Radio Management API |
| 359 | internal static readonly Guid CLSID_RadioManagementAPI = new Guid(0x581333f6, 0x28db, 0x41be, 0xbc, 0x7a, 0xff, 0x20, 0x1f, 0x12, 0xf3, 0xf6); |
| 360 | internal static readonly Guid IID_IRadioManager = new Guid(0xdb3afbfb, 0x08e6, 0x46c6, 0xaa, 0x70, 0xbf, 0x9a, 0x34, 0xc3, 0x0a, 0xb7); |
| 361 | |
| 362 | [ComImport] |
| 363 | [Guid("db3afbfb-08e6-46c6-aa70-bf9a34c30ab7")] |
| 364 | [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 365 | internal interface IRadioManager |
| 366 | { |
| 367 | [PreserveSig] |
| 368 | int IsRMSupported(out uint pdwState); |
| 369 | |
| 370 | [PreserveSig] |
| 371 | int GetUIRadioInstances([MarshalAs(UnmanagedType.IUnknown)] out object ppCollection); |
| 372 | |
| 373 | [PreserveSig] |
| 374 | int GetSystemRadioState(out int pbEnabled, out int param2, out int pChangeReason); |
| 375 | |
| 376 | [PreserveSig] |
| 377 | int SetSystemRadioState(int bEnabled); |
| 378 | |
| 379 | [PreserveSig] |
| 380 | int Refresh(); |
| 381 | |
| 382 | [PreserveSig] |
| 383 | int OnHardwareSliderChange(int param1, int param2); |
| 384 | } |
| 385 | |
| 386 | #region WiFi |
| 387 | |
| 388 | // WLAN API P/Invoke declarations |
| 389 | [DllImport("wlanapi.dll")] |
| 390 | static extern int WlanOpenHandle(uint dwClientVersion, IntPtr pReserved, out uint pdwNegotiatedVersion, out IntPtr phClientHandle); |
| 391 | |
| 392 | [DllImport("wlanapi.dll")] |
| 393 | static extern int WlanCloseHandle(IntPtr hClientHandle, IntPtr pReserved); |
| 394 | |
| 395 | [DllImport("wlanapi.dll")] |
| 396 | static extern int WlanEnumInterfaces(IntPtr hClientHandle, IntPtr pReserved, out IntPtr ppInterfaceList); |
| 397 | |
| 398 | [DllImport("wlanapi.dll")] |
| 399 | static extern int WlanGetAvailableNetworkList(IntPtr hClientHandle, ref Guid pInterfaceGuid, uint dwFlags, IntPtr pReserved, out IntPtr ppAvailableNetworkList); |
| 400 | |
| 401 | [DllImport("wlanapi.dll")] |
| 402 | static extern int WlanScan(IntPtr hClientHandle, ref Guid pInterfaceGuid, IntPtr pDot11Ssid, IntPtr pIeData, IntPtr pReserved); |
| 403 | |
| 404 | [DllImport("wlanapi.dll")] |
| 405 | static extern void WlanFreeMemory(IntPtr pMemory); |
| 406 | |
| 407 | [DllImport("wlanapi.dll")] |
| 408 | static extern int WlanConnect(IntPtr hClientHandle, ref Guid pInterfaceGuid, ref WLAN_CONNECTION_PARAMETERS pConnectionParameters, IntPtr pReserved); |
| 409 | |
| 410 | [DllImport("wlanapi.dll")] |
| 411 | static extern int WlanDisconnect(IntPtr hClientHandle, ref Guid pInterfaceGuid, IntPtr pReserved); |
| 412 | |
| 413 | [DllImport("wlanapi.dll")] |
| 414 | static extern int WlanSetProfile(IntPtr hClientHandle, ref Guid pInterfaceGuid, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string strProfileXml, [MarshalAs(UnmanagedType.LPWStr)] string strAllUserProfileSecurity, bool bOverwrite, IntPtr pReserved, out uint pdwReasonCode); |
| 415 | |
| 416 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
| 417 | struct WLAN_INTERFACE_INFO |
| 418 | { |
| 419 | public Guid InterfaceGuid; |
| 420 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] |
| 421 | public string strInterfaceDescription; |
| 422 | public int isState; |
| 423 | } |
| 424 | |
| 425 | [StructLayout(LayoutKind.Sequential)] |
| 426 | struct WLAN_INTERFACE_INFO_LIST |
| 427 | { |
| 428 | public uint dwNumberOfItems; |
| 429 | public uint dwIndex; |
| 430 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] |
| 431 | public WLAN_INTERFACE_INFO[] InterfaceInfo; |
| 432 | } |
| 433 | |
| 434 | [StructLayout(LayoutKind.Sequential)] |
| 435 | struct DOT11_SSID |
| 436 | { |
| 437 | public uint SSIDLength; |
| 438 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] |
| 439 | public byte[] SSID; |
| 440 | } |
| 441 | |
| 442 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
| 443 | struct WLAN_AVAILABLE_NETWORK |
| 444 | { |
| 445 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] |
| 446 | public string strProfileName; |
| 447 | public DOT11_SSID dot11Ssid; |
| 448 | public int dot11BssType; |
| 449 | public uint uNumberOfBssids; |
| 450 | public bool bNetworkConnectable; |
| 451 | public uint wlanNotConnectableReason; |
| 452 | public uint uNumberOfPhyTypes; |
| 453 | [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] |
| 454 | public int[] dot11PhyTypes; |
| 455 | public bool bMorePhyTypes; |
| 456 | public uint wlanSignalQuality; |
| 457 | public bool bSecurityEnabled; |
| 458 | public int dot11DefaultAuthAlgorithm; |
| 459 | public int dot11DefaultCipherAlgorithm; |
| 460 | public uint dwFlags; |
| 461 | public uint dwReserved; |
| 462 | } |
| 463 | |
| 464 | [StructLayout(LayoutKind.Sequential)] |
| 465 | struct WLAN_AVAILABLE_NETWORK_LIST |
| 466 | { |
| 467 | public uint dwNumberOfItems; |
| 468 | public uint dwIndex; |
| 469 | } |
| 470 | |
| 471 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] |
| 472 | struct WLAN_CONNECTION_PARAMETERS |
| 473 | { |
| 474 | public WLAN_CONNECTION_MODE wlanConnectionMode; |
| 475 | [MarshalAs(UnmanagedType.LPWStr)] |
| 476 | public string strProfile; |
| 477 | public IntPtr pDot11Ssid; |
| 478 | public IntPtr pDesiredBssidList; |
| 479 | public DOT11_BSS_TYPE dot11BssType; |
| 480 | public uint dwFlags; |
| 481 | } |
| 482 | |
| 483 | enum WLAN_CONNECTION_MODE |
| 484 | { |
| 485 | wlan_connection_mode_profile = 0, |
| 486 | wlan_connection_mode_temporary_profile = 1, |
| 487 | wlan_connection_mode_discovery_secure = 2, |
| 488 | wlan_connection_mode_discovery_unsecure = 3, |
| 489 | wlan_connection_mode_auto = 4 |
| 490 | } |
| 491 | |
| 492 | enum DOT11_BSS_TYPE |
| 493 | { |
| 494 | dot11_BSS_type_infrastructure = 1, |
| 495 | dot11_BSS_type_independent = 2, |
| 496 | dot11_BSS_type_any = 3 |
| 497 | } |
| 498 | |
| 499 | #endregion WiFi |
| 500 | } |
| 501 | } |