[英]How do I get the process ID of something in windows using c++
I am trying to get the procid of a process using the name of the process.我正在尝试使用进程名称获取进程的 procid。 The errors are talking about procEntry.szExeFile.
错误是关于 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?有没有其他方法可以获取进程 ID? I have tried reinstalling c++ libraries.
我尝试重新安装 c++ 库。 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;
}
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).您正在使用基于
TCHAR
的宏,并且您正在编译您的项目并定义了UNICODE
,因此这些宏 map 到基于wchar_t
的 API(即PROCESSENTRY32
-> PROCESSENTRY32W
, Process32First
-> Process32FirstW
)。 As such, the PROCESSENTRY32::szExeFile
field is a wchar_t[]
array.因此,
PROCESSENTRY32::szExeFile
字段是一个wchar_t[]
数组。 But, strcmp()
expects char*
strings instead, hence the compiler error you are getting.但是,
strcmp()
需要char*
字符串,因此会出现编译器错误。
Since your function takes in a const char*
as input, you shouldn't be using the TCHAR
APIs at all.由于您的 function 接受
const char*
作为输入,因此您根本不应该使用TCHAR
API。 Use the A
nsi-based APIs instead, eg:请改用基于
A
nsi 的 API,例如:
#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:否则,将代码更改为使用
wchar_t
字符串和基于W
ide 的 API,例如:
#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.如果可以提供帮助,请远离
TCHAR
及其相关宏。 They are a remnant from the Win9x/ME days when people were migrating code from ANSI to UNICODE.它们是 Win9x/ME 时代的遗留物,当时人们将代码从 ANSI 迁移到 UNICODE。 They really don't have a place in modern coding, as everything is now Unicode.
它们在现代编码中确实没有一席之地,因为现在一切都是 Unicode。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.