Commit 5b7c11ed by 刘帅

禁止重复启动

parent 20527f5d
...@@ -68,4 +68,4 @@ Thumbs.db ...@@ -68,4 +68,4 @@ Thumbs.db
# Binaries # Binaries
# -------- # --------
*.exe bin/*.exe
...@@ -24,6 +24,7 @@ SOURCES += main.cpp\ ...@@ -24,6 +24,7 @@ SOURCES += main.cpp\
HEADERS += widget.h \ HEADERS += widget.h \
fmcrypt.h \ fmcrypt.h \
qfmclient.h qfmclient.h \
app_single.h
FORMS += widget.ui FORMS += widget.ui
#ifndef APP_SINGLE
#define APP_SINGLE
#if defined WIN32 //for win
#include <windows.h>
bool checkOnly()
{
// 创建互斥量
HANDLE m_hMutex = CreateMutex(NULL, FALSE, L"FmclientUi" );
// 检查错误代码
if (GetLastError() == ERROR_ALREADY_EXISTS) {
// 如果已有互斥量存在则释放句柄并复位互斥量
CloseHandle(m_hMutex);
m_hMutex = NULL;
// 程序退出
return false;
}
else
return true;
}
#endif
#if defined Q_OS_LINUX //for linux
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
bool checkOnly()
{
const char filename[] = "/tmp/lockfile";
int fd = open (filename, O_WRONLY | O_CREAT , 0644);
int flock = lockf(fd, F_TLOCK, 0 );
if (fd == -1) {
perror("open lockfile/n");
return false;
}
//给文件加锁
if (flock == -1) {
perror("lock file error/n");
return false;
}
//程序退出后,文件自动解锁
return true;
}
#endif
#endif // APP_SINGLE
No preview for this file type
#include "widget.h" #include "widget.h"
#include <QApplication> #include <QApplication>
#include "qfmclient.h" #include "qfmclient.h"
#include "app_single.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
if(!checkOnly())
{
OutputDebugString(L"FmclientUi running.");
return 0;
}
Widget w; Widget w;
QFmClient client; QFmClient client;
QObject::connect(&client,&QFmClient::HideUi, &w, &Widget::hide,Qt::QueuedConnection); QObject::connect(&client,&QFmClient::HideUi, &w, &Widget::hide,Qt::QueuedConnection);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment