Search code examples
c++windows-10wifi-directc++-winrtwrl

How to make the Make method of HString available


I am trying to connect my Surplex VR tracking shoes to my desktop via Wifi but unfortunately Microsoft in their infinite wisdom have depreciated the functionality to setup a simple Wifi hotspot.

The have provided some example C++ code that allows the creation of a "legacy" hotspot but it has one major flaw that makes it impractical to use - it generates a random password every time it is ran.

I am a Python novice that has never touched C++ before but I figured hard-coding something that is currently randomly generated would be straightforward but it seems like C++ has multiple different kinds of strings and methods are picky about what they are given?

This is the current block of code that handles the SSID and passcode:

    // Either specify a passphrase, or read the randomly generated one
    ComPtr<IPasswordCredential> passwordCredential;

    hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
    if (FAILED(hr))
    {
        throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
    }

    if (_passphraseProvided)
    {
        hr = hstrPassphrase.Set(_passphrase.c_str());
        if (FAILED(hr))
        {
            throw WlanHostedNetworkException("Failed to create HSTRING representation for Passphrase", hr);
        }

        hr = passwordCredential->put_Password(hstrPassphrase.Get());
        if (FAILED(hr))
        {
            throw WlanHostedNetworkException("Set Passphrase for WiFiDirectLegacySettings failed", hr);
        }
    }
    else
    {
        hr = passwordCredential->get_Password(hstrPassphrase.GetAddressOf());
        if (FAILED(hr))
        {
            throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
        }

        _passphrase = hstrPassphrase.GetRawBuffer(nullptr);
    }

And this is what I thought I would need to replace it with to make it functional

    HString hstrSSID = HString::Make(L"surplex-io");
    HString hstrPassphrase = HString::Make(L"abcd1234");
    _ssid = hstrSSID.GetRawBuffer(nullptr);
    _passphrase = hstrPassphrase.GetRawBuffer(nullptr);

It was suggested I could use the Make method to turn a regular old string into the HString required by the rest of the code. However I am repeatedly told

'Make': is not a member of 'Microsoft::WRL::Wrappers::HString'

Some investigation suggested the Make method of HString could be found in the icrosoft.WRL.Wrappers.h header file which is included in the Windows 10 SDK and I'd need to include that. But the Windows SDK does not include a file of this name so I'm at a bit of a loss.

The original unmodified demo code can be found here: https://github.com/zig13/WiFiDirectLegacySurplex I hope to hardcode the SSID and passcode to the defaults used by Surplex* shoes and make it available on Github for others in the same position. *Although I will probably set a different password for my own use


Solution

  • Just remove the conditional checking for passphrase being provided and use that branch to always set password, .c_str() can be replaced by a string literal, L"password".

    ComPtr<IPasswordCredential> passwordCredential;
    
    hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
    if (FAILED(hr))
    {
        throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
    }
    
    hr = hstrPassphrase.Set(L"Hardcoded Password");
    if (FAILED(hr))
    {
    throw WlanHostedNetworkException("Failed to create HSTRING representation for Passphrase", hr);
    }
    
    hr = passwordCredential->put_Password(hstrPassphrase.Get());
    if (FAILED(hr))
    {
    throw WlanHostedNetworkException("Set Passphrase for WiFiDirectLegacySettings failed", hr);
    }