![](/img/trans.png)
[英]argument of type "WCHAR" is incompatible with parameter of type "const char"
[英]Argument type “WCHAR *” is incompatible with parameter of type “const char *”
在下面的论坛中找到了这个有用的代码,但它在这一行引发了错误
if (stricmp(entry.szExeFile, "target.exe") == 0)
说
参数类型“WCHAR *”与类型“const char *”的参数不兼容。
我正在使用 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;
}
您正在编译项目,其字符集选项设置为 Unicode,因此Process32First()
和Process32Next()
map 到它们的WCHAR[]
版本,因此是entry.szExeFile
。
但是stricmp()
取而代之的是char
输入,这就是代码无法编译的原因。
您需要改用 stricmp 的stricmp
版本,即wcsicmp()
或_wcsicmp()
,例如:
if (wcsicmp(entry.szExeFile, L"target.exe") == 0)
或者更好的是,在<tchar.h>
中使用_tcsicmp()
,因为您实际上使用的是Process32...
函数的TCHAR
版本,例如:
if (_tcsicmp(entry.szExeFile, _T("target.exe")) == 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.