0

TCHAR 配列を wstring に変換したいと思います。

    TCHAR szFileName[MAX_PATH+1];
#ifdef _DEBUG
    std::string str="m:\\compiled\\data.dat";
    TCHAR *param=new TCHAR[str.size()+1];
    szFileName[str.size()]=0;
    std::copy(str.begin(),str.end(),szFileName);
#else
    //Retrieve the path to the data.dat in the same dir as our data.dll is located
    GetModuleFileName(_Module.m_hInst, szFileName, MAX_PATH+1);
    StrCpy(PathFindFileName(szFileName), _T("data.dat"));
#endif  

wstring sPath(T2W(szFileName));

szFileName期待する関数に渡す必要があります

const WCHAR *

完全を期すために、渡す必要があるボイドを述べていますszFileName

HRESULT CEngObj::MapFile( const WCHAR * pszTokenVal,  // Value that contains file path
                        HANDLE * phMapping,          // Pointer to file mapping handle
                        void ** ppvData )            // Pointer to the data

ただし、T2W は機能しません。コンパイラは と言っthat "_lpa" is not definedていますが、ここからどこに進むべきかわかりません。ネットで見つけた他の変換方法を試しましたが、どちらも機能しませんでした。

4

2 に答える 2

2

のような機能があります。

mbstowcs_s()

から に変換char*wchar_t*ます。

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

char *orig = "Hello, World!";
cout << orig << " (char *)" << endl;

// Convert to a wchar_t*
size_t origsize = strlen(orig) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
wcscat_s(wcstring, L" (wchar_t *)");
wcout << wcstring << endl;

記事についてはこちらを、MSDNについてはこちらをご覧ください。

于 2013-04-03T11:32:37.627 に答える
0

の定義はTCHAR、特定のプリプロセッサ マクロが定義されているかどうかによって異なります。可能な組み合わせについては、この記事などを参照してください。

これは、TCHARがすでに である可能性があることを意味しwchar_tます。

マクロを使用して_UNICODE、文字列を変換する必要があるかどうかを確認できます。もしそうなら、あなたはmbstowcs変換を行うために使用することができます:

std::wstring str;

#ifdef _UNICODE
    // No need to convert the string
    str = your_tchar_string;
#else
    // Need to convert the string
    // First get the length needed
    int length = mbstowcs(nullptr, your_tchar_string, 0);

    // Allocate a temporary string
    wchar_t* tmpstr = new wchar_t[length + 1];

    // Do the actual conversion
    mbstowcs(tmpstr, your_tchar_str, length + 1);

    str = tmpstr;

    // Free the temporary string
    delete[] tmpstr;
#endif
于 2013-04-03T11:50:24.873 に答える