简体   繁体   English

参数类型 (WCHAR*) 与“const char*”类型的参数不兼容

[英]Argument Type (WCHAR*) is incompatible with parameter of type "const char*)

This is part of some CSGO code and I don't know how to fix this problem;这是一些 CSGO 代码的一部分,我不知道如何解决这个问题; It's on line 8 of the snippet shown below:它位于如下所示代码段的第 8 行:

在此处输入图像描述

uintptr_t GetModuleBaseAddress(const char* modName) {
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
    if (hSnap != INVALID_HANDLE_VALUE) {
        MODULEENTRY32 modEntry;
        modEntry.dwSize = sizeof(modEntry);
        if (Module32First(hSnap, &modEntry)) {
            do {
                if (!strcmp(modEntry.szModule, modName)) {
                    CloseHandle(hSnap);
                    return (uintptr_t)modEntry.modBaseAddr;
                }
            } while (Module32Next(hSnap, &modEntry));
        }
    }
}

Error message:错误信息:

Argument Type (WCHAR*) is incompatible with parameter of type "const char*)

As mentioned in the comments, your problem lies in the fact that your (new) project has the "Use Unicode Character Set" option set for builds but the (old) code base expects to be built using the "Use Multi-Byte Character Set" option.如评论中所述,您的问题在于您的(新)项目具有“使用 Unicode 字符集”选项集,但(旧)代码库预计将使用“使用多字节字符集”构建“ 选项。 (The former uses the wchar_t* type for strings in Windows API calls and structures, whereas the latter uses the char* type; these are incompatible pointers.) (前者对 Windows API 调用和结构中的字符串使用wchar_t*类型,而后者使用char*类型;这些是不兼容的指针。)

So, the 'quick fix' is to set your project's build options to use the "Multi-Byte" character set.因此,“快速修复”是将项目的构建选项设置为使用“多字节”字符集。 In Visual Studio 2019, this option is the project's Properties -> Advanced -> Character Set when you right-click on the project in the Solution Explorer;在 Visual Studio 2019 中,当您在解决方案资源管理器中右键单击项目时,此选项是项目的属性 -> 高级 -> 字符集 earlier versions of Visual Studio have the option in the General property tab, IIRC.早期版本的 Visual Studio 在常规属性选项卡 IIRC 中有选项。

If you are using some other IDE, then you'll need to 'manually' undefine the UNICODE macro (which signals to the compiler that the "Unicode Character Set" is in use);如果您正在使用其他一些 IDE,那么您需要“手动”取消定义UNICODE宏(向编译器发出“Unicode 字符集”正在使用的信号); an #undef UNICODE line before any of your #include <...> lines for the system headers would work. #undef UNICODE之前的任何#include <...>系统标头行将起作用。

For much (most) of the Windows API, this issue can also be resolved by using explicit wchar_t or char versions of structures and functions;对于大部分(大多数)Windows API,这个问题也可以通过使用显式wchar_tchar版本的结构和函数来解决; for example, the GetModuleFileName function, mentioned in the comments, resolves to either GetModuleFileNameW or GetModuleFileNameA , depending on which character set is chosen for the build, and structures used in those calls have W and A versions defined explicitly.例如,注释中提到的GetModuleFileName function 解析为GetModuleFileNameWGetModuleFileNameA ,具体取决于为构建选择的字符集,并且这些调用中使用的结构具有明确定义的WA版本。 Thus, one can 'override' the default call for the project's setting by explicitly using the A - or W -suffixed versions of those functions and structures.因此,可以通过显式使用这些函数和结构的AW后缀版本来“覆盖”项目设置的默认调用。

However, in your particular case, unfortunately, the TlHelp32.h header file (and the functions it defines) are not so well defined: There are no explicit A -suffixed versions, so you can't simply suffix your MODULEENTRY32 and Module32First references with an A , as one would like.但是,不幸的是,在您的特定情况下, TlHelp32.h header 文件(及其定义的函数)的定义不是很好:没有明确A后缀版本,因此您不能简单地为MODULEENTRY32Module32First引用添加后缀一个A ,就像一个人想要的那样。 (For Unicode builds, the W -suffixed versions simply overwrite the 'original' function and structure names, so these are then lost to your code.) (对于 Unicode 构建,以W为后缀的版本只会覆盖“原始”function 和结构名称,因此这些名称随后会丢失到您的代码中。)

Thus, without changing your code to use TCHAR types throughout ( TCHAR resolves to either char or wchar_t , depending on the character set option), your only sensible option is the aforementioned quick fix: change the build settings.因此,无需更改代码以始终使用TCHAR类型( TCHAR解析为charwchar_t ,具体取决于字符集选项),您唯一明智的选择是上述快速修复:更改构建设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “WCHAR”类型的参数与“const char”类型的参数不兼容 - argument of type "WCHAR" is incompatible with parameter of type "const char" 参数类型“WCHAR *”与类型“const char *”的参数不兼容 - Argument type “WCHAR *” is incompatible with parameter of type “const char *” E0167 “CHAR *”类型的参数与“const wchar_t *”类型的参数不兼容 - E0167 argument of type "CHAR *" is incompatible with parameter of type "const wchar_t *" “ const char **”类型的参数与“ const char *”类型的参数不兼容 - Argument of type “const char **” is incompatible with parameter of type “const char *” WCHAR 类型的参数与 const char* 类型的参数不兼容 - Argument of type WCHAR is incomaptible with parameter of type const char* “unsigned char *”类型的参数与“const char *”类型的参数不兼容 - Argument of type "unsigned 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 *” “const char *”类型的默认参数与“char *”类型的参数不兼容 - Default argument of type "const char *" is incompatible with parameter of type "char *" “const char *”类型的参数与“char”类型的参数不兼容 - argument of type "const char *" is incompatible with parameter of type "char"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM