想弱弱的请教下关于进程的问题,请高手赐教!谢谢
现在碰到一个问题,在任务管理器中的进程,我需要监视CF的(一个应用程序)进程,一直扫描,当这个进程结束了,然后电脑马上shutdown -s。请大虾们赐教。谢谢!
#include <windows.h> #include <stdio.h> #include "psapi.h" #pragma comment( lib, "psapi" ) int main() { DWORD aProcesses[1024],cbNeeded,cbMNeeded; HMODULE hMods[1024]; HANDLE hProcess; char szProcessName[128]; if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))//枚举进程ID printf("Enumprocess error\n"); int j = (int) (cbNeeded / sizeof(DWORD)); for(int i=0; i< j; i++) { ZeroMemory(szProcessName,128); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);//获取进程实例 EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);//枚举该进程中所有的模块ID GetModuleFileNameEx( hProcess, hMods[0], szProcessName,128);//获取该模块的名字 0 为可执行PE文件的名字 printf("%d\t%s\n",aProcesses[i], szProcessName); CloseHandle(hProcess); } return 0; }这个简单些 不用枚举dll模块!
#include <windows.h> #include <stdio.h> #include "psapi.h" #pragma comment( lib, "psapi" ) bool FindProcess(const char* m_Processname){ DWORD aProcesses[1024],cbNeeded,cbMNeeded; HMODULE hMods[1024]; HANDLE hProcess; char szProcessName[128]; if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))//枚举进程ID printf("Enumprocess error\n"); int j = (int) (cbNeeded / sizeof(DWORD)); for(int i=0; i< j; i++) { ZeroMemory(szProcessName,128); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);//获取进程实例 EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);//枚举该进程中所有的模块ID GetModuleFileNameEx( hProcess, hMods[0], szProcessName,128);//获取该模块的名字 0 为可执行PE文件的名字 if(strstr(szProcessName,m_Processname)){ CloseHandle(hProcess); return 1; } //printf("%d\t%s\n",aProcesses[i], szProcessName); CloseHandle(hProcess); } return 0; } int main() { if(FindProcess("explorer.exe")){ printf("explorer.exe is existed"); } else printf("explorer.exe isn't existed"); return 0; }写成函数! 其实只需要其进程句柄就可以了 !也就是说枚举一次就可以了 然后保留ID进行判断
#include <windows.h> #include <stdio.h> #include "psapi.h" #include <conio.h> #pragma comment( lib, "psapi" ) unsigned long WINAPI Thread(PVOID pvoid); bool FindProcess(const char* m_Processname){ DWORD aProcesses[1024],cbNeeded,cbMNeeded; HMODULE hMods[1024]; HANDLE hProcess; char szProcessName[128]; if(!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))//枚举进程ID printf("Enumprocess error\n"); int j = (int) (cbNeeded / sizeof(DWORD)); for(int i=0; i< j; i++) { ZeroMemory(szProcessName,128); hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);//获取进程实例 EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);//枚举该进程中所有的模块ID GetModuleFileNameEx( hProcess, hMods[0], szProcessName,128);//获取该模块的名字 0 为可执行PE文件的名字 if(strstr(szProcessName,m_Processname)){ CloseHandle(hProcess); return 1; } //printf("%d\t%s\n",aProcesses[i], szProcessName); CloseHandle(hProcess); } return 0; } int main() { DWORD dwThreadId; HANDLE hThread = CreateThread( NULL,0,Thread,0,0,&dwThreadId); WaitForSingleObject(hThread,INFINITE); return 0; } unsigned long WINAPI Thread(PVOID pvoid) { MSG msg; PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE); UINT timerid=SetTimer(NULL,1,1000,NULL); //一秒触发一次的计时器 while(true) { GetMessage(&msg,NULL,0,0); if(msg.message==WM_TIMER){ if(FindProcess("explorer.exe")){ printf("explorer.exe is existed\n"); } else{ printf("explorer.exe isn't existed\n"); system("shutdown -s"); } } else { TranslateMessage(&msg); DispatchMessage(&msg); } } KillTimer(NULL,timerid); return 0; }完全的粘贴加复制 多简单!