Search code examples
windowsqtwlanapi

QString Spanish accent issue


I have a program which connects to the wireless networks. It combines the Win API and Qt API. The problem is that it fails to connect for example to the network with SSID: Escritório. For other networks with only English characters it connects successfully. So, it seems the issue is with QString conversion containing this letter: ó.

Code:

QString securedAPProfile(QString profileName, QString apName, _DOT11_AUTH_ALGORITHM authAlgorithm, _DOT11_CIPHER_ALGORITHM encryption, QString password, bool hiddenAP, bool isAutoConnection)
{
    QString xmlProfile = QString("<?xml version=\"1.0\"?><WLANProfile xmlns=\"http://www.microsoft.com/networking/WLAN/profile/v1\"><name>%1</name><SSIDConfig><SSID><hex>%2</hex><name>%3</name></SSID><nonBroadcast>%4</nonBroadcast></SSIDConfig><connectionType>ESS</connectionType><connectionMode>%5</connectionMode><MSM><security><authEncryption><authentication>%6</authentication><encryption>%7</encryption><useOneX>false</useOneX></authEncryption><sharedKey><keyType>%8</keyType><protected>false</protected><keyMaterial>%9</keyMaterial></sharedKey></security></MSM></WLANProfile>").arg(profileName, getSSIDHex(apName), apName, checkHiddenAP(hiddenAP), checkAutoConnection(isAutoConnection), checkAuthentication(authAlgorithm), checkEncryption(encryption), checkKeyType(encryption), password);
    return xmlProfile;
}

LPCWSTR wlanProfile = reinterpret_cast<LPCWSTR>(profileName.utf16());
DWORD dwResult = WlanSetProfile(hClient, &adapterGUID, NULL, reinterpret_cast<LPCWSTR>(securedAPProfile(profileName, apName, authAlgorithm, encryption, password, hiddenAP, isAutoConnection).utf16()), nullptr, TRUE, nullptr, &wlanReasonCode);

WLAN_CONNECTION_PARAMETERS connectionParameters;
memset(&connectionParameters, NULL, sizeof(WLAN_CONNECTION_PARAMETERS));
connectionParameters.wlanConnectionMode = wlan_connection_mode_profile;
connectionParameters.strProfile = wlanProfile;

It displays the following error: "The specific network is not available. (163851)" and does not connect to this network. Using Windows interface, it successfully connects. I have tried to use different QString methods including the QTextCodec::codecForName method with UTF-8/Windows-1251 encoding.

QString xmlProfileName = QTextCodec::codecForName("Windows-1251")->toUnicode(profileName.toLocal8Bit());
QString xmlAPName = QTextCodec::codecForName("Windows-1251")->toUnicode(apName.toLocal8Bit());

Then set these variables to the profile as arguments but no result: It leads to WlanConnect: 87 code, which means that wireless profile is not valid or could be corrupted. Any ideas how to convert QString with ó character (or similar Spanish accent characters) to LPCWSTR? What encoding should I use to fix this issue? Thank you.


Solution

  • Finally! I have fixed this issue. The problem was with wrong SSID hex encoding in the wireless profile.

    Code:

    QString getHex(QString ssid)
    {
        const char hexDigits[] = "0123456789ABCDEF";
        std::string hexSSID;
        hexSSID.reserve(ssid.toStdString().length() * 2);
    
        for (unsigned char ssidChar : ssid.toStdString()) {
             hexSSID.push_back(hexDigits[ssidChar >> 4]);
             hexSSID.push_back(hexDigits[ssidChar & 15]);
        }
    
        return QString::fromStdString(hexSSID).toUpper();
    }
    

    Now it connects to all networks names - Spanish/Cyrillic/English. The issue is resolved.