将BSTR转换为char * [英] Problems converting BSTR to char *

查看:293
本文介绍了将BSTR转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个旧的C ++应用程式,使用WinHttp.WinHttpRequest.5.1来呼叫第三方网路服务。

We've an old C++ app that's making calls to third-party webservices, using WinHttp.WinHttpRequest.5.1.

我不会列出所有的细节的调用序列,因为我不认为它与问题相关,但我们通过调用 hr = pIWinHttpRequest-> get_ResponseText(& bstrResponse); bstrResponse 的类型为BSTR。

I won't list all of the details of the call sequence, as I don't think it's relevant to the problem, but we finish by calling hr = pIWinHttpRequest->get_ResponseText(&bstrResponse);, where bstrResponse is of type BSTR.

调用代码不适用于BSTR,它与标准C / C ++ char * ,因此代码将BSTR转换为 char *

The calling code doesn't work with BSTRs, it works with standard C/C++ char *'s, so the code converts the BSTR to a char * with:

_bstr_t b(bstrResponse);
const char *c = static_cast<char *>(b);

对于我们使用此代码访问的所有以前的Web服务,但对于这个新的,它不是。

And for all of the prior webservices we've accessed with this code, this has worked. But for this new one, it's not.

我们回来的数据应该是XML,但对于这一个webservice,看起来像我们一些字符代码转换的问题。我们生成的字符串以; ?& lt;?xml version =1.0encoding =utf-8?& gt; ...

The data we're getting back is supposed to be XML, but for this one webservice, it looks like we're getting some character code conversion problems. Our resulting string starts with; "?&lt;?xml version="1.0" encoding="utf-8"?&gt;..."

注意额外的开头。当在调试器中遍历这个时,我们在 bstrResponse 的显示值中看不到这个,我们在显示的 b ,但我们确实看到 c 的显示值。

Notice the extra ? at the beginning. When walking through this in the debugger, we don't see this in displayed value of bstrResponse, and we don't see it in the displayed value of b, but we do see it in the displayed value of c.

关于可能发生什么的任何想法?

Any ideas as to what might be going on?

EDITED

我理解BSTR是一个多字节类型,但是此字符串中的所有字符都是纯ASCII,并且调用此函数的代码都不能处理多字节字符。浏览网页,我经常看到这个特定的机制,但在这种情况下,它不工作。

I understand that BSTR is a multi-byte type, but all of the characters in this string are plain ASCII, and none of the code that calls this function can handle multi-byte characters. Browsing around the web, I see this specific mechanism recommended frequently, but in this case, it doesn't work.

我需要将这个字符串从BSTR转换为数组的单字节字符。

I need to convert this string from BSTR to an array of single-byte characters. Even if that means stripping out multi-byte characters that cannot be converted.

推荐答案

使用<$ c $在代码中进行转换c> static_cast 在 _bstr_t 上正确转换为ANSI。在编码转换中出现表示字符转换失败。最可能的原因是 bstrResponse 包含ANSI代码页中不存在的字符。我希望你应该转换为UTF-8而不是ANSI,但是当然我没有你有的所有信息。

The conversion in your code using static_cast on a _bstr_t converts to ANSI correctly. The appearance of ? in an encoding conversion indicates that the conversion of a character failed. The most likely reason for this is that bstrResponse contains characters that are not present in your ANSI codepage. I would expect that you should be converting to UTF-8 rather than ANSI, but of course I don't have all the information that you have.

底线是表示源字符串包含无法在目标字符集中编码的字符。

The bottom line is that the ? indicates that the source string contains a character that cannot be encoded in the destination character set.

更新

您的答案进一步证明您应该转换为UTF-8。只有你能肯定知道,但你提出的证据与这个结论是一致的。

Your answer gives further evidence that you should be converting to UTF-8. Only you can know for sure, but the evidence you present is consistent with that conclusion.

这篇关于将BSTR转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆