c++结束进程命令(c++ 如何关闭所有进程)

本文目录
c++ 如何关闭所有进程
#pragma once
//////////////////
// Process iterator -- iterator over all system processes
// Always skips the first (IDLE) process with PID=0.
class CProcessIterator {
protected:
DWORD* m_pids; // array of procssor IDs
DWORD m_count; // size of array
DWORD m_current; // next array item
public:
CProcessIterator();
~CProcessIterator();
DWORD First();
DWORD Next() {
return m_pids && m_current 《 m_count ? m_pids : 0;
}
DWORD GetCount() {
return m_count;
}
};
//////////////////
// Handy class to facilitate finding and killing a process by name.
class CFindKillProcess {
public:
CFindKillProcess();
~CFindKillProcess();
DWORD FindProcess(LPCTSTR lpModname, BOOL bAddExe=TRUE);
BOOL KillProcess(DWORD pid, BOOL bZap);
};
////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EnumProc.h"
// CProcessIterator - Iterates all processes
CProcessIterator::CProcessIterator()
{
m_pids = NULL;
}
CProcessIterator::~CProcessIterator()
{
delete m_pids;
}
//////////////////
// Get first process: Call EnumProcesses to init array. Return first one.
DWORD CProcessIterator::First()
{
m_current = (DWORD)-1;
m_count = 0;
DWORD nalloc = 1024;
do {
delete m_pids;
m_pids = new DWORD ;
if (EnumProcesses(m_pids, nalloc*sizeof(DWORD), &m_count)) {
m_count /= sizeof(DWORD);
m_current = 1; // skip IDLE process
}
} while (nalloc 《= m_count);
return Next();
}
////////////////////////////////////////////////////////////////
// CFindKillProcess - to find/kill a process by module name.
//
CFindKillProcess::CFindKillProcess()
{}
CFindKillProcess::~CFindKillProcess()
{}
//////////////////
// Search for process whose module name matches parameter.
// Finds "foo" or "foo.exe"
DWORD CFindKillProcess::FindProcess(LPCTSTR modname, BOOL bAddExe)
{
CProcessIterator itp;
for (DWORD pid=itp.First(); pid; pid=itp.Next()) {
TCHAR name;
CProcessModuleIterator itm(pid);
HMODULE hModule = itm.First(); // .EXE
if (hModule) {
GetModuleBaseName(itm.GetProcessHandle(),hModule, name, _MAX_PATH);
string sModName = modname;
if (strcmpi(sModName.c_str(),name)==0)
return pid;
sModName += ".exe";
if (bAddExe && strcmpi(sModName.c_str(),name)==0)
return pid;
}
}
return 0;
}
//////////////////
// Kill a process cleanly: Close main windows and wait.
// bZap=TRUE to force kill.
BOOL CFindKillProcess::KillProcess(DWORD pid, BOOL bZap)
{
CMainWindowIterator itw(pid);
for (HWND hwnd=itw.First(); hwnd; hwnd=itw.Next()) {
DWORD bOKToKill = FALSE;
SendMessageTimeout(hwnd, WM_QUERYENDSESSION, 0, 0,
SMTO_ABORTIFHUNG|SMTO_NOTIMEOUTIFNOTHUNG,100, &bOKToKill);
if (!bOKToKill)
return FALSE; // window doesn’t want to die: abort
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
// I’ve closed the main windows; now wait for process to die.
BOOL bKilled = TRUE;
HANDLE hp=OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE,FALSE,pid);
if (hp) {
if (WaitForSingleObject(hp, 5000) != WAIT_OBJECT_0) {
if (bZap) { // didn’t die: force kill it if zap requested
TerminateProcess(hp,0);
} else {
bKilled = FALSE;
}
}
CloseHandle(hp);
}
return bKilled;
}
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EnumProc.h"
#define tpf _tprintf // to save typing
typedef list《string》 CStringList; // like MFC, but with STL
// pre-declare functions
int help();
// check for switch: / or -
inline BOOL isswitch(TCHAR c) { return c==L’/’ || c==L’-’; }
int main(int argc, TCHAR* argv)
{
CStringList cmdargs; // command-line args (processes to kill)
BOOL bDisplayOnly=FALSE; // don’t kill, just show results
BOOL bQuiet=FALSE; // suppress error messages
BOOL bZap=FALSE; // force-kill process
// Parse command line. Switches can come in any order.
for (int i=1; i《argc; i++) {
if (isswitch(argv)) {
for (UINT j=1; j《strlen(argv); j++) {
switch(tolower(argv)) {
case ’?’: help(); return 0;
case ’n’: bDisplayOnly=TRUE; break;
case ’q’: bQuiet=TRUE; break;
case ’z’: bZap=TRUE; break;
default:
return help();
}
}
} else {
cmdargs.push_back(argv); // got a non-switch arg: add to list
}
}
if (cmdargs.size()《=0)
help();
// Now iterate args (module names), killing each one
CStringList::iterator it;
for (it=cmdargs.begin(); it!=cmdargs.end(); it++) {
CFindKillProcess fkp;
DWORD pid = fkp.FindProcess(it-》c_str());
if (pid) {
if (bDisplayOnly) {
tpf(_T("Kill process %d(0x%08x)\n"),pid,pid);
} else {
fkp.KillProcess(pid, bZap);
}
} else if (!bQuiet) {
tpf(_T("Error: Can’t find process ’%s’.\n"),it-》c_str());
}
}
return 0;
}
int help()
{
tpf(_T("kp: Kill process from command line.\n"));
tpf(_T(" Copyright 2002 Paul DiLascia.\n\n"));
tpf(_T(" kp \n"));
tpf(_T(" where modnameN is a module name; eg foo or foo.exe\n"));
tpf(_T("\n"));
tpf(_T(" /n(othing) don’t kill, just show results\n"));
tpf(_T(" /q(uiet) don’t show errors\n"));
tpf(_T(" /z(ap) force kill (ignore WM_QUERYENDSESSION)\n"));
tpf(_T("\n"));
return 0;
}
用c/c++怎么关闭指定进程例如我的进程是a.exe,怎么用c/c++关闭求大神指点
taskkill /f /im "WDKeyMonitorCCB.exe" 这是cmd关闭正在运行程序的命令
***隐藏网址***
这是c/c++运行dos命令的办法
#include 《iostream》
using namespace std;
int main(int argc, char *argv)
{
system("taskkill /f /im ’notepad.exe’");
return 0;
}
我用的是记事本的进程 你可以换别的
[急]C++结束系统进程
#include 《windows.h》
#include 《iostream》
using namespace std;
int main()
{
int pid;
cout《《"please input pid:";//每个进程都有唯一的pid 系统分配的
cin》》pid;
//根据pid获取进程的权限,第一个代表全部权限
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
if(hProcess==NULL)
return -1;
int n= TerminateProcess(hProcess,0);//调用函数关闭进程 ,系统函数能不能没试过
//但是大部分进程都行
if(n==0)
{
cout《《"失败原因:"《《GetLastError();
return -1;
}
CloseHandle(hProcess);
return 0;
}
C++ 程序中什么函数可以立刻结束程序
exit 和 return
一般使用return,exit不建议使用(因为exit为退出所有程序 即在子程序、或者其他函数中使用的话 整个程序便退出了)
而用 return 就可以只退出当前程序或函数

更多文章:
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)
2026年9月7日 06:30



