内核对象 互斥体 CreateMutex 跨进程边界共享内核对象 命名对象 程序单实例

来源:互联网 发布:美国黑人政治正确知乎 编辑:程序博客网 时间:2024/06/05 02:15

1、相关api

CreateMutex

2、api说明

3、举个栗子

#include <windows.h>HANDLE g_hAppInstance = NULL;HANDLE g_hAppMutex = NULL;int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){    try {        g_hAppInstance = hInstance;        // 创建互斥体:1、程序单实例运行;2、避免程序运行时安装或卸载程序;3、程序运行时传递命令行参数。        SetLastError (ERROR_SUCCESS);        g_hAppMutex = CreateMutex (NULL, FALSE, _T("命名对象名称"));        DWORD dwError = GetLastError ();        if (ERROR_ALREADY_EXISTS == dwError) {            // ...传递命令行参数...            CloseHandle (g_hAppMutex);            g_hAppMutex = NULL;            return 0;        }    }    catch (Exception& e) {//      SystemLog::getInstance ()->log (ERROR_LOG_TYPE, e.toFullString ().getCStr());    }    catch (...) {//      string err;//      err.format (_T("%s %s %d"), String(__FILE__).getCStr (), String (__FUNCTION__).getCStr (), __LINE__);//      SystemLog::getInstance ()->log (ERROR_LOG_TYPE, err.getCStr ());    }    return 0;}
0 0