CoTaskMemAlloc (the OLE allocator) is required to be used only if the resultant string will be passed to another OLE component and if that component can free the string. NOTE: Comments in AnsiToUnicode and UnicodeToAnsi regarding the allocator that is used to allocate the converted string. SetWindowText(m_hwndStatus, pszStatusTextA) UnicodeToAnsi(pszStatusTextW, &pszStatusTextA)
Windows 95 supports onlyĬOleInPlaceFrame::SetStatusText(LPCOLESTR pszStatusTextW) passed to the ANSI version of SetWindowText. The string is converted to ANSI before it is The lpszStatusText string, that is received from another OLE
The following code implements IOleInPlaceFrame::SetStatusText. The ANSI file name willĪnsiToUnicode(ofn.lpstrFile, &pszFileNameW) Get file name from OpenFile Common Dialog. a Unicode string and is passed to the OLE API CreateFileMoniker. The following code gets an ANSI filename that is specified by the LPOLESTR is a pointer to a Unicode string. The converted string must not be freed if it is returned through an out-parameter to another OLE component, because that component is responsible for freeing the string. CoTaskMemFree is used to free the converted string if CoTaskMemAlloc was used to allocate the string. Sample use of these functions is as follows. If (0 = WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA, ANSI string will never be passed to another COM component. Use of the OLE allocator is not required because the resultant ANSI string can have at most 2 bytes per character (for Double Determine number of bytes to be allocated for ANSI string. HRESULT _fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA) * the converted string is allocated by UnicodeToAnsi. * and returns the ANSI string through ppszA. * UnicodeToAnsi converts the Unicode string pszW to an ANSI string If (0 = MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters, *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2) Otherwise you can use your own allocator. string will be passed to another COM component and if that Use of the OLE allocator is required if the resultant Unicode Determine number of wide characters to be allocated for the If input is null then just return the same.
HRESULT _fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) * the converted string is allocated by AnsiToUnicode. * and returns the Unicode string through ppszW. * AnsiToUnicode converts the ANSI string pszA to a Unicode string This article provides AnsiToUnicode and UnicodeToAnsi, which uses these functions for ANSI/Unicode conversion. Win32 provides MultiByteToWideChar and WideCharToMultiByte to convert ANSI strings to Unicode and Unicode strings to ANSI. This means that a 32-bit application that needs to run on both Windows 95 and Windows NT must use the ANSI versions of the non-OLE Win32 functions and must convert ANSI strings to Unicode before they are passed to OLE.Ī 32-bit Unicode application that runs only on Windows NT need not use any ANSI/Unicode conversion functions. ANSI versions of these functions are not implemented either on Windows NT or Windows 95. 32-bit OLE APIs and interface methods on Windows NT and Windows 95 use Unicode exclusively. Instead it implements only the ANSI versions of these functions.Ī major exception to this rule is 32-bit OLE. However Windows 95 does not implement the Unicode version of most Win32 functions that take string parameters. Windows NT implements Unicode (or wide character) and ANSI versions of Win32 functions that take string parameters. This article demonstrates how these conversions can be done. This requires applications that use ANSI strings to convert them to Unicode before passing them to OLE and to convert the Unicode strings that are received from OLE to ANSI.
How To Convert from ANSI to Unicode & Unicode to ANSI for OLEĪll strings that are passed to and received from 32-bit OLE APIs and interface methods use Unicode.