I am trying to get the procid of a process using the name of the process. The errors are talking about procEntry.szExeFile. However, I am getting the errors:
[{
"owner": "C/C ",
"code": "167",
"severity": 8,
"message": "argument of type \"WCHAR *\" is incompatible with parameter of type \"const char *\"",
"source": "C/C ",
"startLineNumber": 17,
"startColumn": 17,
"endLineNumber": 17,
"endColumn": 26
},{
"owner": "C/C ",
"code": "167",
"severity": 8,
"message": "argument of type \"WCHAR *\" is incompatible with parameter of type \"const char *\"",
"source": "C/C ",
"startLineNumber": 24,
"startColumn": 21,
"endLineNumber": 24,
"endColumn": 30
}]
Is there another way to get the process ID? I have tried reinstalling c libraries. I have also tried converting it but that didn't work either. Here is the code I am using:
#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <TlHelp32.h>
// Get process id from name
DWORD GetProcId(const char* procName)
{
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(procEntry);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap == INVALID_HANDLE_VALUE)
return 0;
Process32First(hSnap, &procEntry);
if (!strcmp(procEntry.szExeFile, procName))
{
CloseHandle(hSnap);
return procEntry.th32ProcessID;
}
while (Process32Next(hSnap, &procEntry))
{
if (!strcmp(procEntry.szExeFile, procName))
{
CloseHandle(hSnap);
return procEntry.th32ProcessID;
}
}
CloseHandle(hSnap);
return 0;
}
int main()
{
// Get process id from name
DWORD procId = GetProcId("csgo.exe");
if (!procId)
{
std::cout << "Could not find process" << std::endl;
return 0;
}
// wait for key press
std::cout << "Press any key to continue" << std::endl;
std::getchar();
return 0;
}
CodePudding user response:
You are using TCHAR
-based macros, and you are compiling your project with UNICODE
defined, so those macros map to the wchar_t
-based APIs (ie, PROCESSENTRY32
-> PROCESSENTRY32W
, Process32First
-> Process32FirstW
, etc). As such, the PROCESSENTRY32::szExeFile
field is a wchar_t[]
array. But, strcmp()
expects char*
strings instead, hence the compiler error you are getting.
Since your function takes in a const char*
as input, you shouldn't be using the TCHAR
APIs at all. Use the A
nsi-based APIs instead, eg:
#include <iostream>
#include <string>
#include <cstring>
#include <windows.h>
#include <TlHelp32.h>
// Get process id from name
DWORD GetProcId(const char* procName)
{
PROCESSENTRY32A procEntry;
procEntry.dwSize = sizeof(procEntry);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap == INVALID_HANDLE_VALUE)
return 0;
if (Process32FirstA(hSnap, &procEntry))
{
do
{
if (std::strcmp(procEntry.szExeFile, procName) == 0)
{
CloseHandle(hSnap);
return procEntry.th32ProcessID;
}
}
while (Process32NextA(hSnap, &procEntry));
}
CloseHandle(hSnap);
return 0;
}
int main()
{
// Get process id from name
DWORD procId = GetProcId("csgo.exe");
if (!procId)
{
std::cout << "Could not find process" << std::endl;
return 0;
}
// wait for key press
std::cout << "Press any key to continue" << std::endl;
std::getchar();
return 0;
}
Otherwise, change the code to use wchar_t
strings and W
ide-based APIs instead, eg:
#include <iostream>
#include <string>
#include <cwchar>
#include <windows.h>
#include <TlHelp32.h>
// Get process id from name
DWORD GetProcId(const wchar_t* procName)
{
PROCESSENTRY32W procEntry;
procEntry.dwSize = sizeof(procEntry);
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnap == INVALID_HANDLE_VALUE)
return 0;
if (Process32FirstW(hSnap, &procEntry))
{
do
{
if (std::wcscmp(procEntry.szExeFile, procName) == 0)
{
CloseHandle(hSnap);
return procEntry.th32ProcessID;
}
}
while (Process32NextW(hSnap, &procEntry));
}
CloseHandle(hSnap);
return 0;
}
int main()
{
// Get process id from name
DWORD procId = GetProcId(L"csgo.exe");
if (!procId)
{
std::cout << "Could not find process" << std::endl;
return 0;
}
// wait for key press
std::cout << "Press any key to continue" << std::endl;
std::getchar();
return 0;
}
Stay away from TCHAR
and its related macros if you can help it. They are a remnant from the Win9x/ME days when people were migrating code from ANSI to UNICODE. They really don't have a place in modern coding, as everything is now Unicode.