日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows编程—杀死指定路径程序文件的进程

發布時間:2025/3/15 windows 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows编程—杀死指定路径程序文件的进程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

由于Windows命令 taskkill 無法通過程序文件的完整路徑匹配來殺死指定進程,通過程序名稱容易誤殺進程,所有筆者做了一個簡單的封裝做了個mytaskkill.exe,用來殺死指定路徑的程序文件的進程。

支持Windows xp 及以上版本,用法為:mytaskkill.exe “C:\xxx1.exe” “C:\xxx2.exe” “C:\xxx3.exe”

源碼

使用wmi命令和taskkill命令結合來殺死指定路徑程序文件的進程,使用了Windows管道讀取命令輸出信息進而找到指定路徑名進程的PID 然后使用taskkill 干掉這個PID。編碼中使用了 boost庫用來操作字符串,所以源碼編譯要自己指定boost頭文件和庫文件路徑。

核心代碼如下:

/*殺死指定路徑程序的所有進程 */ BOOL KillSpecifiedProcess(const std::string& p_strPath) {/*C:\Users\10139>wmic process where name="notepad.exe" get executablepath,processidExecutablePath ProcessIdC:\WINDOWS\system32\notepad.exe 6196C:\WINDOWS\system32\notepad.exe 6056C:\Users\10139>taskkill /F /PID 6196 /PID 6056成功: 已終止 PID 為 6196 的進程。成功: 已終止 PID 為 6056 的進程。*/if(!boost::filesystem::exists(p_strPath)){cout << p_strPath << " not exist" << endl;return FALSE;}int index = p_strPath.rfind("\\");std::string strName = p_strPath.substr(index + 1);SECURITY_ATTRIBUTES sa;sa.nLength = sizeof(SECURITY_ATTRIBUTES);sa.bInheritHandle = TRUE;sa.lpSecurityDescriptor = NULL;HANDLE hStdOutRead = NULL, hStdOutWrite = NULL;if (!CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0)){cout << "create pipe error," << GetLastError() << endl; return FALSE;}STARTUPINFOA startInfo;PROCESS_INFORMATION procInfo;BOOL bSuccess = FALSE;ZeroMemory(&procInfo, sizeof(PROCESS_INFORMATION));ZeroMemory(&startInfo, sizeof(STARTUPINFOA));startInfo.cb = sizeof(STARTUPINFOA);startInfo.hStdOutput = hStdOutWrite;startInfo.dwFlags |= (STARTF_USESTDHANDLES |STARTF_USESHOWWINDOW) ;startInfo.wShowWindow = SW_HIDE;boost::format fmt("wmic process where name=\"%1%\" get executablepath,processid");fmt % strName;std::string strSQL = fmt.str();bSuccess = CreateProcessA(NULL, (char*)strSQL.data(), NULL, NULL, TRUE, 0, NULL, NULL, &startInfo, &procInfo);if (!bSuccess){cout << "create process error," << GetLastError() << endl;return FALSE;}WaitForSingleObject(procInfo.hProcess,INFINITE);CloseHandle(hStdOutWrite);DWORD byteRead = 0;std::string strContent;char buffer[READ_ONE_NUM] = {0};while (true){byteRead = 0;memset(buffer, 0, READ_ONE_NUM);BOOL bRead = ReadFile(hStdOutRead, buffer, (READ_ONE_NUM-1)* sizeof(buffer[0]) , &byteRead, NULL);if (!bRead){break;}strContent.append(buffer);}CloseHandle(hStdOutRead);std::vector<std::string> splitVec;boost::split(splitVec, strContent, boost::is_any_of("\r\n"), boost::token_compress_on);if(splitVec.size() > 0){if( !boost::icontains(splitVec[0], "ExecutablePath") ){// 沒有這個進程名cout << strName << " is not runing" << endl;return FALSE;}// 下面for代碼:可以優化使用正則表達式來獲取程序完整路徑和程序PID// 第1行和最后1行都不是for(int i = 1; i < splitVec.size() -1; i++){std::vector<std::string> splitVec2;boost::split(splitVec2, splitVec[i], boost::is_any_of(" "), boost::token_compress_on);int size = splitVec2.size();if(size >= 3){std::string exePath;// 取到同名程序的完整路徑for(int i = 0; i < size -1 -1; i++){exePath.append(splitVec2[i]);exePath.append(" ");}// 判定路徑是否完全匹配if( !boost::icontains(exePath, p_strPath) ){continue;}// 程序路徑可能有空格,倒數第2項為pidstd::string pId = splitVec2[size -1 -1];std::string cmd = "taskkill /F /PID ";cmd.append(pId);cout << p_strPath << "->" << cmd << endl;WinExec(cmd.c_str(), SW_HIDE);}}}return TRUE; }

程序及代碼下載

編譯好的可執行程序和源代碼下載點擊這里。

總結

以上是生活随笔為你收集整理的Windows编程—杀死指定路径程序文件的进程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。