![](/img/trans.png)
[英]argument of type "WCHAR" is incompatible with parameter of type "const char"
[英]Argument of type WCHAR is incomaptible with parameter of type const char*
DWORD Snapshots::getWindow(const char* windowName)
{
initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32 /// also using tagPROCESENTRY32
do
{
if (!strcmp(pEntry.szExeFile, windowName)) /// we compare the found exe to the exe's name we need.
{
}
return 0;
} while(Process32Next(initVariables::hSnapshot, &pEntry)); /// 1. arg = our handle 2. arg = us referencing pEntry as our lpme
return 0;
}
我正在使用多字节字符集,此错误仅在调试模式下发生,在发布时不会发生。
当项目字符集设置为 Unicode(显然是这种情况)时,此代码将无法编译,因为strcmp()
接受char*
输入,而不是wchar_t*
输入。
由于代码使用的是 Win32 API 的TCHAR
版本,因此它应该使用_tcscmp()
来匹配:
#include <tchar.h>
DWORD Snapshots::getWindow(const char* windowName)
{
initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (initVariables::hSnapshot == INVALID_HANDLE_VALUE)
return 0;
PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32 /// also using tagPROCESENTRY32
if (Process32First(initVariables::hSnapshot, &pEntry))
{
do
{
if (!_tcscmp(pEntry.szExeFile, windowName)) /// we compare the found exe to the exe's name we need.
{
}
}
while (Process32Next(initVariables::hSnapshot, &pEntry)); /// 1. arg = our handle 2. arg = us referencing pEntry as our lpme
}
CloseHandle(initVariables::hSnapshot);
return 0;
}
但是,由于windowName
是char*
而不是TCHAR*
,因此不受项目字符集的影响,直接使用基于 ANSI 的 API:
#include <tchar.h>
DWORD Snapshots::getWindow(const char* windowName)
{
initVariables::hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (initVariables::hSnapshot == INVALID_HANDLE_VALUE)
return 0;
PROCESSENTRY32 pEntry; /// new variable named pEntry, that works around tagPROCESSENTRY32
pEntry.dwSize = sizeof(PROCESSENTRY32); /// compiler will deny any other value that doesn't fit in tagPROCESSEENTRY32 /// also using tagPROCESENTRY32
if (Process32FirstA(initVariables::hSnapshot, &pEntry))
{
do
{
if (!strcmp(pEntry.szExeFile, windowName)) /// we compare the found exe to the exe's name we need.
{
}
}
while (Process32NextA(initVariables::hSnapshot, &pEntry)); /// 1. arg = our handle 2. arg = us referencing pEntry as our lpme
}
CloseHandle(initVariables::hSnapshot);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.