如何将 char * 转换为 BSTR?
如何将 char * 从 C dll 传递到 VB
以下是示例代码:
void Cfunc(char *buffer,int len)
{
BSTR buf_bstr = SysAllocString((BSTR)buffer);
VBptr.VBfunc(buf_bstr,len);
}
该函数不起作用,实际上一些其他值被发送到 VB 而不是实际值。
有人可以提出解决方案吗?
How can I pass a char * from C dll to VB
Here is sample code:
void Cfunc(char *buffer,int len)
{
BSTR buf_bstr = SysAllocString((BSTR)buffer);
VBptr.VBfunc(buf_bstr,len);
}
This function is not working, In actual some other values are sent to the VB rather than the actual value.
Could anyone please suggest a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
_bstr_t
:这是字符串转换文章的圣杯
Use
_bstr_t
:Here is the holy grail of string conversion articles
调用
MultiByteToWideChar()
,然后调用SysAllocString()
或SysAllocStringLen()
。当您不再需要 BSTR 时,请不要忘记调用
SysFreeString()
。详细信息(
SysAllocStringLen()
变体 - 它更短且更快):调用
MultiByteToWideChar()
并传递 0 作为第五个和第六个参数。 它将返回 ANSI 字符串的 Unicode 等效字符数。 请记住,ANSI 字符串可以包含任何字符,而不仅仅是 ASCII,因此任何在给定 ANSI 字符串长度的情况下手动计算 Unicode 字符数的尝试可能在某些情况下有效,但在其他情况下无效。使用
SysAllocStringLen()
为 BSTR 分配缓冲区。 传递 0 作为第一个参数,传递 Unicode 字符数作为第二个参数。 您现在拥有一个正确分配但未初始化的 BSTR。 它已经有用于尾随零的位置,并且该尾随零已正确放置。第二次调用
MultiByteToWideChar()
,这次将分配的BSTR传递给那里。 该函数会将字符串转换为 Unicode 并将结果复制到 BSTR 中。 现在您已经有了一个正确分配的 BSTR,其中包含 ANSI 字符串的 Unicode 等效项。将 BSTR 传递到 VB。 享受吧。
调用
SysFreeString()
来释放BSTR。Call
MultiByteToWideChar()
, then eitherSysAllocString()
orSysAllocStringLen()
.Don't forget to call
SysFreeString()
when you no longer need the BSTR.In detail (
SysAllocStringLen()
variant – it's shorter and faster):Call
MultiByteToWideChar()
and pass 0 as fifth and sixth parameters. It will return the number of characters in the Unicode equivalent of the ANSI string. Remember, ANSI string can contain whatever characters, not only ASCII, so any attempts to manually calculate the number of Unicode characters given the ANSI string length may work in some cases and not work in others.Allocate a buffer for the BSTR with
SysAllocStringLen()
. Pass 0 as the first parameter and the number of Unicode characters as the second parameter. You now have a properly allocated but uninitialized BSTR. It already has place for the trailing zero and this trailing zero is properly placed.Call
MultiByteToWideChar()
second time and this time pass the allocated BSTR there. The function will convert the string into Unicode and copy the result into the BSTR. Now you have a propely allocated BSTR containing the Unicode equivalent of your ANSI string.Pass the BSTR into VB. Enjoy.
Call
SysFreeString()
to deallocate the BSTR.这是我使用 sharptooths 答案编写的代码
请注意,使用 -1 作为字符串的长度会导致结果中包含空终止符
This is the code I wrote using sharptooths answer
Note that using -1 for the length of the string results in the null terminator being included in the result
我对 ajryan 的回答没有任何异议,但这里有一个替代方案...
SysAllocString 被定义为采用 OLECHAR * 类型的参数。 你给它一个 char * 。 这些不是同一件事。 在某些情况下,它们可能是同一件事,但您不能依赖它。 因此,首先您需要将 char * 转换为 OLECHAR *。 有一个名为 A2OLE 的宏可以为您执行此操作,并且在 char * 和 OLECHAR * 相同的情况下,该宏将编译为空(我认为)。
有关 A2OLE 及其朋友的详细信息,请参阅此页面。
哦,将 char * 转换为 BSTR 实际上根本不会改变它,它既不是 BSTR 也不是 OLECHAR *。
I don't have any objection to ajryan's answer, but here's an alternative...
SysAllocString is defined to take a parameter of type OLECHAR *. You're giving it a char *. These are not the same thing. There are certain circumstances when they might be the same thing, but you can't depend on it. So first of all you need to convert your char * into an OLECHAR *. There is a macro called A2OLE that can do this for you, and in those cases where char * and OLECHAR * are the same thing, the macro compiles away to nothing (I think).
See this page for details of A2OLE and its friends.
Oh, and casting your char * to a BSTR doesn't actually change it at all, it is neither a BSTR nor an OLECHAR *.
尝试使用 _tchar* 数组代替 char* 数组。因为 Sysallocstring 在 32 位应用程序中仅接受 Unicode 字符。
instead of char* array,try _tchar* array.Because Sysallocstring takes only the Unicode characters in a 32 bit application.