How to convert char * to BSTR?

Joseph_blr picture Joseph_blr · Mar 3, 2009 · Viewed 40.6k times · Source

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?

Answer

Aidan Ryan picture Aidan Ryan · Mar 3, 2009

Use _bstr_t:

_bstr_t bstrt(buffer);

Here is the holy grail of string conversion articles