我在用C++复制文件时用的是CopyFile函数,出现了两个错误
不存在从 “std::string” 到 “LPCWSTR” 的适当转换函数
解决办法
LPCWSTR PCLVisualizer::stringToLPCWSTR(std::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;
}
无法将参数 2 从“const char [60]”转换为“LPCWSTR”
解决办法
这是Windows开发的函数命名规则,unicode版本的函数在函数名后加W,ansi则加A。所以你只需要在你的参数字符串前加L就可以了。
CopyFile(outname, L"E:\\PCLData\\temporaryFiles\\cc.asc",false);
更好的方法是使用自适配版本,即LoadLibrary,字符串参数改为_T( Y O U R S T R I N G YOUR_STRING YOURSTRING),_T()是一个宏,当程序定义为unicode时,相当于L,当程序定义为ansi时,则被忽略。