互斥量实现单exe实例运行

说明:需要同路径下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 object

CWinApp 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中,可以使用QSharedMemory类实现实例运行。具体实现步骤如下: 1. 在主函数中创建一个QSharedMemory对象,并给它一个唯一的标识符。 2. 调用QSharedMemory::create()函数来创建共享内存区域。如果该函数返回false,则说明已经有一个实例运行,直接退出程序即可。 3. 如果create()函数返回true,则说明当前是第一个实例,可以正常运行程序。在程序退出时,需要调用QSharedMemory::detach()函数来释放共享内存区域。 以下是示例代码: ```cpp #include <QApplication> #include <QSharedMemory> #include <QDebug> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 设置共享内存标识符(可以随意设置,但必须是唯一的) QString sharedMemKey = "MyApplicationKey"; // 创建共享内存区域 QSharedMemory sharedMem(sharedMemKey); if (!sharedMem.create(1)) { qDebug() << "Another instance is already running."; return 0; } // 正常运行程序 // ... // 程序退出时释放共享内存区域 sharedMem.detach(); return app.exec(); } ``` 在上述代码中,我们创建了一个唯一的标识符“MyApplicationKey”,并调用了QSharedMemory::create()函数来创建共享内存区域。如果已经有一个实例运行,则该函数会返回false,程序直接退出。如果当前是第一个实例,则该函数会返回true,程序可以正常运行。在程序退出时,我们调用了QSharedMemory::detach()函数来释放共享内存区域。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值