#include <QSharedMemory>
#include <QMessageBox>
#include <QProcess>
int main(int argc, char *argv[])
{
QSharedMemory shared("APP");
if(shared.attach()){
int app_pid = 0;
QMessageBox box;
box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
box.setText(QStringLiteral("程序已启动,是否关闭上一次执行程序"));
if(box.exec() == QMessageBox::Yes){
QProcess process;
process.start("tasklist.exe /FI \"IMAGENAME eq app.exe\"");
if(process.waitForFinished()){
QByteArray result = process.readAll();
int left = result.indexOf("Console",1);
QByteArray m_temp = result.mid(left-6,5);
QString m_str = m_temp;
app_pid = m_str.replace(" ","").toInt();
}
QString cmd = QString("taskkill /F /PID %1 /T").arg(app_pid);
process.execute(cmd);
}else{
exit(0);
}
}
shared.create(1);
QApplication a(argc, argv);
Widget w;
w.move(0,0);
w.show();
return a.exec();
}
|