如何让应用程序只有一个实例
来源:互联网 发布:如何关闭手机数据功能 编辑:程序博客网 时间:2024/05/12 14:29
有一种方法,比较简单,用共享内存的形式。
#include <QMessageBox>#include <windows.h>const char *fn = "TT's Map";//If the function succeeds, the return value is an open handle to the specified file mapping object.//If the function fails, the return value is NULL.HANDLE fd = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, (LPCWSTR)fn );if( fd == NULL ){ create( fn, 16 );}else{ QMessageBox::information( NULL, "Error", "The programming is runing" ); return -1;}bool create( const char *fn, int len ){ if( fn == NULL || len < 0 ) { return false; } char *map = (char *)malloc( len ); if( !map ) { abort(); } if( *fn == '/' ) { ++fn; } if( len ) { HANDLE fd = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, len, (LPCWSTR)fn ); } return true;}
方法2:使用EXE程序的句柄
int main(int argc, char *argv[]){ QApplication a(argc, argv); QString str = "OMRON"; //OMRON.exe HWND hwnd = ::FindWindowW( NULL, str.toStdWString().c_str() ); if( hwnd ) //found { QMessageBox::warning(0, QObject::tr("Error"), QObject::tr("OMRON.exe is already running.")); QApplication::exit(); return; } return KernelExecute::Exec<OMRON_Main_Win>(a, true);}
方法3:使用 OpenMutex (为现有的一个已命名互斥体对象创建一个新句柄 )和 CreateMutex
用 CloseHandle来关闭一个已经打开的Mutex。
0 0
- 如何让应用程序只有一个实例运行
- 如何让Qt应用程序只有一个实例
- 如何让应用程序只有一个实例
- 如何让Qt应用程序只有一个实例
- 让应用程序只有一个实例
- 如何让应用程序只有一个实例在运行?
- 如何让应用程序只有一个实例在运行
- 让应用程序只有一个实例运行
- 应用程序只有一个实例
- 用C#让应用程序只有一个实例在运行
- 让程序只有一个应用程序实例在运行
- 用C#让应用程序只有一个实例在运行
- 如何让一个程序只有一个运行实例
- 如何让应用程序只启动一个实例
- 如何使应用程序只有一个实例在运行
- 如何确保只有一个应用程序实例在运行?
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- SQL数据库比较存储过程
- struts2 上传图片
- 字符串包含
- 【android】安卓的3d动画效果
- linux 磁盘挂载
- 如何让应用程序只有一个实例
- 二叉排序树及其C代码
- SGU 108 Self-numbers II
- 判断一个二叉树是不是平衡的(算法)
- 安卓开发经验分享:资源、UI、函数库、测试、构建
- UVA - 810 (A Dice problem 搜索)
- springMVC使用RequestBody注解声明接收json
- Rest API registration and login
- 初学spring