char string 轉換成 wchar_t string

| 0 Comments| | 00:29
Categories:

方法一:

#include <cstdlib>
int mbstowcs(wchar_t *out, const char *in, size_t size);

 
方法二:

#include <windows.h>
int MultiByteToWideChar(
    [in]            UINT      CodePage,
    [in]            DWORD     dwFlags,
    [in]            LPCSTR    lpMultiByteStr,
    [in]            int       cbMultiByte,
    [out, optional] LPWSTR    lpWideCharStr,
    [in]            int       cchWideChar
);

 
範例:

    catch (const std::exception& ex)
    {
        int len = MultiByteToWideChar(CP_ACPMB_PRECOMPOSED, ex.what(), -1, nullptr, 0);
        wchar_t* wszDest = new wchar_t[len];
        MultiByteToWideChar(CP_ACPMB_PRECOMPOSED, ex.what(), -1, wszDest, len);
        MessageBoxW(m_hwnd, wszDest, L"InitializeWindows Error", MB_OK | MB_ICONERROR);
        delete [] wszDest;
        exit(EXIT_FAILURE);
    }

 
 
source: How do I convert a char string to a wchar_t string?
 
 
#ansi #unicode #convert

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *