使 HString 的 Make 方法可用。

huangapple go评论76阅读模式
英文:

How to make the Make method of HString available

问题

Here is the translated code portion you requested:

我正在尝试通过Wifi将Surplex VR跟踪鞋连接到我的台式电脑,但不幸的是,微软在其无穷的智慧中已经弃用了设置简单Wifi热点功能的功能。

他们提供了一些示例C++代码,允许创建一个“传统”热点,但它有一个主要缺陷,使其无法使用 - 每次运行时都会生成一个随机密码。

我是一个从未碰过C++的Python初学者,但我想硬编码当前随机生成的内容似乎是很简单的,但似乎C++有多种不同类型的字符串,方法对于它们得到的内容挑剔?

这是处理SSID和密码的当前代码块:

// 要么指定一个密码,要么读取随机生成的一个
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);
}

这是我认为需要替换以使其正常运行的部分:

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

建议我可以使用Make方法将普通的字符串转换为代码的其余部分所需的HString。但我一直被告知
> 'Make':不是'Microsoft::WRL::Wrappers::HString'的成员

一些调查表明HString的Make方法可以在Microsoft.WRL.Wrappers.h头文件中找到,该头文件包含在Windows 10 SDK中并且需要包含它。但是Windows SDK不包括这个名称的文件,所以我有点不知所措。

原始未修改的演示代码可以在此处找到:
https://github.com/zig13/WiFiDirectLegacySurplex
我希望将SSID和密码硬编码到Surplex*鞋子使用的默认值,并在Github上提供给其他处于相同位置的人使用。*尽管我可能会为自己使用设置不同的密码。


<details>
<summary>英文:</summary>

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 &quot;legacy&quot; 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&lt;IPasswordCredential&gt; passwordCredential;

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

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

    hr = passwordCredential-&gt;put_Password(hstrPassphrase.Get());
    if (FAILED(hr))
    {
        throw WlanHostedNetworkException(&quot;Set Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
    }
}
else
{
    hr = passwordCredential-&gt;get_Password(hstrPassphrase.GetAddressOf());
    if (FAILED(hr))
    {
        throw WlanHostedNetworkException(&quot;Get Passphrase for WiFiDirectLegacySettings failed&quot;, 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&quot;surplex-io&quot;);
HString hstrPassphrase = HString::Make(L&quot;abcd1234&quot;);
_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
&gt; &#39;Make&#39;: is not a member of &#39;Microsoft::WRL::Wrappers::HString&#39;

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&#39;d need to include that. But the Windows SDK does not include a file of this name so I&#39;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

</details>


# 答案1
**得分**: 1

只移除对提供密码短语的条件检查,并始终使用该分支来设置密码,`.c_str()` 可以替换为字符串字面量 `L"password"`。

```cpp
ComPtr<IPasswordCredential> passwordCredential;

hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
if (FAILED(hr))
{
    throw WlanHostedNetworkException("获取 WiFiDirectLegacySettings 的密码短语失败", hr);
}

hr = hstrPassphrase.Set(L"硬编码密码");
if (FAILED(hr))
{
    throw WlanHostedNetworkException("创建密码短语的 HSTRING 表示失败", hr);
}

hr = passwordCredential->put_Password(hstrPassphrase.Get());
if (FAILED(hr))
{
    throw WlanHostedNetworkException("设置 WiFiDirectLegacySettings 的密码短语失败", hr);
}
英文:

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&quot;password&quot;.

ComPtr&lt;IPasswordCredential&gt; passwordCredential;

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

hr = hstrPassphrase.Set(L&quot;Hardcoded Password&quot;);
if (FAILED(hr))
{
throw WlanHostedNetworkException(&quot;Failed to create HSTRING representation for Passphrase&quot;, hr);
}

hr = passwordCredential-&gt;put_Password(hstrPassphrase.Get());
if (FAILED(hr))
{
throw WlanHostedNetworkException(&quot;Set Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
}

huangapple
  • 本文由 发表于 2023年5月13日 15:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定