tags:

views:

5077

answers:

3

Hi,

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.

Can Anyone .. tell me how the solution for this :).

Thanks in advance : ) ..Joseph

+5  A: 

Use _bstr_t:

_bstr_t bstrt(buffer);

Here is the holy grail of string conversion articles

Aidan Ryan
_bstr_t is a class. Not useful for pure C, I suppose.
sharptooth
Yes u r right. Thanks
Great link, thanks!
Colen
A: 

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

Martin
Btw, casting a WCHAR* to BSTR is a bad idea too if the string has not been allocated with one of SysAllocString() family functions. Doing so may cause a crash if the call you pass such pseudo-BSTR calls SysStringLen() on it.
sharptooth
+5  A: 

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.

sharptooth
Still my code is not working, i have modified like this.void Cfunc(char *buffer,int len){ BSTR buf_bstr; ULONG cCharacters; if (0 == MultiByteToWideChar(0, 0, buffer, cCharacters, buf_bstr, cCharacters)); VBptr.VBfunc(buf_bstr,len);}VB Exe is crashed.
Hi Sharptooth.. i am new to this COM :(
I've edited the answer to include a step-by step how-to.
sharptooth
Wow ... It is working fine.. Thanks sharptooth :)....