Qt保证只有一个实例(将CreateMutex得到的handle通过转换得到值)

使用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();
}

砖头不离身推荐阅读:

Vega Prime的简介

http://blog.sina.com.cn/s/blog_668aae7801017gib.html

posted @ 2015-12-17 19:45  findumars  Views(1546)  Comments(0Edit  收藏  举报