根据进程名获得进程ID(不区分进程名大小写)

来源:互联网 发布:生死狙击狸猫js直播间 编辑:程序博客网 时间:2024/06/01 08:08

其实在应用层和驱动从呢个,根据进程名进行一些操作还是非常常见的。这里简单写了一个应用层,根据进程名,返回进程ID的小函数。

#include <windows.h>#include <Tlhelp32.h>#include <string.h>DWORD GetProcessIdByName(LPSTR pName){HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if (INVALID_HANDLE_VALUE == hSnapshot) {return NULL;}PROCESSENTRY32 pe = { sizeof(pe) };BOOL fOk;for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe)) {if (!strcmp(_strlwr(pe.szExeFile), _strlwr(pName))) {CloseHandle(hSnapshot);return pe.th32ProcessID;}}return 0;}int main(){HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetProcessIdByName("NoTepad.exe"));TerminateProcess(hProcess, 0);CloseHandle (hProcess); return 0;}

都是自解释的,将就看吧。工程下载链接:下载