Esempio n. 1
0
        public static WlanHostedNetworkStatus QueryStatus()
        {
            IntPtr ptr;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkQueryStatus
                                           (wlanHandle, out ptr, IntPtr.Zero));
            var netStat = Marshal.PtrToStructure <WlanHostedNetworkStatusTemp>(ptr);
            var stat    = new WlanHostedNetworkStatus();

            if ((stat.HostedNetworkState = netStat.HostedNetworkState) != WlanHostedNetworkState.Unavailable)
            {
                stat.IPDeviceID             = netStat.IPDeviceID;
                stat.wlanHostedNetworkBSSID = netStat.wlanHostedNetworkBSSID;
                if (netStat.HostedNetworkState == WlanHostedNetworkState.Active)
                {
                    stat.dot11PhyType       = netStat.dot11PhyType;
                    stat.ulChannelFrequency = netStat.ChannelFrequency;
                    stat.dwNumberOfPeers    = netStat.NumberOfPeers;
                    stat.PeerList           = new WlanHostedNetworkPeerState[stat.dwNumberOfPeers];
                    var offset = Marshal.SizeOf(typeof(WlanHostedNetworkStatusTemp));
                    for (var i = 0; i < netStat.NumberOfPeers; i++)
                    {
                        offset += Marshal.SizeOf(stat.PeerList[i] =
                                                     Marshal.PtrToStructure <WlanHostedNetworkPeerState>(new IntPtr(ptr.ToInt64() + offset)));
                    }
                }
            }
            return(stat);
        }
Esempio n. 2
0
        public static WlanHostedNetworkReason RefreshSecuritySettings()
        {
            WlanHostedNetworkReason failReason;

            Helper.ThrowExceptionForHR
                (WlanNativeMethods.WlanHostedNetworkRefreshSecuritySettings(wlanHandle, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 3
0
        public static WlanHostedNetworkReason SetSecondaryKey(byte[] passKey, bool isPersistent = true)
        {
            WlanHostedNetworkReason failReason;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkSetSecondaryKey(wlanHandle,
                                                                                          32, passKey, false, isPersistent, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 4
0
        private static WlanHostedNetworkReason InitSettings()
        {
            WlanHostedNetworkReason failReason;

            Helper.ThrowExceptionForHR
                (WlanNativeMethods.WlanHostedNetworkInitSettings(wlanHandle, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 5
0
        public static WlanHostedNetworkReason ForceStop()
        {
            WlanHostedNetworkReason failReason;

            Helper.ThrowExceptionForHR
                (WlanNativeMethods.WlanHostedNetworkForceStop(wlanHandle, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 6
0
        public static WlanHostedNetworkReason SetSecondaryKey(string passKey, bool isPersistent = true)
        {
            WlanHostedNetworkReason failReason;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkSetSecondaryKey(wlanHandle,
                                                                                          (uint)(passKey.Length + 1), Encoding.Default.GetBytes(passKey), true, isPersistent, out failReason,
                                                                                          IntPtr.Zero));
            return(failReason);
        }
Esempio n. 7
0
        public static WlanHostedNetworkReason QuerySecondaryKey(out string passKey, out bool isPassPhrase,
                                                                out bool isPersistent)
        {
            WlanHostedNetworkReason failReason;
            uint keyLen;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkQuerySecondaryKey(wlanHandle, out keyLen,
                                                                                            out passKey, out isPassPhrase, out isPersistent, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 8
0
        public static WlanHostedNetworkReason SetEnabled(bool enabled)
        {
            WlanHostedNetworkReason failReason;
            var settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(enabled));

            Marshal.StructureToPtr(enabled, settingsPtr, false);
            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkSetProperty(wlanHandle,
                                                                                      WlanHostedNetworkOpcode.Enable,
                                                                                      (uint)Marshal.SizeOf(enabled), settingsPtr, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 9
0
        public static WlanOpcodeValueType QueryEnabled(out bool enabled)
        {
            uint   dataSize;
            IntPtr dataPtr;
            WlanOpcodeValueType opcode;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkQueryProperty(wlanHandle,
                                                                                        WlanHostedNetworkOpcode.Enable,
                                                                                        out dataSize, out dataPtr, out opcode, IntPtr.Zero));
            enabled = Marshal.PtrToStructure <bool>(dataPtr);
            return(opcode);
        }
Esempio n. 10
0
        public static WlanOpcodeValueType QuerySecuritySettings(out WlanHostedNetworkSecuritySettings settings)
        {
            uint   dataSize;
            IntPtr dataPtr;
            WlanOpcodeValueType opcode;

            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkQueryProperty(wlanHandle,
                                                                                        WlanHostedNetworkOpcode.SecuritySettings,
                                                                                        out dataSize, out dataPtr, out opcode, IntPtr.Zero));
            settings = Marshal.PtrToStructure <WlanHostedNetworkSecuritySettings>(dataPtr);
            return(opcode);
        }
Esempio n. 11
0
        public static WlanHostedNetworkReason SetConnectionSettings(string hostedNetworkSsid, int maxNumberOfPeers)
        {
            WlanHostedNetworkReason failReason;
            var settings = new WlanHostedNetworkConnectionSettings
            {
                HostedNetworkSSID = ToDOT11_SSID(hostedNetworkSsid), MaxNumberOfPeers = (uint)maxNumberOfPeers
            };
            var settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(settings));

            Marshal.StructureToPtr(settings, settingsPtr, false);
            Helper.ThrowExceptionForHR(WlanNativeMethods.WlanHostedNetworkSetProperty(wlanHandle,
                                                                                      WlanHostedNetworkOpcode.ConnectionSettings,
                                                                                      (uint)Marshal.SizeOf(settings), settingsPtr, out failReason, IntPtr.Zero));
            return(failReason);
        }
Esempio n. 12
0
        public static WlanOpcodeValueType QueryStationProfile(out string profile)
        {
            uint   dataSize;
            IntPtr dataPtr;
            WlanOpcodeValueType opcode;
            var hr = WlanNativeMethods.WlanHostedNetworkQueryProperty(wlanHandle,
                                                                      WlanHostedNetworkOpcode.StationProfile,
                                                                      out dataSize, out dataPtr, out opcode, IntPtr.Zero);

            if (hr == 1610)
            {
                throw new BadConfigurationException();
            }
            Helper.ThrowExceptionForHR(hr);
            profile = Marshal.PtrToStringUni(dataPtr, (int)(dataSize >> 1));
            return(opcode);
        }
Esempio n. 13
0
        public static WlanOpcodeValueType QueryConnectionSettings(out WlanHostedNetworkConnectionSettings settings)
        {
            uint   dataSize;
            IntPtr dataPtr;
            WlanOpcodeValueType opcode;
            var hr = WlanNativeMethods.WlanHostedNetworkQueryProperty(wlanHandle,
                                                                      WlanHostedNetworkOpcode.ConnectionSettings,
                                                                      out dataSize, out dataPtr, out opcode, IntPtr.Zero);

            if (hr == 1610)
            {
                throw new BadConfigurationException();
            }
            Helper.ThrowExceptionForHR(hr);
            settings = Marshal.PtrToStructure <WlanHostedNetworkConnectionSettings>(dataPtr);
            return(opcode);
        }
Esempio n. 14
0
 public static void Restart()
 {
     lock (Callback)
     {
         try
         {
             if (wlanHandle != IntPtr.Zero)
             {
                 WlanNativeMethods.WlanCloseHandle(wlanHandle, IntPtr.Zero);
             }
         }
         catch { }   // who cares
         var failed = true;
         try
         {
             uint serverVersion;
             Helper.ThrowExceptionForHR(WlanNativeMethods.WlanOpenHandle(2, IntPtr.Zero, out serverVersion,
                                                                         out wlanHandle));
             // WLAN_CLIENT_VERSION_VISTA: Client version for Windows Vista and Windows Server 2008
             WlanNotificationSource notifSource;
             Helper.ThrowExceptionForHR(WlanNativeMethods.WlanRegisterNotification(wlanHandle,
                                                                                   WlanNotificationSource.All, true, Callback, IntPtr.Zero, IntPtr.Zero, out notifSource));
             var failReason = InitSettings();
             if (failReason != WlanHostedNetworkReason.Success)
             {
                 throw new Exception("Init Error WlanHostedNetworkInitSettings: " + failReason);
             }
             AppDomain.CurrentDomain.DomainUnload += (sender, e) =>
             {
                 if (wlanHandle != IntPtr.Zero)
                 {
                     Helper.ThrowExceptionForHR(WlanNativeMethods.WlanCloseHandle(wlanHandle, IntPtr.Zero));
                 }
             };
             failed = false;
         }
         finally
         {
             if (failed)
             {
                 WlanNativeMethods.WlanCloseHandle(wlanHandle, IntPtr.Zero);
             }
         }
     }
 }