在C中将BSTR转换为CHAR *并将CHAR *转换为BSTR [英] Convert BSTR to CHAR* and CHAR* to BSTR in C

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

问题描述

我必须在C中将BSTR转换为CHAR *数据类型。无论如何,不​​使用VC ++库或函数就可以做到这一点。代码必须使用纯C语言。我认为'comutil.h'具有功能,但我想它们使用C ++。

I have to convert a BSTR to CHAR* datatype in C. Is there anyway to do this without using VC++ libraries or functions. The code has to be in pure C. I think 'comutil.h' has got functions but they are in C++ I suppose.

另外,关于SysAllocString函数,可以我们在C语言中使用它并转换为CHAR *?。请帮忙。

Also, about the SysAllocString function, can we use it in C and convert to CHAR*?. Please help.

谢谢,
sveerap

Thanks, sveerap

推荐答案

您可以使用SysAllocStringStringByteLen()从char *转换为BSTR。但这仅在char *仅包含ASCII字符且不进行任何字符转换的情况下才是安全的。不会引起问题的情况将非常罕见。

You can go from a char* to a BSTR with SysAllocStringByteLen(). But this is only safe if the char* contains only ASCII characters, no character conversion takes place. This not causing problems would be pretty rare.

下一跳是MultiByteToWideChar(),除非您知道该字符串已被另一个编码,否则您通常希望将CodePage参数设置为CP_ACP。办法。产生一个宽字符串,然后您可以将其传递给SysAllocString()以获取BSTR。

Next hop is MultiByteToWideChar(), you typically want to set the CodePage argument to CP_ACP unless you know that the string is encoded another way. That produces a wide string which you can then pass to SysAllocString() to get the BSTR.

另一种方法是WideCharToMultiByte(),除非直接传递BSTR即可,除非它包含嵌入式0个字符。无论如何,转换都是有损失的,要当心,当您使用包含重音符号的字符串或在您使用的代码页中无法表示的任何其他内容时,这可能会引起相当大的麻烦。

The other way around is WideCharToMultiByte(), passing the BSTR directly will work unless it contains embedded 0 chars. The conversion is lossy anyway however, beware the considerable headache this can cause when you work with strings that contain accented characters or anything else that cannot be represented in the code page you use.

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

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