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