C++進程檢測,發現進程路徑,關閉進程(二)

來源:互聯網
上載者:User

C++進程檢測,發現進程路徑,關閉進程(二)

上一章使用的函數比較複雜,本章直接使用根據進程ID號並使用GetModuleFileNameEx函數實現進程路徑的擷取

#include "stdafx.h"#include <windows.h>#include "stdio.h"#include <tlhelp32.h>#include "Psapi.h"int _tmain(int argc, _TCHAR* argv[]){int num=0;TCHAR exe_name[20]=_T("notepad++.exe");//要查詢的進程名TCHAR pszFullPath[MAX_PATH];PROCESSENTRY32 pe32;//用於存放進程資訊的結構體HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//建立進程快照pe32.dwSize=sizeof(pe32);if(hProcessSnap==INVALID_HANDLE_VALUE){printf("CreateToolhelp32Snapshot failed!\n");return -1;}BOOL bMore=::Process32First(hProcessSnap,&pe32);//擷取第一個進程資訊到pe32結構體中while(bMore){printf("name is: %ls\n",pe32.szExeFile);printf("num is: %d\n",num);printf("ID is: %d\n",pe32.th32ProcessID);if(!_tcscmp(exe_name,pe32.szExeFile))//發現要尋找的進程後結束尋找{printf("find the file you want: %ls\n",pe32.szExeFile);break;}num++;bMore=::Process32Next(hProcessSnap,&pe32);}if(!_tcscmp(exe_name,pe32.szExeFile)){HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);TCHAR pBuffer[MAX_PATH+1];GetModuleFileNameEx(hProcess,NULL,pBuffer,MAX_PATH+1);//直接使用GetModuleFileNameEx函數擷取進程檔案路徑printf("file name is: %ls\n\n",pBuffer);HANDLE hprocess=::OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32.th32ProcessID);if(hprocess!=NULL){::TerminateProcess(hprocess,0);//關閉進程printf("I have close the process you choose!\n");::CloseHandle(hprocess);}}CloseHandle(hProcessSnap);getchar();//讓程式有輸入,在結束return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.