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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

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

發(fā)布時(shí)間:2025/3/15 windows 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows编程—杀死指定路径程序文件的进程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

由于Windows命令 taskkill 無(wú)法通過(guò)程序文件的完整路徑匹配來(lái)殺死指定進(jìn)程,通過(guò)程序名稱(chēng)容易誤殺進(jìn)程,所有筆者做了一個(gè)簡(jiǎn)單的封裝做了個(gè)mytaskkill.exe,用來(lái)殺死指定路徑的程序文件的進(jìn)程。

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

源碼

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

核心代碼如下:

/*殺死指定路徑程序的所有進(jìn)程 */ 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 的進(jìn)程。成功: 已終止 PID 為 6056 的進(jìn)程。*/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") ){// 沒(méi)有這個(gè)進(jìn)程名cout << strName << " is not runing" << endl;return FALSE;}// 下面for代碼:可以?xún)?yōu)化使用正則表達(dá)式來(lái)獲取程序完整路徑和程序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;}// 程序路徑可能有空格,倒數(shù)第2項(xiàng)為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; }

程序及代碼下載

編譯好的可執(zhí)行程序和源代碼下載點(diǎn)擊這里。

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。