跨平台杀进程

为Windows和Linux平台。

 

#include <string> #include <algorithm> #include <functional> #include <map> #include <time.h> #include <vector> #include <sstream> #include <fstream> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <errno.h> #ifdef WIN32 #include <winsock2.h> #include <process.h> #pragma comment(lib, "Ws2_32.lib") #include <direct.h> #include "tlhelp32.h" #include "shellapi.h" #else #include <unistd.h> #include <dirent.h> #include <stdlib.h> #include <sys/procfs.h> #include <unistd.h> #include <stropts.h> #include <fcntl.h> #include <signal.h> #endif /** * @brief killProcess * * Detailed description. * @param[in] processName * @return bool */ inline bool killProcess(std::string processName) { #ifdef WIN32 DWORD pid; while (processExist(processName, pid)) { HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid); // gets handle to process if (hProcess) { BOOL ret = TerminateProcess(hProcess, 0); // Terminate process by handle CloseHandle(hProcess); // close the handle if (!ret) { return false; } } else { return false; } } #else pid_t pid; while (processExist(processName, pid)) { int ret = kill(pid, SIGKILL); if(-1 == ret) { return false; } } #endif return true; }

 

其中processExist函数的实现为:

http://blog.csdn.net/donhao/archive/2010/06/01/5638249.aspx

 

你可能感兴趣的:(windows,linux,String,kill,平台,跨平台)