実戦c++のstringシリーズ--stringからLPCWSTRへの変換

1831 ワード

今日はstringからLPCWSTRへの変換方法を紹介します.
LPCWSTRはどんなタイプですか?定義方法を見てみましょう.
typedef const wchar_t* LPCWSTR;

その名の通り、LPCWSTRはunicode符号化文字列を指す32ビットポインタであり、指す文字列はchar型ではなくwchar型である.
例えばMessageBoxWの2番目、3番目のパラメータはLPCWSTRタイプである.
`MessageBoxW(__in_opt HWND hWnd, __in_opt LPCWSTR lpText,
            __in_opt LPCWSTR lpCaption, __in UINT uType)`

そこで質問ですが、stringタイプの文字列がありますが、どのようにMessageBoxで表示されますか?LPCWSTRタイプへのstring変換が必要です!!
        string image_path = "c:\\avi.png";
        size_t size = image_path.length();
        wchar_t *buffer = new wchar_t[size + 1];
        MultiByteToWideChar(CP_ACP, 0, response->image_path.c_str(), size, buffer, size * sizeof(wchar_t));
        buffer[size] = 0;  //    '\0'    
        ::MessageBox(NULL, buffer, NULL, NULL);
        delete buffer;
        buffer = null;

見たでしょうまたMultiByteToWideChar関数を使いました.だからこの関数の使い方をしっかり覚えてください.http://blog.csdn.net/wangshubo1989/article/details/49210385