win10 Hotspot Programming

// need winrt library
#include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.Networking.Connectivity.h> #include <winrt/Windows.Networking.NetworkOperators.h> using namespace winrt; using namespace Windows::Foundation; void get_client(Windows::Networking::NetworkOperators::NetworkOperatorTetheringManager &wifiManager) { char ch = '\0'; while ('q' != ch) { auto listClients = wifiManager.GetTetheringClients(); if (listClients.Size() > 0) { for (auto x : listClients) { auto listHostNames = x.HostNames(); winrt::hstring mac = x.MacAddress(); printf("mac=%ls\n", mac.c_str()); for (auto host : listHostNames) { winrt::hstring name = host.DisplayName(); // show hostName & IP Address printf("HostName = %ls\n", name.c_str()); /*Windows::Networking::Connectivity::IPInformation info = host.IPInformation(); auto net = info.NetworkAdapter();*/ } } } scanf_s("%c", &ch, 1); } }

 

Windows::Networking::NetworkOperators::NetworkOperatorTetheringManager af_winrt_wifi_hotspot_test() {
    auto connectionProfile{ Windows::Networking::Connectivity::NetworkInformation::GetInternetConnectionProfile() };
    auto wifiManager = Windows::Networking::NetworkOperators::NetworkOperatorTetheringManager::CreateFromConnectionProfile(connectionProfile);
    switch (wifiManager.TetheringOperationalState()) {
    case Windows::Networking::NetworkOperators::TetheringOperationalState::Off:
    {
        auto access = wifiManager.GetCurrentAccessPointConfiguration();
        wchar_t ssid[64] = { '\0'};
        const char str[9] = "UVC_TEST";
        mbstowcs(ssid, str, 64);
        winrt::hstring hs(ssid);
        access.Ssid(hs);
        printf("ssid=%ls\n", ssid);
        wchar_t passwd[64] = { '\0'};
        const char str2[9] = "admin123";
        mbstowcs(passwd, str2, 64);
        winrt::hstring hs2(passwd);
        access.Passphrase(hs2);
        printf("passwd=%ls\n", passwd);
        wifiManager.ConfigureAccessPointAsync(access);

      auto ioAsync = wifiManager.StartTetheringAsync();
      auto fResult = ioAsync.get();
  }
  break;

 

case Windows::Networking::NetworkOperators::TetheringOperationalState::On:
  {
    auto ioAsync = wifiManager.StopTetheringAsync();
    auto fResult = ioAsync.get();
  }
  break;
  case Windows::Networking::NetworkOperators::TetheringOperationalState::InTransition:
  default:
  break;
  }
  return wifiManager;
}

int main()
{
  printf("Start Task...\n");
  init_apartment();
  auto wifiManager = af_winrt_wifi_hotspot_test();
  get_client(wifiManager);

  clear_factory_cache();
  uninit_apartment();
  printf("Done.\n");

  system("pause");
}

 

posted @ 2022-05-09 16:14  straymonkey  阅读(204)  评论(0)    收藏  举报