1,221
社区成员
发帖
与我相关
我的任务
分享
#include "iKillProgram.h" // 杀死指定进程;关闭外部程序;关闭指定进程
// #include "iKillProgram.cpp" // 杀死指定进程;关闭外部程序;关闭指定进程
#ifndef _iKillProgram_cpp_
#define _iKillProgram_cpp_
#include "iGetModuleFileNameEx.h" // 列举(列出)所有系统进程
BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam)
{
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_QUIT, 0, 0); // 只返回是否成功发送了消息,而不管结果是成功还是失败
//SendMessage(hwnd,WM_CLOSE,NULL,NULL); // 能收到处理后的结果是成功还是失败
return FALSE; // 此处返回值必须是大写的FALE,改成TRUE等关闭程序就不好用了?
}
return TRUE;
}
//#include <tlhelp32.h>
int KillProgram( // 关闭指定全路径的程序
// 要关闭程序的全路径
String ExeName
){
int T_SQKH=0;
if(L"" == ExeName.Trim())
{
return T_SQKH;
}
HANDLE snapshot;
PROCESSENTRY32 processinfo; // #include <tlhelp32.h>
processinfo.dwSize = sizeof (processinfo);
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(NULL == snapshot)
{
return T_SQKH;
}
bool status = Process32First(snapshot,&processinfo);
while(status)
{
//TListItem *li = ListView1->Items->Add();
String buffer;
int length;
buffer.SetLength(512);
//length = sprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
length = swprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
buffer.SetLength(length);
//li->Caption = buffer;
buffer.SetLength(512);
length = swprintf(buffer.w_str(),L"%08X",processinfo.th32ParentProcessID);
buffer.SetLength(length);
if(ExeName.LowerCase()==ProcessNameAndID(processinfo.th32ProcessID).LowerCase())
{
bool T_GKTQ=EnumWindows((WNDENUMPROC)EnumWinProc,processinfo.th32ProcessID);
T_SQKH=T_SQKH+((T_GKTQ == FALSE) ? 1 : 0);
}
status = Process32Next(snapshot,&processinfo);
}
return T_SQKH;
}
int KillProgramFiles( // 关闭指定目录中运行的除自身外的所有程序
// 要关闭的程序所在的目录
String FilePath
,
// 当前程序的全路径,当主程序在FilePath目录中时用次参数避免被关闭
String T_BCQY
){
int T_NPJY=0;
if(L"" == FilePath.Trim())
{
return T_NPJY;
}
if(L"\\" != FilePath.SubString( FilePath.Length(),1) )
{
FilePath = FilePath+L"\\"; // 如果Path最后一个字符不是“\”就在后面加上“\”
}
HANDLE snapshot;
PROCESSENTRY32 processinfo; // #include <tlhelp32.h>
processinfo.dwSize = sizeof (processinfo);
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(NULL == snapshot)
{
return T_NPJY;
}
bool status = Process32First(snapshot,&processinfo);
while(status)
{
//TListItem *li = ListView1->Items->Add();
String buffer;
int length;
buffer.SetLength(512);
//length = sprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
length = swprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
buffer.SetLength(length);
//li->Caption = buffer;
buffer.SetLength(512);
length = swprintf(buffer.w_str(),L"%08X",processinfo.th32ParentProcessID);
buffer.SetLength(length);
if(
(ProcessNameAndID(processinfo.th32ProcessID).LowerCase()).Pos(FilePath.LowerCase())>0
){
if(
T_BCQY.LowerCase()!=ProcessNameAndID(processinfo.th32ProcessID).LowerCase()
){
bool T_FSHI=EnumWindows((WNDENUMPROC)EnumWinProc,processinfo.th32ProcessID);
T_NPJY=T_NPJY+StrToInt(((T_FSHI == FALSE) ? 1 : 0));
}
}
status = Process32Next(snapshot,&processinfo);
}
return T_NPJY;
}
#endif
/*// 使用方法:
void __fastcall TForm1::Button1Click(TObject *Sender)
{ // 关闭指定全路径的程序
//KillProgram(L"R:\\NOTEPAD.EXE"); // 关闭程序
//KillProgram(ExpandFileName(Application->ExeName).w_str()); // 关闭程序自身
KillProgram(ExpandFileName(Application->ExeName)); // 关闭程序自身
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ // 关闭指定目录中运行的除自身外的所有程序
int T_APEZ=KillProgramFiles( // 关闭指定目录中运行的除自身外的所有程序
// 要关闭的程序所在的目录
ExtractFilePath(ExpandFileName(Application->ExeName))
,
// 当前程序的全路径,当主程序在FilePath目录中时用此参数避免被关闭
ExpandFileName(Application->ExeName)
);
ShowMessage(IntToStr(T_APEZ));
}
*/
TerminateProcess(procStruct.hProcess,0);
用这个语句结束进程,