13,825
社区成员
发帖
与我相关
我的任务
分享
#include <registry.hpp>
// 从注册表中获取Excel.exe的路径
String CrnGetExcelAppPath()
{
TRegistry *reg = new TRegistry;
reg->RootKey = HKEY_LOCAL_MACHINE;
reg->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\excel.exe", false);
String strExcelAppPath = reg->ReadString("");
reg->CloseKey();
delete reg;
return strExcelAppPath;
}
//---------------------------------------------------------------------------
#include <tlhelp32.hpp>
int CloseProcess(String strFileName)
{
String strExeFile;
BOOL bTPed(TRUE);
PROCESSENTRY32 pe32 = { sizeof(pe32) };
MODULEENTRY32 me32 = { sizeof(me32) };
HANDLE hSnapShot, hModuleSnap;
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapShot == NULL)return -1;
bool bFlag = Process32First(hSnapShot, &pe32);
while(bFlag){
strExeFile = String(pe32.szExeFile);
if(strExeFile.Pos("\\") == 0){ //如果不带路径,只模糊比对进程名,去掉此条件判断
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, pe32.th32ProcessID);
if(hModuleSnap == INVALID_HANDLE_VALUE){
strExeFile = String(pe32.szExeFile);
}
else{
if(Module32First(hModuleSnap, &me32)){
strExeFile = String(me32.szExePath); //获得进程全路径名
}
}
CloseHandle(hModuleSnap);
}
if(strExeFile.UpperCase() == strFileName.UpperCase()){
//关闭指定的进程
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE,FALSE,pe32.th32ProcessID);
bTPed=::TerminateProcess(hProcess,4);
}
bFlag = Process32Next(hSnapShot, &pe32);
}
CloseHandle(hSnapShot);
if(!bTPed)return -2;
return 0;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//CloseProcess("excel.exe");
CloseProcess("C:\\Program Files\\Microsoft Office\\Office10\\excel.exe");
}
//---------------------------------------------------------------------------