vc关闭外部程序


#include <Windows.h>
#include <iostream>
using namespace std;

int main()
{
WinExec( "C://WINDOWS//NOTEPAD.EXE", SW_SHOW);
...
return 0;
}


终止指定进程

#include "tlhelp32.h"

void KillExe(const char* szExeName)
{
if (szExeName == NULL)
return 0;
int lsr = lstrlen(szExeName);
if (lsr == 0)
return 0;

PROCESSENTRY32 my;
HANDLE l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (((int)l) != -1)
{
my.dwSize = sizeof(my);
if (Process32First(l, &my))
{
do
{
if (lstrcmpi(szExeName, my.szExeFile + lstrlen(my.szExeFile) - lsr) == 0)
{
HANDLE hProcessHandle;
hProcessHandle = OpenProcess(0x1F0FFF, true, my.th32ProcessID);
if (hProcessHandle != NULL)
{
if (TerminateProcess(hProcessHandle, 0) != NULL)
{
CloseHandle(hProcessHandle);
}
}
CloseHandle(l);
return;
}
}while (Process32Next(l, &my));
}
CloseHandle(l);
}
}


调用方式
KillExe("c://windows//notepad.exe");

完整或部分路径名,不区分大小写

你可能感兴趣的:(c,null,include,iostream)