How to convert char * to BSTR?

后端 未结 5 1290
滥情空心
滥情空心 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: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

提交回复
热议问题