locked
wlanconnect example? RRS feed

  • Pergunta

  • 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.

    terça-feira, 7 de março de 2017 19:00

Respostas

  • The first parameter of WlanEnumInterfaces is the handle returned by WlanOpenHandle
    • Marcado como Resposta Enrac terça-feira, 7 de março de 2017 23:29
    terça-feira, 7 de março de 2017 23:01

Todas as Respostas

  • 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);


    terça-feira, 7 de março de 2017 20:33
  • 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?



    • Editado Enrac terça-feira, 7 de março de 2017 22:34
    terça-feira, 7 de março de 2017 22:33
  • The first parameter of WlanEnumInterfaces is the handle returned by WlanOpenHandle
    • Marcado como Resposta Enrac terça-feira, 7 de março de 2017 23:29
    terça-feira, 7 de março de 2017 23:01
  • D'OH! That'll teach me. Thank you :)
    terça-feira, 7 de março de 2017 23:30
  • Hi there, one more question, I'm now seeing WlanConnect(...) returning an 87, any ideas why?
    terça-feira, 7 de março de 2017 23:47
  • 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

    terça-feira, 16 de junho de 2020 13:14