在C++中,找不到将 "std::string" 转换为 "LPCWSTR" 的合适转换方法
最编程
2024-07-26 22:15:06
...
//orig为输入的string字符串
#include <string>
#include <iostream>
#include<cstdlib>
typedef const wchar_t* LPCWSTR;//#include<winnt.h>
using namespace std;
LPCWSTR stringToLPCWSTR(string orig)
{
size_t origsize = orig.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t* wcstring = new wchar_t[sizeof(wchar_t) * (orig.length() - 1)];
mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
return wcstring;
}
int main()
{
string in= "this is a txt!";
LPCWSTR out = stringToLPCWSTR(in);
wcout << out << endl;
//别忘了释放内存~
delete out;
out = nullptr;
return 0;
}
上一篇: 侯捷C++指南:转换函数详解
下一篇: 简易理解的图灵机示例