使 HString 的 Make 方法可用。

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

How to make the Make method of HString available

问题

Here is the translated code portion you requested:

  1. 我正在尝试通过WifiSurplex VR跟踪鞋连接到我的台式电脑,但不幸的是,微软在其无穷的智慧中已经弃用了设置简单Wifi热点功能的功能。
  2. 他们提供了一些示例C++代码,允许创建一个“传统”热点,但它有一个主要缺陷,使其无法使用 - 每次运行时都会生成一个随机密码。
  3. 我是一个从未碰过C++的Python初学者,但我想硬编码当前随机生成的内容似乎是很简单的,但似乎C++有多种不同类型的字符串,方法对于它们得到的内容挑剔?
  4. 这是处理SSID和密码的当前代码块:
  5. // 要么指定一个密码,要么读取随机生成的一个
  6. ComPtr<IPasswordCredential> passwordCredential;
  7. hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
  8. if (FAILED(hr))
  9. {
  10. throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
  11. }
  12. if (_passphraseProvided)
  13. {
  14. hr = hstrPassphrase.Set(_passphrase.c_str());
  15. if (FAILED(hr))
  16. {
  17. throw WlanHostedNetworkException("Failed to create HSTRING representation for Passphrase", hr);
  18. }
  19. hr = passwordCredential->put_Password(hstrPassphrase.Get());
  20. if (FAILED(hr))
  21. {
  22. throw WlanHostedNetworkException("Set Passphrase for WiFiDirectLegacySettings failed", hr);
  23. }
  24. }
  25. else
  26. {
  27. hr = passwordCredential->get_Password(hstrPassphrase.GetAddressOf());
  28. if (FAILED(hr))
  29. {
  30. throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
  31. }
  32. _passphrase = hstrPassphrase.GetRawBuffer(nullptr);
  33. }

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

  1. HString hstrSSID = HString::Make(L"surplex-io");
  2. HString hstrPassphrase = HString::Make(L"abcd1234");
  3. _ssid = hstrSSID.GetRawBuffer(nullptr);
  4. _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上提供给其他处于相同位置的人使用。*尽管我可能会为自己使用设置不同的密码。

  1. <details>
  2. <summary>英文:</summary>
  3. 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.
  4. 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.
  5. 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?
  6. This is the current block of code that handles the SSID and passcode:
  1. // Either specify a passphrase, or read the randomly generated one
  2. ComPtr&lt;IPasswordCredential&gt; passwordCredential;
  3. hr = _legacySettings-&gt;get_Passphrase(passwordCredential.GetAddressOf());
  4. if (FAILED(hr))
  5. {
  6. throw WlanHostedNetworkException(&quot;Get Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
  7. }
  8. if (_passphraseProvided)
  9. {
  10. hr = hstrPassphrase.Set(_passphrase.c_str());
  11. if (FAILED(hr))
  12. {
  13. throw WlanHostedNetworkException(&quot;Failed to create HSTRING representation for Passphrase&quot;, hr);
  14. }
  15. hr = passwordCredential-&gt;put_Password(hstrPassphrase.Get());
  16. if (FAILED(hr))
  17. {
  18. throw WlanHostedNetworkException(&quot;Set Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
  19. }
  20. }
  21. else
  22. {
  23. hr = passwordCredential-&gt;get_Password(hstrPassphrase.GetAddressOf());
  24. if (FAILED(hr))
  25. {
  26. throw WlanHostedNetworkException(&quot;Get Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
  27. }
  28. _passphrase = hstrPassphrase.GetRawBuffer(nullptr);
  29. }
  1. And this is what I thought I would need to replace it with to make it functional
  1. HString hstrSSID = HString::Make(L&quot;surplex-io&quot;);
  2. HString hstrPassphrase = HString::Make(L&quot;abcd1234&quot;);
  3. _ssid = hstrSSID.GetRawBuffer(nullptr);
  4. _passphrase = hstrPassphrase.GetRawBuffer(nullptr);
  1. 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.
  2. However I am repeatedly told
  3. &gt; &#39;Make&#39;: is not a member of &#39;Microsoft::WRL::Wrappers::HString&#39;
  4. 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.
  5. The original unmodified demo code can be found here:
  6. https://github.com/zig13/WiFiDirectLegacySurplex
  7. 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
  8. </details>
  9. # 答案1
  10. **得分**: 1
  11. 只移除对提供密码短语的条件检查,并始终使用该分支来设置密码,`.c_str()` 可以替换为字符串字面量 `L"password"`
  12. ```cpp
  13. ComPtr<IPasswordCredential> passwordCredential;
  14. hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
  15. if (FAILED(hr))
  16. {
  17. throw WlanHostedNetworkException("获取 WiFiDirectLegacySettings 的密码短语失败", hr);
  18. }
  19. hr = hstrPassphrase.Set(L"硬编码密码");
  20. if (FAILED(hr))
  21. {
  22. throw WlanHostedNetworkException("创建密码短语的 HSTRING 表示失败", hr);
  23. }
  24. hr = passwordCredential->put_Password(hstrPassphrase.Get());
  25. if (FAILED(hr))
  26. {
  27. throw WlanHostedNetworkException("设置 WiFiDirectLegacySettings 的密码短语失败", hr);
  28. }
英文:

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

  1. ComPtr&lt;IPasswordCredential&gt; passwordCredential;
  2. hr = _legacySettings-&gt;get_Passphrase(passwordCredential.GetAddressOf());
  3. if (FAILED(hr))
  4. {
  5. throw WlanHostedNetworkException(&quot;Get Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
  6. }
  7. hr = hstrPassphrase.Set(L&quot;Hardcoded Password&quot;);
  8. if (FAILED(hr))
  9. {
  10. throw WlanHostedNetworkException(&quot;Failed to create HSTRING representation for Passphrase&quot;, hr);
  11. }
  12. hr = passwordCredential-&gt;put_Password(hstrPassphrase.Get());
  13. if (FAILED(hr))
  14. {
  15. throw WlanHostedNetworkException(&quot;Set Passphrase for WiFiDirectLegacySettings failed&quot;, hr);
  16. }

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:

确定