判断QT程序是否重复运行

打开exe,再次打开进行提示。

main.cpp添加:

复制代码
#include "QtFilePreview.h"
#include <QtWidgets/QApplication>
#include <windows.h>
#include <qmessagebox.h>
#pragma execution_character_set("utf-8")


bool CheckRun()
{
    //  创建互斥量
    HANDLE m_hMutex = CreateMutex(NULL, FALSE, L"onlyone");
    //  检查错误代码
    if (GetLastError() == ERROR_ALREADY_EXISTS) {
        //  如果已有互斥量存在则释放句柄并复位互斥量
        CloseHandle(m_hMutex);
        m_hMutex = NULL;
        //  程序退出
        return  false;
    }
    else
        return true;
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    if (CheckRun() == false)
    {
        QMessageBox::information(nullptr,"tips","this program has already been run\n程序已经运行");
        return 0;
    }
    QtFilePreview w;
    w.show();
    return a.exec();
}
相关推荐
用户8055336980318 小时前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner18 小时前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz6 天前
QML Hello World 入门示例
qt
xcyxiner9 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner9 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner10 天前
DicomViewer (添加模型类)3
qt
xcyxiner11 天前
DicomViewer (目录调整) 2
qt
xcyxiner11 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00612 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术12 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript