互斥量实现单exe实例运行
来源:互联网 发布:淘宝卖家版下载官方 编辑:程序博客网 时间:2024/05/01 12:30
说明:需要同路径下exe只有一个实例运行,不同路径下同名exe不互相影响。
技术实现:使用内核对象mutex实现进程间互斥。
vs2005创建win32工程(支持MFC),字符集为unicode。
// MutexTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "MutexTest.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// The one and only application objectCWinApp theApp;HANDLE m_hMutex;using namespace std;//获取互斥量名称CString GetMutexName();int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ int nRetCode = 0; CString strMutexName = GetMutexName(); m_hMutex = CreateMutex(NULL, TRUE, strMutexName); // 检测是否已经创建Mutex // 如果已经创建,就终止进程的启动 if ((m_hMutex != NULL) && (GetLastError() == ERROR_ALREADY_EXISTS)) { ReleaseMutex(m_hMutex); return nRetCode; } // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. } wcout<<(const TCHAR*)strMutexName<<endl; Sleep(10000); if (m_hMutex != NULL) { ReleaseMutex(m_hMutex); CloseHandle(m_hMutex); } return nRetCode;}void GetModulePath(CString& strPath){ TCHAR szFileNames[260]; //获取主模块的绝对路径 DWORD dwLen = GetModuleFileName(NULL, szFileNames, sizeof(szFileNames)); for(DWORD offset=dwLen; offset>=0; offset--) { if(szFileNames[offset] == '\\') { szFileNames[offset] = '\0'; break; } } strPath = szFileNames; strPath += "\\";}CString GetMutexName(){ //不同路径下名称相同的exe可同时运行 CString strModePath = _T(""); GetModulePath(strModePath); strModePath.TrimRight(_T("\\")); CString strMutexName = strModePath + _T("\\CSingletonApp"); strMutexName.Replace(_T("\\"), _T("+")); return strMutexName;}
- 互斥量实现单exe实例运行
- 利用共享数据段实现限制EXE单实例运行
- 实现单实例运行
- C#单实例运行的实现
- C# Mutex实现程序单实例运行
- WPF实现单例运行 - 唯一实例运行
- 使用Mutex实现会话状态下单实例运行和系统范围内单实例运行
- 使用Mutex实现会话状态下单实例运行和系统范围内单实例运行
- 单实例运行
- VB 单实例运行
- 单实例运行程序
- WinForm单实例运行
- C# 单实例运行
- c# 单实例运行
- Winform 单实例运行
- C++ 单实例运行
- 程序单实例运行
- 利用C#线程机制实现应用程序的单实例运行
- Mootools和Jquery之间的选择问题
- 如何写出杀手级简历(针对程序员)
- DotNetBar 介绍
- 一元多项式的计算
- 黑马程序员——银行业务调度系统
- 互斥量实现单exe实例运行
- 网络知识点
- 2012第18周移动游戏推荐
- 集线器,路由器,交换机,网关设备之间的区别
- 前端图表系列之一 --Highchart
- hibernate 具有主外键关系的多表级联保存
- 游戏编程基础(四)GDI游戏循环
- Activity-Intent-Intent filters
- 一道看似简单的sql面试题(count,group by,distinct)