The Win32 system APIs operate on LPCWSTR (pointers to C-style NULL terminated arrays of wchar_t).
The system headers provide two versions of each API, for instance SetWindowTextA(HWND, LPCSTR) and SetWindowTextW(HWND, LPCWSTR) and a macro mapping SetWindowText() to the relevant one depending on the definition of UNICODE in your project ie:
#ifdef UNICODE
#define SetWindowText SetWindowTextW
#else
#define SetWindowText SetWindowTextA
#endif // !UNICODE
The SetWindowTextA() API will build a temporary LPCWSTR from it's LPCSTR parameter and call SetWindowTextW() where the operating code is, so not using UNICODE and wchar_t in your code gets a performance penalty.
The COM interfaces are designed to operate on BSTR which are system allocated to be shared between different processes.
Happily a BSTR can be built from a LPCWSTR with th MS compiler provided _bstr_t class: _bstr_t myBstr(/LPCWSTR/ psText).
The ATL::CString and MFC CString classes (which share most of their code) use a same macro mechanism and are mapped to CStringA or CStringW. Both have a builtin operator (CStringA::operator LPCSTR() CStringW::operator LPCWSTR()) so you can pass a CString to a Win32 API:
CString myStr = _T("Hello");
::SetWindowText(myHwnd, myStr);
std::[w]string has no such built-in operator but a c_str() member which does the same:
std::wstring myStr = L"Hello"; // assuming UNICODE defined
::SetWindowText(myHwnd, myStr.c_str());
Summary:
- #define UNICODE in all projects to avoid performance penalty on Win32 platforms.
- If you use MFC you will naturally use it's CString class, if you use ATL or none you may use ATL::CString which is a standalone class
(#include <atlstr.h>
does not include the ATL library) or std::wstring.
- If portability to other systems is a must you have to use std::wstring.
cheers,
AR