//根据进程名获取PID DWORD CBiaoBai1Dlg::GetProcessIdByProcessName(char* pszProcessName) { //1.创建进程快照 HANDLE hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, //遍历进程快照1 0); //进程PID if (NULL == hSnap) { MessageBox("创建进程快照失败!"); return 0; } //2.获取第一条进程快照信息 PROCESSENTRY32 stcPe = { sizeof(stcPe) }; if (Process32First(hSnap, &stcPe)) { //3.循环遍历进程Next do { //获取快照信息 USES_CONVERSION; CString ProcessName = A2T(pszProcessName); if (!lstrcmp(stcPe.szExeFile, ProcessName)) { //4.关闭句柄 CloseHandle(hSnap); return stcPe.th32ProcessID; } } while (Process32Next(hSnap, &stcPe)); } //4.关闭句柄 CloseHandle(hSnap); return 0; }