你会如何转换std::string成BSTR*?
STDMETHODIMP CMyRESTApp::rest(BSTR data, BSTR* restr)
{
RESTClient restclient;
RESTClient::response resp = restclient.get(data);
Log("Response Status code: %s", resp.code);
Log("Response Body: %s", resp.body);
*restr = // here
return S_OK;
}
Run Code Online (Sandbox Code Playgroud)
我需要转换resp.body,然后返回*restr此处.
基于ATL的方法是使用ATL::CComBSTR然后将Detach()(或CopyTo(...))结果CComBSTR用于BSTR*
就像是:
CComBSTR temp(stlstr.c_str());
*restr = temp.Detach();
Run Code Online (Sandbox Code Playgroud)
另外一般来说,std::basic_string您可以使用Win32 API Sys*系列函数,例如SysAllocStringByteLen和SysAllocString;
// For the `const char*` data type (`LPCSTR`);
*restr = SysAllocStringByteLen(stlstr.c_str(), stlstr.size());
Run Code Online (Sandbox Code Playgroud)
// More suitable for OLECHAR
*restr = SysAllocString(stlwstr.c_str());
Run Code Online (Sandbox Code Playgroud)
OLECHAR取决于目标平台,但通常是wchar_t.
鉴于您的代码,最短的代码片段可能就是;
*restr = SysAllocStringByteLen(resp.body.c_str(), resp.body.size());
Run Code Online (Sandbox Code Playgroud)
请注意,这些Windows API函数使用"常规"Windows代码页转换,如果需要,请参阅MSDN文档,了解如何控制它.
| 归档时间: |
|
| 查看次数: |
12132 次 |
| 最近记录: |