How to convert char * to BSTR?

后端 未结 5 1274
滥情空心
滥情空心 2020-12-03 01:51

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);
  V         


        
相关标签:
5条回答
  • 2020-12-03 02:01

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

    0 讨论(0)
  • 2020-12-03 02:09

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

    0 讨论(0)
  • 2020-12-03 02:15

    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

    0 讨论(0)
  • 2020-12-03 02:18

    Use _bstr_t:

    _bstr_t bstrt(buffer);
    

    Here is the holy grail of string conversion articles

    0 讨论(0)
  • 2020-12-03 02:18

    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.

    0 讨论(0)
提交回复
热议问题