简体   繁体   English

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

[英]Argument type “WCHAR *” is incompatible with parameter of type “const char *”

Found this useful code below, here in forum but it raised an error at this line在下面的论坛中找到了这个有用的代码,但它在这一行引发了错误

if (stricmp(entry.szExeFile, "target.exe") == 0)

says

Argument type "WCHAR *" is incompatible with parameter of type "const char *".参数类型“WCHAR *”与类型“const char *”的参数不兼容。

I'm using Visual Studio 2019.我正在使用 Visual Studio 2019。

#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>

void EnableDebugPriv()
{
    HANDLE hToken;
    LUID luid;
    TOKEN_PRIVILEGES tkp;

    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);

    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);

    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = luid;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);

    CloseHandle(hToken);
}

int main(int, char* [])
{
    EnableDebugPriv();

    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            if (stricmp(entry.szExeFile, "target.exe") == 0)
            {
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);

                // Do stuff..

                CloseHandle(hProcess);
            }
        }
    }

    CloseHandle(snapshot);

    return 0;
}

You are compiling your project with its Character Set option set to Unicode, so Process32First() and Process32Next() map to their Unicode versions, thus entry.szExeFile is a WCHAR[] array.您正在编译项目,其字符集选项设置为 Unicode,因此Process32First()Process32Next() map 到它们的WCHAR[]版本,因此是entry.szExeFile

But stricmp() takes char input instead, which is why the code fails to compile.但是stricmp()取而代之的是char输入,这就是代码无法编译的原因。

You need to use the Unicode version of stricmp instead, which is wcsicmp() or _wcsicmp() , eg:您需要改用 stricmp 的stricmp版本,即wcsicmp()_wcsicmp() ,例如:

if (wcsicmp(entry.szExeFile, L"target.exe") == 0)

Or better, use _tcsicmp() in <tchar.h> , since you are actually using the TCHAR version of the Process32... functions, eg:或者更好的是,在<tchar.h>中使用_tcsicmp() ,因为您实际上使用的是Process32...函数的TCHAR版本,例如:

if (_tcsicmp(entry.szExeFile, _T("target.exe")) == 0)

暂无
暂无

声明:本站的技术帖子网页,遵循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