欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在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; }