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

      使用CreateMutex 可以实现只启动一个应用程序实例

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

猜你喜欢

转载自blog.csdn.net/lengyuezuixue/article/details/81433388
今日推荐