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

2/22/2017来源:ASP.NET技巧人气:1278

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