????? ????? ?? ANSI ??? Unicode & Unicode ??? ANSI ?? OLE

?????? ????????? ?????? ?????????
???? ???????: 138813 - ??? ???????? ???? ????? ????? ??? ???????.
????? ???? | ?? ????

??????

??????? ???? ??????? ???? ??? ??????? ??? ??????? ?? 32-?? OLE APIs ??????? ????? Unicode. ????? ??? ????????? ???? ?????? ????? ANSI ???????? ??? Unicode ??? ??????? ??? OLE ? ?????? ????? Unicode ???? ??? ?????? ?? OLE ANSI. ???? ??? ?????? ??? ???? ????? ??? ?????????.

??????? ????

????? Windows NT Unicode (?? ???? ?????) ??????? ????? Win32 ?????? ?????? ????? ANSI. ???? ?? ???? ???? ??????? Windows 95 ????? Unicode ???? ??????? Win32 ???? ?????? ???????. ????? ?? ??? ?????? ANSI ??????? ??? ???????.

??????? ????? ???? ??????? ?? OLE 32-??. 32-?? OLE APIs ??????? ????? ??? Windows NT ? Windows 95 ??????? Unicode ???? ???. ?? ??? ????? ANSI ??????? ??? ??????? ?? Windows NT ?? Windows 95.

??? ???? ?? ????? 32-?? ????? ????? ??? ?? ?? Windows 95 ? Windows NT ??? ?? ?????? ??????? ANSI ????? ???? OLE Win32 ???? ????? ??????? ANSI ??? Unicode ??? ?? ??? ????? ??? OLE.

??? ??? ??????? ????? Unicode 32-?? ???? ???? ??? Windows NT ??? ??? ????? ??????? ANSI/Unicode.

Win32 ???? MultiByteToWideChar WideCharToMultiByte ?????? ??????? ANSI ??? Unicode ? ????? Unicode ??? ANSI. ???? ??? ??????? AnsiToUnicode UnicodeToAnsi ???? ?????? ??? ??????? ??????? ANSI/Unicode.
/*
 * AnsiToUnicode converts the ANSI string pszA to a Unicode string
 * and returns the Unicode string through ppszW. Space for the
 * the converted string is allocated by AnsiToUnicode.
 */ 

HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{

    ULONG cCharacters;
    DWORD dwError;

    // If input is null then just return the same.
    if (NULL == pszA)
    {
        *ppszW = NULL;
        return NOERROR;
    }

    // Determine number of wide characters to be allocated for the
    // Unicode string.
    cCharacters =  strlen(pszA)+1;

    // Use of the OLE allocator is required if the resultant Unicode
    // string will be passed to another COM component and if that
    // component will free it. Otherwise you can use your own allocator.
    *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
    if (NULL == *ppszW)
        return E_OUTOFMEMORY;

    // Covert to Unicode.
    if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
                  *ppszW, cCharacters))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszW);
        *ppszW = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }

    return NOERROR;
/*
 * UnicodeToAnsi converts the Unicode string pszW to an ANSI string
 * and returns the ANSI string through ppszA. Space for the
 * the converted string is allocated by UnicodeToAnsi.
 */ 

HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
{

    ULONG cbAnsi, cCharacters;
    DWORD dwError;

    // If input is null then just return the same.
    if (pszW == NULL)
    {
        *ppszA = NULL;
        return NOERROR;
    }

    cCharacters = wcslen(pszW)+1;
    // Determine number of bytes to be allocated for ANSI string. An
    // ANSI string can have at most 2 bytes per character (for Double
    // Byte Character Strings.)
    cbAnsi = cCharacters*2;

    // Use of the OLE allocator is not required because the resultant
    // ANSI  string will never be passed to another COM component. You
    // can use your own allocator.
    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
    if (NULL == *ppszA)
        return E_OUTOFMEMORY;

    // Convert to ANSI.
    if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
                  cbAnsi, NULL, NULL))
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszA);
        *ppszA = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }
    return NOERROR;

}
				
??????? ????? ??? ??????? ??? ???. ??? ??????? CoTaskMemFree ?????? ????? ?????? ??? ?? ??????? CoTaskMemAlloc ????? ???????. ????? ?????? ??? ?? ?? ???? ????? ??? ???? ???? ????? ???? ??? ???? OLE ??? ??? ?????? ??????? ?? ????? ???????. LPOLESTR ???? ??? ????? Unicode.
// The following code gets an ANSI filename that is specified by the
// user in the OpenFile common dialog. This file name is converted into
// a Unicode string and is passed to the OLE API CreateFileMoniker. The
// Unicode string is then freed.

OPENFILENAME ofn;
LPOLESTR pszFileNameW;
LPMONIKER pmk;
:

// Get file name from OpenFile Common Dialog. The ANSI file name will
// be placed in ofn.lpstrFile

GetOpenFileName(&ofn);
:
AnsiToUnicode(ofn.lpstrFile, &pszFileNameW);
CreateFileMoniker(pszFileNameW, &pmk);
CoTaskMemFree(pszFileNameW);

// The following code implements IOleInPlaceFrame::SetStatusText.
// The lpszStatusText string, that is received from another OLE
// component, uses Unicode. The string is converted to ANSI before it is
// passed to the ANSI version of SetWindowText. Windows 95 supports only
// the ANSI version of SetWindowText.

COleInPlaceFrame::SetStatusText(LPCOLESTR pszStatusTextW)
{

    LPSTR pszStatusTextA;
    UnicodeToAnsi(pszStatusTextW, &pszStatusTextA);
    SetWindowText(m_hwndStatus, pszStatusTextA);
    CoTaskMemFree(pszStatusTextA);

}
				
??????: ??????? ?? AnsiToUnicode ? UnicodeToAnsi ???? ????? ???? ????????? ?? ????? ????? ??????. ????? CoTaskMemAlloc (???? OLE) ???? ???????? ??? ??? ???? ??????? ??????? ???? ???? ??????? ??? ???? OLE ??? ???? ??? ??? ?????? ???? ????? ???????. ??? ??? ??????? ??? ???? ?? ??????? ???? ??? ??????? ??????? ?? ??? ?????? ????? OLE ???? OLE. ??? ?? ???? ??????? ???? ??? ??????? ??????? ??- ????? ?? ????? ???? ?????? ???????? ??- ????? - ?????? ?? ??????? ???????? ???? OLE.

???? ????? ????? ??????? ??? Unicode ?? ??? ??????? ??????? ???????? ????? OLESTR. ??? ???? ??????:
CreateFileMoniker(OLESTR("c:\\boo\\har.doc"), &pmk);
				
???? ?????? ??? ???? ??? ?? ????????? ??????? ANSI/Unicode ?? ????????? ???????? ?????? ???? Foundation ?? Microsoft "(MFC) ???? ???? ?? ?????? ??????? ??????? 4.0 Visual C++. ??? ????????? ??????? ?? Technote MFC 59: '??????? MFC MBCS/Unicode "????? ?????"'. ??????? ????? ??????? ??? OLE2T ? T2OLE ? OLE2CT ? T2COLE ? A2W ? W2A ? A2CW ? W2CA ? USES_CONVERSION ??? \msdev\mfc\include\afxpriv.h. ???? ????? AfxA2WHelper ? AfxW2AHelper ?? ????????? ???????? ?????? MFC ?? \msdev\mfc\src ? ??????? OLE2T ? T2OLE OLE2CT ? T2COLE ?? ????????? ???????? ?????? MFC ?? \msdev\mfc\src. ???? ??? ??????? ?????? ?????? ??? ??????? ??????? ??? Unicode ?? ANSI ???????? ??? ?? ??? ?? ????? ????? preprocessor _UNICODE. ??? ???? ??????? ??????? CreateFileMoniker ?? ?????? ????? ???? ????? ??? ??? ?? ????? ??????? MFC:
USES_CONVERSION;
GetOpenFileName(&ofn);
CreateFileMoniker(T2OLE(ofn.lpstrFile), &pmk);
				
??? ?? ????? _UNICODE T2OLE ????? ??? ???:
inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
				
??? ?? ??? ????? _UNICODE T2OLE ????? ??? ???:
#define T2OLE(lpa) A2W(lpa)
				
T ?? T2OLE ???? ??? ??? ??? ??????? ??? ????? OLE (Unicode ?????) ????? Unicode ??? ????? _UNICODE ? ????? ?? ????? ANSI ????? ?? ??? ????? _UNICODE. ????? LPTSTR ??? ????? ????? ??? ????? Unicode ??? ????? _UNICODE ? ????? ??? ????? ANSI ????? ?? ??? ????? _UNICODE. ?? T2OLE ?? ??????? ??? ????? _UNICODE (LPTSTR == LPOLESTR). ????? ?? ??? ????? Unicode ???? A2W. A2W ????? ????? ?? ????? ANSI ??? Unicode ??? ???:
#define A2W(lpa) (\ 
        ((LPCSTR)lpa == NULL) ? NULL : (\ 
            _convert = (strlen(lpa)+1),\ 
            AfxA2WHelper((LPWSTR) alloca(_convert*2), lpa, _convert)\ 
        )\ 

)
				
AfxA2WHelper ?????? MultiByteToWideChar ?????? ???????.

??????? ????? ??????? ??????? MFC _alloca ?????? ????? ?? ?????? ???????? ?????? ???????. ??? deallocated ??????? ???? ?????? ??? ?????? ??????? ???????. ????? OLE ???? OLE ????????? ?? ???? ??????? (????????) ???? ???? ??????? ?? ??? ???? ???? ???????? ??????? ?????? ???. ???? ??? ?? ??????? ???? ?? ??????? ?? ???? ?????? ????? ????? ??- ????? - ?????? ?????? OLE ?? ???? OLE. ?? ???????? ????? ?? ???? ????? ???? OLE ??? ?????? ??????? ?? ????? ?????. ???? ????????/Embedding ???????? OLE ? API ????? ????? ??? ?? ????????. ???????? ???? ??????? ????? ??????? ??????? MFC ?? ???? ???????. ?? ???? ??????? ????? ??????? ??????? MFC ?????? ???? ?? ????? ????? ???? ?????? ?????? ??? ???? ????? ????? ???????? ???? OLE. ???? ??????? AnsiToUnicode ? UnicodeToAnsi ?? ??? ???????.

??? ??? ?????? ??????? ????? Unicode/ANSI ????? ?????? ??? ?????? Don ???? OLE ?? ??????? ????? Microsoft ? ????? 1995, 8 No. 10 Vol. 86 ??????. ???? ???? Don ??? C++ ?? ???? ????? ??????? ???? ????? ?????? ????? ???? Unicode/ANSI ?????. ??? ???????? ????? ??????? ??????? ????? ????? ?????? ???? ??????. ???? ????? ??? ????? ????? ???????? ???? OLE ? ?? ????? ??????? ??????? ?? ??????? ???? ?? ??????? ?? ???? ?????? ?? ?????? ??????.

??? ?????? ? String16 ? ?? ?????? Don ???? ???? ???? ????? ?? ????? ANSI ??? Unicode ? ????. ??? ??????? ??? ???? String8 ?????? ??? ??? ANSI ??? Unicode ???????. ???? ????? ?????? CreateFileMoniker ?? ?????? ?????? ??? ??? ?? ??? ?????:
GetOpenFileName(&ofn);
CreateFileMoniker(String16(ofn.lpstrFile), &pmk);
				
??? ????? ???? String16 ?? ????????? ???????? ?????. ??? ????? ????? ????? ??????? ANSI ??? Unicode. ???? ????? ??? ??????? ???? ????? ??????? ? wchar_t const ???? ????? * ? ??? ????? ??? ??????? ??? ??? ??????? ?????? ????? CreateFileMoniker. ???? ????? ??????? ?????? ????? ???? Unicode ???? ??? ??????? ??? CreateFileMoniker. ??? destruct ?????? ?? ???????? ???? ??????.
// String16 //////////////////////////////////////////////////////// 
// Shim class that converts both 8-bit (foreign) and
// 16-bit (native) strings to 16-bit wideness

class String16 {
public:

// native and foreign constructors
    String16(const char *p8);
    String16(const wchar_t *p16);

// non-virtual destructor (this class is concrete)

  ~String16(void);

// native conversion operator

  operator const wchar_t * (void) const;
private:

// native wideness string
    wchar_t *m_sz;
// is foreign??
    BOOL m_bIsForeign;

// protect against assignment!

  String16(const String16&);

    String16& operator=(const String16&);
};

// native constructor is a pass-through

inline String16::String16(const wchar_t *p16)
: m_sz((wchar_t *)p16), m_bIsForeign(FALSE)
{
}

// simply give out the native wideness string

inline String16::operator const wchar_t * (void) const
{
  return m_sz;
}

// foreign constructor requires allocation of a native
// string and conversion

inline String16::String16(const char *p8)
: m_bIsForeign(TRUE)
{

// calculate string length

  size_t len = strlen(p8);

// calculate required buffer size (some characters may
// already occupy 16-bits under DBCS)

  size_t size = mbstowcs(0, p8, len) + 1;

// alloc native string and convert

  if (m_sz = new wchar_t[size])

    mbstowcs(m_sz, p8, size);

}

// delete native string only if synthesized in foreign constructor

inline String16::~String16(void) {
  if (m_bIsForeign)

    delete[] m_sz;

}
				

???????

???? ???????: 138813 - ????? ??? ??????: 06/???/1426 - ??????: 2.4
????? ???
  • Microsoft OLE 4.0, ????? ?????? ??:
    • Microsoft Windows NT 3.51 Service Pack 5
    • Microsoft Windows NT 4.0
    • Microsoft Windows 95
????? ??????: 
kbmt kbcode kbhowto kbprogramming KB138813 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????138813

????? ???????

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com