使用CreateMutex 可以实现只启动一个应用程序实例
view plaincopy to clipboardprint?
#include <QApplication>
#include <QtNetwork>
#include "mydlg.h"
#ifdef Q_WS_WIN
#include <windows.h>
#endif
int main(int argc, char * argv[]) {
QApplication app(argc,argv);
#ifdef Q_WS_WIN
HANDLE hMutex = CreateMutex(NULL, true, QString("[‘{EFEB2EF6-F8E0-AE44-BABE-1BBEF2C7FD56}‘]").toStdWString().c_str());
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMutex);
QMessageBox::information(NULL, "Info", "Has been Run", "OK");
app.exit(1);
return 1;
}
#endif
myDlg *win = new myDlg;
win->showLoginForm();
return app.exec();
}
#include <QApplication>
#include <QtNetwork>
#include "mydlg.h"
#ifdef Q_WS_WIN
#include <windows.h>
#endif
int main(int argc, char * argv[]) {
QApplication app(argc,argv);
#ifdef Q_WS_WIN
HANDLE hMutex = CreateMutex(NULL, true, QString("[‘{EFEB2EF6-F8E0-AE44-BABE-1BBEF2C7FD56}‘]").toStdWString().c_str());
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMutex);
QMessageBox::information(NULL, "Info", "Has been Run", "OK");
app.exit(1);
return 1;
}
#endif
myDlg *win = new myDlg;
win->showLoginForm();
return app.exec();
}
砖头不离身推荐阅读:
http://blog.sina.com.cn/s/blog_668aae7801017gib.html