Click to See Complete Forum and Search --> : Converting Strings


RubenVanStraten
June 29th, 1999, 08:21 AM
Hi,

I'm having a problem concerning the conversion of strings.
I need to convert a char* to a BSTR. I can't get this working correctly.
can somebody please help me?

Kind regards

Ruben van Straten

Radracr
June 29th, 1999, 11:02 AM
Try Using,

int iwstrLen = (strlen(szAnsiString) + 1) * 2;
WCHAR* pwString = new WCHAR[iwstrLen];

MultiByteToWideChar(CP_ACP, NULL,
szAnsiString, -1,
pwString, iwstrLen,
NULL, NULL);
BSTR bstrString = SysAllocString(pwString);
delete []pwString;




You can find more information and examples by searching MSDN for :
MutliByteToWideChar
or
WideCharToMultiByte


-Erik

chiuyan
June 29th, 1999, 11:07 AM
there is a macro defined
T2OLE
which takes a char* as its parameter and returns a BSTR, something like:
USES_CONVERSION; // defines some stuff that T2OLE needs
char* pString = "Some string";
BSTR bstrString = T2OLE(pString);

HTH
--michael

Todd Jeffreys
June 29th, 1999, 11:41 AM
You're code is wrong there. When you calculate the size of the string you don't have to multiply it by 2. The new operator can figure out the size, so therefore you don't have to account that WCHAR is 2 times as large

Radracr
June 29th, 1999, 12:58 PM
Yes. I realize that now. I should have had a can of Coke before I started replying to messages this morning. Over all it still works. It just has extra memory allocated that is not being used or needed for a short time.