一、查找程序路径
通过window系统注册表,查询程序路径位置,比如我要启动百度管家
QCoreApplication::setOrganizationName(QString("Baidu"));
QCoreApplication::setApplicationName(QString("BaiduYunGuanjia"));
QSettings settings(QSettings::NativeFormat, QSettings::UserScope,
QCoreApplication::organizationName(),QCoreApplication::applicationName());
m_localBaiduPath = settings.value("installDir").toString();
m_localBaiduVersion = settings.value("Version").toString();
二、打开程序
博客:QT Process基本使用
三、查找程序PID
int MainWindow::processPid(const char *processName)
{
PROCESSENTRY32 pe32;
pe32.dwSize=sizeof(pe32);
HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
BOOL bMore=::Process32First(hProcessSnap,&pe32);
while(bMore) {
int len= WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, wcslen(pe32.szExeFile),
NULL, 0, NULL, NULL);
char* m_char = new char[len+1];
WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, wcslen(pe32.szExeFile),
m_char, len, NULL, NULL);
m_char[len]='\0';
if(strcmp(processName, m_char) == 0) {
return pe32.th32ProcessID;
}
bMore=::Process32Next(hProcessSnap,&pe32);
delete[] m_char;
}
return 0;
}
int pid = processPid("baiduyunguanjia.exe");
四、关闭进程
void MainWindow::exitProcesses()
{
PROCESSENTRY32 pe32;
pe32.dwSize=sizeof(pe32);
HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
BOOL bMore=::Process32First(hProcessSnap,&pe32);
while(bMore) {
int len= WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, wcslen(pe32.szExeFile),
NULL, 0, NULL, NULL);
char* m_char = new char[len+1];
WideCharToMultiByte(CP_ACP, 0, pe32.szExeFile, wcslen(pe32.szExeFile),
m_char, len, NULL, NULL);
m_char[len]='\0';
if(strcmp("baiduyunguanjia.exe", m_char) == 0) {
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
if(hProcess != NULL) {
TerminateProcess(hProcess, 0);
}
}
bMore=::Process32Next(hProcessSnap,&pe32);
delete[] m_char;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)