简体   繁体   English

“char *”类型的参数与“LPWSTR”类型的参数不兼容

[英]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.

相关问题 char类型的参数与参数类型LPWSTR不兼容 - Argument of type char is incompatible with parameter type LPWSTR CreateProcess 使用路径时的错误事件:“char *”类型的 E0167 参数与“LPWSTR”类型的参数不兼容 - Error events when CreateProcess uses a path: E0167 argument of type "char *" is incompatible with parameter of type "LPWSTR" “ int”类型的参数与“ char”类型的参数不兼容 - Argument of type 'int' is incompatible with parameter of type 'char' “ char *”类型的参数与“ STARTUPINFO”类型的参数不兼容 - Argument of type “char *” is incompatible with parameter of type “STARTUPINFO” 构建 DLL 时出现 C++ 错误:const wchar_t* 类型的参数与 LPWSTR 类型的参数不兼容 - C++ error when building DLL: argument of type const wchar_t* is incompatible with parameter of type LPWSTR “unsigned char *”类型的参数与“const char *”类型的参数不兼容 - Argument of type "unsigned char *" is incompatible with parameter of type "const char *" “ const char **”类型的参数与“ const char *”类型的参数不兼容 - Argument of type “const char **” is incompatible with parameter of type “const char *” 类型“ const char *”的参数与类型“ char *”的参数不兼容。 但为什么? :( - Argument of type “const char*” is incompatible with parameter of type “char*”. But why? :( “const char *”类型的参数与“char *”类型的参数不兼容 - Argument of type “const char *” is incompatible with parameter of type “char *” “char”类型的 C++ 参数与“char(*)[7]”类型的参数不兼容 - C++ argument of type “char” is incompatible with parameter of type “char(*)[7]”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM