Qt 关于应用程序只启动一个实例的问题

Qt 中可以使用纯Win32 API


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

你可能感兴趣的:(C++,c,windows,C#,qt)