【转】Converting char array to BSTR

转自:http://forums.devx.com/archive/index.php/t-86391.html

---------------------------------------------------------------------1楼------------------------------------------------------------------------

How can I convert a char array to a BSTR variable?

I have tried using the SysAllocString() function and the syntax is correct.
However, when I call the function from VB I get a GPF.

Here is an example

void __stdcall TestStr ( BSTR var1)
{
char var2[50];

// pass the pointer to the char array.
var1 = SysAllocString(*var2)
}


any input would be appriciated.

Chris
zak
08-17-2000, 04:42 AM

---------------------------------------------------------------------2楼 ------------------------------------------------------------------------

Hi chris,


Here is an example that show how to convert an array of char to a BSTR:

void __stdcall TestStr(BSTR bstr)
{
CComBSTR str(OLESTR(
"My name is prince and I am funky"));
bstr 
= str.Detach();
}


That is all you need to do to convert an array of char to a BSTR.
Don't forget to use CComBSTR. It is a smart BSTR class and so deals with
all the intracacies of BSTR that we mere programers we forget to follow.


There is an iteresting discussion about CComBSTR in the 'ATL Internals' written
by brent rector and chris sells.
 
原文地址:https://www.cnblogs.com/fzzl/p/2151446.html