【原创】关闭指定名字的进程,关闭指定id的进程, 取得指定进程的id
2012-10-16 21:30
405 查看
#include "stdafx.h" #include <windows.h> #include "tlhelp32.h" #include <string> #include <iostream> using std::string; using std::cout; using std::endl; // findprocessid // find the process identifer with the specified process name // proc_name [in] name of process // return value : the identifer of process, if return value is 0, indicate that fail to get process identifer DWORD FindProcessId(const char* proc_name_) { DWORD proc_id_ = 0; PROCESSENTRY32 proc_entry_; HANDLE handle_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(handle_ != INVALID_HANDLE_VALUE) { proc_entry_.dwSize = sizeof(proc_entry_); if(Process32First(handle_, &proc_entry_)) { do { if(stricmp(proc_entry_.szExeFile, proc_name_) == 0) { proc_id_ = proc_entry_.th32ProcessID; break; } }while(Process32Next(handle_, &proc_entry_)); } CloseHandle(handle_); } return proc_id_; } // TerminateProc // Terminate a process that is specified by process identifer // proc_id_ [in] indentifer of process // return value : true -- successful, false -- fail bool TerminateProc(DWORD proc_id_) { bool b_rtn = false; HANDLE handle_ = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id_); if(handle_ != INVALID_HANDLE_VALUE) { DWORD exit_code_; GetExitCodeProcess(handle_, &exit_code_); TerminateProcess(handle_, exit_code_); WaitForSingleObject(handle_, INFINITE); CloseHandle(handle_); b_rtn = true; } return b_rtn; } // TerminateProcSZ // Terminate a process that is specified by process window name // win_name_ [in] name of process window // return value : true -- successful, false -- fail bool TerminateProcSZ(const char* win_name_) { bool b_rtn = false; HWND h_wnd = FindWindow(NULL, win_name_); if(!h_wnd) { return b_rtn; } ::PostMessage(h_wnd, WM_CLOSE, 0,0); DWORD proc_id_ = 0; GetWindowThreadProcessId(h_wnd, &proc_id_); HANDLE handle_ = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id_); if(handle_ != INVALID_HANDLE_VALUE) { DWORD dw_wait = WaitForSingleObject(handle_, 2000); if(dw_wait == WAIT_TIMEOUT) { DWORD exit_code_; GetExitCodeProcess(handle_, &exit_code_); TerminateProcess(handle_, exit_code_); WaitForSingleObject(handle_, INFINITE); } CloseHandle(handle_); b_rtn = true; } return b_rtn; }
相关文章推荐
- 得到端口号进程ID和关闭指定端口
- C#关闭指定名字进程的方法
- 【分享】兼容各种Linux平台的关闭所有指定名字的进程的命令
- 利用ps命令查看进程id(部分原创,转载已著名)
- 关闭占用指定端口的进程
- MFC获得指定进程的ID,路径及终止进程
- 如何关闭指定端口的进程
- 关闭占用指定端口的进程
- 进程1:由进程ID获取进程名字
- 检查所有运行的进程,关闭某个指定的进程
- 获得进程的名字和ID(psapi)
- 根据exe名字查找进程并关闭
- DELPHI实现关闭指定进程,自身防杀
- 关于ztree打开关闭所有节点,选中指定id节点
- Windows的cmd命令查询指定端口占用的进程并关闭
- [linux] 查看占用指定端口进程ID
- 【原创】彻底关闭stormliv.exe进程 禁止stormliv再启动;3DMAX的服务设置里禁止adskscsrv.exe进程
- shell kill掉指定名字的进程
- Linux根据进程名字关闭Java进程
- 一条命令杀死指定名字的进程