[英]Argument of type “char *” is incompatible with parameter of type “LPWSTR”
This has probably been asked before but I can't seem to find the solution: 这可能是之前被问过的,但我似乎无法找到解决方案:
std::string GetPath()
{
char buffer[MAX_PATH];
::GetSystemDirectory(buffer,MAX_PATH);
strcat(buffer,"\\version.dll");
return std::string(buffer);
}
This returns an error stating: 这会返回一个错误说明:
argument of type "char *" is incompatible with parameter of type "LPWSTR"
So yeah. 是的。 Anyone got an answer? 有人得到答案吗?
You need to use the ansi version: 您需要使用ansi版本:
std::string GetPath()
{
char buffer[MAX_PATH] = {};
::GetSystemDirectoryA(buffer,_countof(buffer)); // notice the A
strcat(buffer,"\\version.dll");
return std::string(buffer);
}
Or use unicode: 或者使用unicode:
std::wstring GetPath()
{
wchar_t buffer[MAX_PATH] = {};
::GetSystemDirectoryW(buffer,_countof(buffer)); // notice the W, or drop the W to get it "by default"
wcscat(buffer,L"\\version.dll");
return std::wstring(buffer);
}
Rather than call the A/W versions explicitly you can drop the A/W and configure the whole project to use ansi/unicode instead. 您可以删除A / W并将整个项目配置为使用ansi / unicode,而不是明确调用A / W版本。 All this will do is change some #defines to replace foo with fooA/W. 所有这一切都将改变一些#defines以用fooA / W替换foo。
Notice that you should use _countof() to avoid incorrect sizes depending on the buffers type too. 请注意,您应该使用_countof()来避免不正确的大小,具体取决于缓冲区类型。
If you compile your code using MultiByte support it will compile correctly,but when you compile it using Unicode flag it will give an error because in Unicode support ::GetSystemDirectoryA becomes ::GetSystemDirectoryW use consider using TCHAR instead of char.TCHAR is defined such that it becomes char in Multibyte flag and wchar_t with Unicode flag 如果您使用MultiByte支持编译代码,它将正确编译,但是当您使用Unicode标志编译它时会产生错误,因为在Unicode支持:: GetSystemDirectoryA变为:: GetSystemDirectoryW时请考虑使用TCHAR而不是char.TCHAR定义为它在Multibyte标志中变为char,在Unicode标志中变为wchar_t
TCHAR buffer[MAX_PATH];
::GetSystemDirectory(buffer,MAX_PATH);
_tcscat(buffer,_T("\\version.dll"));
You can use typedef for string /wstring so your code becomes independent 您可以将typedef用于string / wstring,以便您的代码变得独立
#ifdef UNICODE
typedef wstring STRING;
#else
typedef string STRING;
#endif
STRING GetPath()
{
TCHAR buffer[MAX_PATH];
::GetSystemDirectory(buffer,MAX_PATH);
_tcscat(buffer,_T("\\version.dll"));
return STRING(buffer);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.