如何转换的char *为BSTR? [英] How to convert char * to BSTR?

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

问题描述

如何传递一个char *从C DLL到VB

How can I pass a char * from C dll to VB

下面是示例code:

void Cfunc(char *buffer,int len)
{
  BSTR buf_bstr = SysAllocString((BSTR)buffer);
  VBptr.VBfunc(buf_bstr,len);
}

此功能不工作,在实际的一些其它值被发送给VB,而不是实际的数值。

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?

推荐答案

呼叫的MultiByteToWideChar(),然后要么SysAllocString()或SysAllocStringLen()。

Call MultiByteToWideChar(), then either SysAllocString() or SysAllocStringLen().

不要忘记调用SysFreeString()的时候,你不再需要BSTR。

Don't forget to call SysFreeString() when you no longer need the BSTR.

在细节(SysAllocStringLen()的变体 - 这是更短更快):

In detail (SysAllocStringLen() variant – it's shorter and faster):


  1. 呼叫的MultiByteToWideChar(),并通过0作为第五和第六的参数。将返回的统一code当量ANSI字符串的字符数。请记住,ANSI字符串可以包含任何字符,不仅ASCII,因此任何试图手动计算给出的ANSI字符串长度在某些情况下可能工作,而不是在别人工作。

  1. 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.

分配缓冲区与SysAllocStringLen的BSTR()。传递0作为第一个参数,作为第二个参数的统一code字符数。您现在有一个正确分配但未初始化BSTR。它已经为尾随零,这尾随零妥善安置到位。

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.

呼叫的MultiByteToWideChar()第二次,这次通过分配BSTR那里。该功能将转换成字符串统一code和结果复制到BSTR。现在你有一个包含统一code相当于你的ANSI字符串的propely分配的BSTR。

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.

传递BSTR到VB。享受。

Pass the BSTR into VB. Enjoy.

调用SysFreeString()解除分配BSTR。

Call SysFreeString() to deallocate the BSTR.

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

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