C++ TCHAR 数组到 wstring 在 VS2010 中不起作用

标签 c++ arrays wstring tchar

我想将 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 对我不起作用。编译器说 “_lpa” 未定义,我不知道从哪里开始。我尝试了其他我在网上找到的转换方法,但它们都没有用。

最佳答案

有类似的功能

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;

here一篇文章和here对于 MSDN。

关于C++ TCHAR 数组到 wstring 在 VS2010 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786068/

相关文章:

java - 如何解析 Json 对象中具有相同名称的多个 Json 数组

java - 将线性和二进制搜索应用于数组

c++ - 无法将 std::wstring 写入 wofstream

javascript - 如何使 JSON 数组唯一

c++ - 多重集的所有子树

c++ - 如何运行完全相互隔离的谷歌测试?

c++ - opencv - rgb值不断变化

C++ L"whatever"-> wstring -> basic_string<T>

c++ - C++ 中的 UTF-8 兼容性

c++ - Visual C++ - 从设置表单图标中抛出未处理的异常?