问题描述
在psapi或windows.h中有任何函数来获取所需的进程'仅通过进程名(例如:chrome.exe)运行,而不会得到所有进程。
Is there any function in psapi or windows.h to get desired process' is running via only the process name (e.g : "chrome.exe") without getting all processes.
编辑:
如果任何一个需要通过运行所有进程列表获得所需的进程信息我可以在这里粘贴我的代码。它在xp机器上工作,并与vs 2008进行编译。
If any one requires to get the desired process information via running through the list of all processes I can paste my code here. it works on a xp-machine and compiled with vs 2008.
我也找到了我的问题的解决方案!但根据,该函数已经通过进程运行,并检查名称,不带扩展名。很快,它搜索chrome并返回chrome的列表。
I have found a solution for my question, too ! But according to the msdn the function runs already through the processes and checks the name without the extension. Shortly it searchs for "chrome" and returns the list of chrome.*
这个函数有一个不错的优势,它返回一个列表中的进程,它可能是一个exe可能运行可能实例。缺点CLR是必需的,它运行速度比psapi函数慢,它有额外的转换要求,如String ^ to wchar或String(我没有测试)
This function has a nice advantage it returns the process in a list, it might be an exe may run with may instances. Disadvantage CLR is required, it runs slower than the psapi functions and it has extra convertion requirements such as String^ to wchar or String (which I have not tested)
推荐答案
上面的答案适用于win 8.这里没有wstring的东西和剥离的路径
the above answer works on win 8. here it is without the wstring stuff and stripping off the path
#include <tlhelp32.h>
DWORD FindProcessId(char* processName)
{
// strip path
char* p = strrchr(processName, '\\');
if(p)
processName = p+1;
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if ( processesSnapshot == INVALID_HANDLE_VALUE )
return 0;
Process32First(processesSnapshot, &processInfo);
if ( !strcmp(processName, processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while ( Process32Next(processesSnapshot, &processInfo) )
{
if ( !strcmp(processName, processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
这篇关于检查一个特定的进程是否在Windows上使用C ++运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!