效果如下:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string>
#include <tlhelp32.h>
BOOL judge = FALSE;
DWORD GetProcessID(char *ProcessName){
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE){
printf("CreateToolhelp32Snapshot error\n");
return 0;
}
BOOL bProcess = Process32First(hProcessSnap, &pe32);
while (bProcess){
char* str1 = pe32.szExeFile;
char* str2 = ProcessName;
char arr1[255] = { 0 };
strcpy(arr1, str1);
memcpy(arr1, str1, sizeof(arr1));
char arr2[255] = { 0 };
strcpy(arr2, str2);
memcpy(arr2, str2, sizeof(arr2));
if (strcmp(strupr(arr1), strupr(arr2)) == 0){
judge = true;
}
bProcess = Process32Next(hProcessSnap, &pe32);
}
CloseHandle(hProcessSnap);
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
GetProcessID("chrome.exe");
if (judge == TRUE){
MessageBox(NULL, "find chrome.exe ","Test",0);
}else{
MessageBox(NULL, "not find chrome.exe", "Test", 0);
}
return 0;
}
|