Windows API 之 CreateToolhelp32Snapshot

时间:2024-01-14 16:50:26

CreateToolhelp32Snapshot:

参考:

https://msdn.microsoft.com/en-us/library/ms682489%28VS.85%29.aspx

HANDLE WINAPI CreateToolhelp32Snapshot(
_In_ DWORD dwFlags,
_In_ DWORD th32ProcessID
);
#include <windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <tchar.h> using namespace std; DWORD GetProcessIDByName(TCHAR *szProcName)
{
DWORD dwProcID = ;
HANDLE hProcessSnap;
PROCESSENTRY32 pe32; //获取系统中全部进程的快照 Take a snapshot of all processes in the system.
//TH32CS_SNAPPROCESS:在快照中包含系统中所有的进程。
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, );
if (INVALID_HANDLE_VALUE == hProcessSnap)
return(FALSE); pe32.dwSize = sizeof(PROCESSENTRY32); // 获取第一个进程的信息 Retrieve information about the first process,
// 若获取失败则退出 and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
// 清除快照 clean the snapshot object
CloseHandle(hProcessSnap);
cout << "!!! Failed to gather information on system processes! \n" << endl;
return(NULL);
}
//匹配进程ID Matching Process ID
do
{
if (!strcmp(szProcName,pe32.szExeFile))
{
//拷贝进程名 Copy Process Name
cout << pe32.szExeFile << " : " << pe32.th32ProcessID << endl;
dwProcID = pe32.th32ProcessID;
}
//cout << pe32.szExeFile << endl;
} while (Process32Next(hProcessSnap, &pe32)); // 清除快照 clean the snapshot object
CloseHandle(hProcessSnap); return dwProcID;
} int main()
{
TCHAR *szProcessName = "chrome.exe";
GetProcessIDByName(szProcessName); system("pause");
return ;
}

找到对应的进程ID:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKgAAABJCAIAAAAmMfrEAAAMG0lEQVR4nO1d91sTWRfOfyK7Ii52xdW10OyASEejiMsidpBiKumkkQSUIt2ysvqBCEhHQYiUaOhGWd3yx3w/DJuNc++cuRNDApt5n/P4zNx57znvPe8dUghRIFUZqBAr0dCLlQaJ0ihRGaV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