将BSTR转换为const char * [英] convert BSTR to const char*
问题描述
可能的重复项:
如何将char *转换为BSTR?
我们是C ++的新手,任何人都知道我如何将BSTR转换为const char *?
A BSTR
实际上是 WCHAR * code>带有长度前缀。
)。 BSTR
值指向字符串的开头,而不是长度前缀(它存储在字节
换句话说,您可以将 BSTR
虽然它是一个 const WCHAR *
。因此你的问题是:如何转换一个Unicode字符串( WCHAR *
)到 char *
?,答案是使用 = :: WideCharToMultiByte
API函数在这里。或者,如果您在应用程序中使用MFC / ATL,请使用 ATL和MFC转换宏。
Possible Duplicates:
Which is better code for converting BSTR parameters to ANSI in C/C++?
How to convert char * to BSTR?
Hi guys, I am very new to C++, any one have any idea how i can convert BSTR to const char*?
A BSTR
is actually a WCHAR*
with a length prefix. The BSTR
value points to the beginning of the string, not to the length prefix (which is stored in the bytes just "before" the location pointed to by the BSTR
).
In other words, you can treat a BSTR
as though it is a const WCHAR*
. No conversion necessary.
So your question is really: "How can I convert a Unicode string (WCHAR*
) to a char*
?" and the answer is to use the ::WideCharToMultiByte
API function as explained here. Or, if you are using MFC/ATL in your application, use the ATL and MFC Conversion Macros.
这篇关于将BSTR转换为const char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!