8

I'm trying to write a program which creates hotspot. I'm using WlanHostedNetworkStartUsing but it returns ERROR_INVALID_STATE. And yet when I call WlanHostedNetworkInitSettings it returns succsess. According to documemtation (last paragraph in Remarks section) it should to create a virtual wireless connection under Control Panel\Network and Internet\Network and Sharing Center but it doesn't.

I've searching a bit and found this:

When I run netsh wlan show drivers it puts:

Driver                    : Intel(R) Dual Band Wireless-AC 3165
Vendor                    : Intel Corporation
Provider                  : Intel
Date                      : 07-Sep-16
Version                   : 19.20.0.6
INF file                  : ????
Type                      : Native Wi-Fi Driver
Radio types supported     : 802.11b 802.11g 802.11n 802.11a 802.11ac
/ ...
Hosted network supported  : No  <--- Here
/ ...

So it says my wifi adapter doesn't wifi sharing at all (I have last drivers from HP site).

BUT when I try to create hotspot with Windows 10 builtin' tool it works. Windows tool sample

The question: How could windows tool do it and how can I use this mechanism in my app?

vlad4378
  • 803
  • 1
  • 9
  • 21
  • 1
    It's hard to tell what you're doing wrong when you haven't posted your code. – Eric Brown Feb 28 '17 at 19:26
  • Vlad. Check out this: https://github.com/wekillpeople/hotspot-windows/blob/master/wlanhost.c -- The repository has just what you need, and good reference! – Droopy Mar 01 '17 at 07:59
  • I have asked a [similar question](https://stackoverflow.com/questions/47744767/how-to-set-up-windows-10-mobile-hotspot-programmatically). – niutech Dec 11 '17 at 00:03
  • That article referenced above on "wlanhost.c" only works for OLDER windows prior to Windows 8. After which they replaced WiFi with "WiFi Direct" apis. For that you need WinRT methods, as I described below. – smallscript Feb 28 '20 at 03:48

3 Answers3

11

Original 06/06/2018 comments here (see updates below):

Microsoft deprecated the WLAN HostedNetwork capability and it is NOT available for Win10 drivers. To use the old model in Win10 you must find and install drivers from 2015 (8.1 or possibly earlier depending on vendor).

The Win10 driver model changed the mechanism of HostedNetwork to be based on WiFi Direct, and took control away from app-developers and moved this feature to the kernel. There are some samples available if you dig around, that show how to use the modern-com (RT) UWP app libraries to configure a WiFi Direct HostedNetwork. It is a PITA, which was not explained by Microsoft, is not understood by most people commenting on this in the web, and which mostly looks like a two-step microsoft failure where product features were cut to make ship schedule and re-orgs among teams changed the ownership and plan for WiFi and hotspots. WiFi direct enables - theoretically - a simpler pairing and authentication model between devices. But the currently implementation involves bluetooth and therefore it is questionable other than support a limited mobile device WiFi 2.0 scenario. If you are working with headless devices or IoT device scenarios this is broken.

I've had to do a lot of work in this area. If you have a choice in WiFi hardware, I strongly recommend a hardware chipset that uses the Intel drivers (they are solid).

You may find this App store app helpful if your scenario allows for UX interaction. http://www.topuwp.com/windowsapps/wifi-direct-access-point/598084.html

====================

02/27/2020 Update to that story...

When Hosted network supported : No then legacy hosted network support is not available on your adapter because you have WiFi Direct in Windows 10 etc. In which case you'll want to know and use this very sparsely commented on supported portion of WiFi Direct:

https://learn.microsoft.com/en-us/uwp/api/windows.networking.networkoperators.networkoperatortetheringmanager.createfromconnectionprofile

Command Line to HotSpot settings: start ms-settings:network-mobilehotspot

Article that talks about PowerShell programmatic access to the WinRT HotSpot APIs

enable Win10 inbuild hotspot by cmd/batch/powershell

KEYWORDS: "Virtual Wi-Fi", SoftAP, AdHoc IBSS, MobileHotSpot, netsh wlan HostedNetwork

====================

Which would not be complete without a working C++/WinRT code sample as follows:

#include <winrt/Windows.Networking.Connectivity.h>
#include <winrt/Windows.Networking.NetworkOperators.h>
#include <winrt/Windows.Devices.WiFiDirect.h>
#include <winrt/Windows.Security.Credentials.h>
namespace winrt { // /ZW embed in :<winrt> when `Windows` is ambiguously defined
  static void af_winrt_wifi_hotspot_test() {
    // start ms-settings:network-mobilehotspot
    init_apartment(); // apartment_type::multi_threaded
    if (false /* play as you wish to test this all in simple c++ console app, I used clang */) {
      auto publisher = Windows::Devices::WiFiDirect::WiFiDirectAdvertisementPublisher();
      auto advertisement = publisher.Advertisement();
      advertisement.ListenStateDiscoverability(Windows::Devices::WiFiDirect::WiFiDirectAdvertisementListenStateDiscoverability::Intensive);
      advertisement.IsAutonomousGroupOwnerEnabled(true);
      auto legacySettings = advertisement.LegacySettings();
      legacySettings.IsEnabled(true);
      legacySettings.Ssid(L"your-hotspot-name");
      auto credential = Windows::Security::Credentials::PasswordCredential(); credential.Password(L"the-password!");
      legacySettings.Passphrase(credential);
      publisher.Start();
    }
    else {
      auto connectionProfile{ Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile() };
      auto tetheringManager = Windows::Networking::NetworkOperators::NetworkOperatorTetheringManager::CreateFromConnectionProfile(connectionProfile);
      auto credential = Windows::Security::Credentials::PasswordCredential(); credential.Password(L"the-password!");
      auto conf = Windows::Networking::NetworkOperators::NetworkOperatorTetheringAccessPointConfiguration();
      conf.Ssid(L"I-Own-You"); conf.Passphrase(credential.Password());
      auto oldConf = tetheringManager.GetCurrentAccessPointConfiguration();
      auto oldSsid = oldConf.Ssid(); auto oldPwd = oldConf.Passphrase();
      tetheringManager.ConfigureAccessPointAsync(conf); // Sets new ssid/pwd here
      switch (tetheringManager.TetheringOperationalState()) {
      case Windows::Networking::NetworkOperators::TetheringOperationalState::Off: {
        auto ioAsync = tetheringManager.StartTetheringAsync();
        auto fResult = ioAsync.get();
        }                                                                                    
        break;
      case Windows::Networking::NetworkOperators::TetheringOperationalState::On: {
        // auto ioAsync = tetheringManager.StopTetheringAsync();
        // auto fResult = ioAsync.get();
        }                                                                                 
        break;
      case Windows::Networking::NetworkOperators::TetheringOperationalState::InTransition:
      default:
        break;
      }
    }        
    clear_factory_cache();
    uninit_apartment();
  }
}

Look here for older Microsoft Samples relating to WiFiDirectAdvertisementPublisher:

So many articles on the web, so much confusion created by WiFi-Direct.

I've spent two whole days figuring it all out. Which, for my time, is a lot.

No excuse for Microsoft (where I use to work as an Architect) not having created a Blog about this very popular topic. Let alone simply having made netsh and Ad Hoc Wifi compat support, instead of leaving it so cryptic and confusing for devops, end-users, and developers.

-- enjoy David

The above is pretty concise, and exposes working c++/WinRT code for all scenarios.

[I now have this bundled in EdgeS: EdgeShell/EdgeScript/afm-scm toolset] [enter image description here]5

smallscript
  • 643
  • 7
  • 13
  • Wow. Thank you so much for the update with solutions. I agree, WiFi Direct is so poorly documented, and I've experienced issues getting non-legacy WiFi Direct behavior working between Windows and Linux. As a result, sorely need a reliable solution so I'm going back to legacy functionality. Thanks again. – schumacher574 Jul 05 '21 at 16:30
-1

Your computer does not support hosted network.

Because of that, this won't work.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
-2

Open command prompt as admin and try these commands:

netsh wlan set hostednetwork mode=allow ssid=“OSToto Hotspot” key=“12345678”

The ssid is the name of your network and the key is the password. You can name them like the above command.

Then run:

netsh wlan start hostednetwork

Rest before saying anything else, I would like to got through your source code.

BhanuSingh
  • 118
  • 1
  • 2
  • 15