#include <iostream>
using namespace std;
//#include <Windows.h>
#include <afx.h>
#include"tlhelp32.h"
DWORD CheckModule(char* ProcessName)
{//传入的参数是要搜索的进程名
HANDLE hSnapshot;
PROCESSENTRY32 lppe;
BOOL Find;
BOOL Found;
hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//创建进程快照
lppe.dwSize=sizeof(PROCESSENTRY32);
Found=Process32First(hSnapshot,&lppe);//得到第一个进程
while(Found)//进程不是空
{
if(strcmpi(ProcessName,lppe.szExeFile)==0)//进程名比较
return lppe.th32ProcessID;//一样即反回进程ID
Found = Process32Next(hSnapshot,&lppe);//得到下一个进程
}
CloseHandle(hSnapshot);
return NULL;
}
int main()
{
DWORD thesignal = CheckModule("vlc.exe");
if (thesignal == 0)
{
int thesignal = MessageBox(NULL, "进行了误操作,是否退出?", "警告", MB_YESNO);
if (thesignal == IDNO)
{
return 0;
}
else if (thesignal == IDYES)
{
exit(0);
}
}
cout<<"获取进程pid"<<endl;
return 0;
}
PROCESSENTRY32 结构体需包夹这个头文件#include"tlhelp32.h"