locked
wlanconnect example? RRS feed

  • Question

  • Hi there, I'm writing something in VS C++ to connect to an arbitrary wireless network that has no security. I am having trouble understanding how the wlanconnect(...) function is meant to be used. Does anyone have an example handy? I saw this:  

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/86d602d9-8bfe-4d4b-8dc0-758f5c391041/wlanconnect-in-wireless-api?forum=netfxnetcom

    However, they don't say in here how to specify the wifi name.

    Thanks in advance.

    Tuesday, March 7, 2017 7:00 PM

Answers

  • The first parameter of WlanEnumInterfaces is the handle returned by WlanOpenHandle
    • Marked as answer by Enrac Tuesday, March 7, 2017 11:29 PM
    Tuesday, March 7, 2017 11:01 PM

All replies

  • This works for me (tested on Windows 10) :

    (pNetworkList is returned by WlanGetAvailableNetworkList(),

    hWlan from WlanOpenHandle(),

    interfaceGuid from WlanEnumInterfaces())

    DWORD dwResult = 0;
    WLAN_CONNECTION_PARAMETERS cp;
    memset(&cp, 0, sizeof(WLAN_CONNECTION_PARAMETERS));
    cp.wlanConnectionMode = wlan_connection_mode_profile;
    cp.strProfile = pNetworkList->Network[i].strProfileName;
    cp.dwFlags = 0;
    cp.pDot11Ssid = NULL;
    cp.pDesiredBssidList = 0;
    cp.dot11BssType = pNetworkList->Network[i].dot11BssType;
    dwResult = WlanConnect(hWlan, &interfaceGuid, &cp, NULL);


    Tuesday, March 7, 2017 8:33 PM
  • Okay, so this is what I have, but am seeing a failure:

    ---- SNIP ----

    HANDLE wlanHandle;
    unsigned long nv;
    WlanOpenHandle(1, NULL, &nv, &wlanHandle);

    DWORD dwResult = 0;
    WLAN_CONNECTION_PARAMETERS cp;
    memset(&cp, 0, sizeof(WLAN_CONNECTION_PARAMETERS));

    cp.wlanConnectionMode = wlan_connection_mode_profile;
    cp.strProfile = convertCharArrayToLPCWSTR(scannerName); 
    cp.dwFlags = 0;
    cp.pDot11Ssid = NULL;
    cp.pDesiredBssidList = 0;
    cp.dot11BssType = dot11_BSS_type_independent;

    HANDLE hClient = NULL;
    PWLAN_INTERFACE_INFO_LIST pIfList = NULL;
    PWLAN_INTERFACE_INFO pIfInfo = NULL;

    // only use the first wifi interface
    pIfInfo = (WLAN_INTERFACE_INFO *)&pIfList->InterfaceInfo[0];

    dwResult = WlanEnumInterfaces(hClient, NULL, &pIfList);

    if (dwResult == ERROR_SUCCESS)
    {
       dwResult = WlanConnect(wlanHandle, &(pIfInfo->InterfaceGuid), &cp, NULL);

       if (dwResult == ERROR_SUCCESS)
       {
          connected = true;
       }
    }

    ---- SNIP ----

    WlanEnumInterfaces(...) returns a status code of 87, which is invalid parameter. Any ideas why?



    • Edited by Enrac Tuesday, March 7, 2017 10:34 PM
    Tuesday, March 7, 2017 10:33 PM
  • The first parameter of WlanEnumInterfaces is the handle returned by WlanOpenHandle
    • Marked as answer by Enrac Tuesday, March 7, 2017 11:29 PM
    Tuesday, March 7, 2017 11:01 PM
  • D'OH! That'll teach me. Thank you :)
    Tuesday, March 7, 2017 11:30 PM
  • Hi there, one more question, I'm now seeing WlanConnect(...) returning an 87, any ideas why?
    Tuesday, March 7, 2017 11:47 PM
  • WLAN_CONNECTION_PARAMETERS cp;
    memset(&cp, 0, sizeof(WLAN_CONNECTION_PARAMETERS));
    cp.wlanConnectionMode = wlan_connection_mode_profile;
    cp.strProfile = pNetworkList->Network[i].strProfileName;
    cp.dot11BssType = dot11_BSS_type_any;
    
    DWORD dwResult = WlanConnect(hWlan, &interfaceGuid, &cp, NULL);
    Thanks Castorix31

    Tuesday, June 16, 2020 1:14 PM