Reputation: 21
so for some reason my GetProcessID function is working in one project (no errors when compiling the source) but recently I just started a new project and now I am getting the error
'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [260]' to 'const char *'
But why am I even getting this cannot convert from WCHAR to const char * error? If I hover over p32.szExeFile szExeFile is type CHAR [260] not WCHAR. Also, I went into property pages and selected Use Multi-Byte Character Set. Even if I change std::string processName parameter to
const char *processName
or
char *processName
wchar_t *processName
I still get the same error.
So my question is this: Why is it that my GetProcessID function works in another project without any compilation errors but when I try to use this same function in a brand new project I get an error?
DWORD GetProcessID(std::string processName)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
std::cout << "CreateToolhelp32Snapshot() (of processes)" << std::endl;
std::cin.get();
return 0;
}
if (!Process32First(hSnapshot, &pe32))
{
CloseHandle(hSnapshot);
std::cout << "The first entry of the process list wasn't copied to the buffer." << std::endl;
std::cin.get();
return 0;
}
while (Process32Next(hSnapshot, &pe32))
{
if (strcmp(pe32.szExeFile, processName.c_str()) == 0)
{
CloseHandle(hSnapshot);
return pe32.th32ProcessID;
}
}
CloseHandle(hSnapshot);
return 0;
}
int main()
{
DWORD dwProcessID = NULL;
while (dwProcessID == NULL)
{
dwProcessID = GetProcessID("proc.exe");
}
std::cout << "Found PID " << dwProcessID << std::endl;
std::cin.get();
return 0;
}
Upvotes: 2
Views: 362
Reputation: 25388
The failing project is compiled as Unicode. When you do that, strings in PROCESSENTRY32
are defined as WCHAR []
rather than char []
.
One solution would be to explicitly code PROCESSENTRY32A
(and Process32FirstA
/ Process32NextA
), although Unicode strings are better able to represent non-ASCII characters.
Upvotes: 4