How to convert char * to BSTR?

2019-01-11 06:55发布

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?

标签: c string com vb6 bstr
5条回答
We Are One
2楼-- · 2019-01-11 07:23

This is the code I wrote using sharptooths answer

    int wslen = MultiByteToWideChar(CP_ACP, 0, str, strlen(str), 0, 0);
    BSTR bstr = SysAllocStringLen(0, wslen);
    MultiByteToWideChar(CP_ACP, 0, str, strlen(str), bstr, wslen);
    // Use bstr here
    SysFreeString(bstr);

Note that using -1 for the length of the string results in the null terminator being included in the result

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-11 07:30

instead of char* array,try _tchar* array.Because Sysallocstring takes only the Unicode characters in a 32 bit application.

查看更多
淡お忘
4楼-- · 2019-01-11 07:34

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

查看更多
可以哭但决不认输i
5楼-- · 2019-01-11 07:43

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

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

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

  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.

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

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

  4. Pass the BSTR into VB. Enjoy.

  5. Call SysFreeString() to deallocate the BSTR.

查看更多
再贱就再见
6楼-- · 2019-01-11 07:46

Use _bstr_t:

_bstr_t bstrt(buffer);

Here is the holy grail of string conversion articles

查看更多
登录 后发表回答