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
I need to convert pointer to wide characters PWCHAR to BSTR and vice versa.
Please comments?
--Azam
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,str len(Src),D estBuffer, sizeof(Des tBuffer));
--> 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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.codeguru.com/forum/archive/index.php/t-150671.html
- Kelvin