小小的测试一下代码,关闭QQ,实际上和C语言一样,只是长字符麻烦了一点!
#include<stdio.h>
#include<Windows.h>
#include<TlHelp32.h>
BOOL TerminateProcessFromID(DWORD dwId);//终止一个进程
//获取正在运行的程序进程列表
int main(int argc, char *argv[])
{
PROCESSENTRY32 pe32;
//使用该结构之前声明大小
pe32.dwSize = sizeof(pe32);
//给系统内所有进程一个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf_s("CreateToolhelp32Snapshot 调用失败!\n");
return -1;
}
//遍历快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
while(bMore)
{
printf_s("进程名称:");
wprintf_s(L"%ls",pe32.szExeFile);
printf_s("\n进程ID:%u\n",pe32.th32ProcessID);
bMore = ::Process32Next(hProcessSnap,&pe32);
if(wcscmp(pe32.szExeFile,L"QQ.exe")==0)
{
if(TerminateProcessFromID(pe32.th32ProcessID))
{
printf("成功终止进程!\n");
}else{
printf("终止进程失败!\n");
}
}
printf("\n");
}
//清除Snapshot对象
::CloseHandle(hProcessSnap);
return 0;
}
BOOL TerminateProcessFromID(DWORD dwId)
{
BOOL bRet = FALSE;
//打开目标程序,取得句柄
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwId);
if(hProcess!=NULL)
{
//终止进程
bRet = ::TerminateProcess(hProcess,0);
}
return bRet;
}
#include<Windows.h>
#include<TlHelp32.h>
BOOL TerminateProcessFromID(DWORD dwId);//终止一个进程
//获取正在运行的程序进程列表
int main(int argc, char *argv[])
{
PROCESSENTRY32 pe32;
//使用该结构之前声明大小
pe32.dwSize = sizeof(pe32);
//给系统内所有进程一个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf_s("CreateToolhelp32Snapshot 调用失败!\n");
return -1;
}
//遍历快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
while(bMore)
{
printf_s("进程名称:");
wprintf_s(L"%ls",pe32.szExeFile);
printf_s("\n进程ID:%u\n",pe32.th32ProcessID);
bMore = ::Process32Next(hProcessSnap,&pe32);
if(wcscmp(pe32.szExeFile,L"QQ.exe")==0)
{
if(TerminateProcessFromID(pe32.th32ProcessID))
{
printf("成功终止进程!\n");
}else{
printf("终止进程失败!\n");
}
}
printf("\n");
}
//清除Snapshot对象
::CloseHandle(hProcessSnap);
return 0;
}
BOOL TerminateProcessFromID(DWORD dwId)
{
BOOL bRet = FALSE;
//打开目标程序,取得句柄
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwId);
if(hProcess!=NULL)
{
//终止进程
bRet = ::TerminateProcess(hProcess,0);
}
return bRet;
}
当前还没有任何评论