Link to home
Start Free TrialLog in
Avatar of thisisazam
thisisazam

asked on

PWCHAR and BSTR conversion

Hi
I need to convert pointer to wide characters PWCHAR to BSTR and vice versa.
Please comments?
--Azam
Avatar of Kelvin_King
Kelvin_King
Flag of Singapore image

Avatar of Deepu Abraham
Use _bstr_t class. _bstr_t instance can be created from BSTR pointer, and casted to char* and wchar_t*.
--> char to bstr to char*
void Foo(BSTR bstr)
{
    _bstr_t b(bstr, false);

    const char* pChar = (const char*)b;
    const wchar_t* pWChar = (const wchar_t*)b;
}

you could use functions:

::SysAllocString()
::SysFreeString()

example:
inline void CStatBar::SetText(OLECHAR * sz)
{
   SysFreeString(m_bstrMsg);   // Free previous string, if any.
   m_bstrMsg = SysAllocString(sz);
   // Caller should check to see if m_bstrMsg is null after calling this method.
}

CStatBar::~CStatBar()
{
   SysFreeString(m_bstrMsg);
}


MultiByteToWideChar() --> char* to wchar / pwchar
example:
char Src[] = "Hello World";
wchar_t DestBuffer[500] = L"";
MultiByteToWideChar(CP_ACP,0,Src,strlen(Src),DestBuffer,sizeof(DestBuffer));
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial