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
C++C

Avatar of undefined
Last Comment
itsmeandnobodyelse
Avatar of Kelvin_King
Kelvin_King
Flag of Singapore image

Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo