创建专有命名空间单实例程序

  1. #pragma once  
  2.   
  3. class CSingleton  
  4. {  
  5. public:  
  6.     CSingleton(LPCTSTR lpszBoundary = _T("MyAppBoundary"), LPCTSTR lpszNamespace = _T("MyAppNamespace"));  
  7.     virtual ~CSingleton(void);  
  8.   
  9. public:  
  10.     BOOL IsInstanceExist(LPCTSTR lpszMutex);  
  11.   
  12. private:  
  13.     void DUMPINFO(LPCTSTR lpszFormat, ...);  
  14.   
  15. private:  
  16.     HANDLE m_hSingleton;  
  17.     HANDLE m_hBoundary;  
  18.     HANDLE m_hNamespace;  
  19.     BOOL m_bNamespaceOpen;  
  20. };  
  21.   
  22. // Singleton.cpp  
  23. #include "stdafx.h"  
  24. #include "Singleton.h"  
  25. #include <sddl.h>  
  26. #pragma comment(lib, "Advapi32.lib")  
  27.   
  28. #define MAX_BUFFER_LENGTH       (1024)  
  29.   
  30. CSingleton::CSingleton(LPCTSTR lpszBoundary, LPCTSTR lpszNamespace) : m_hSingleton(NULL), m_hBoundary(NULL), m_hNamespace(NULL), m_bNamespaceOpen(FALSE)  
  31. {  
  32.     // Create the boundary descriptor  
  33.     if(lpszBoundary && _tcslen(lpszBoundary))  
  34.     {  
  35.         m_hBoundary = CreateBoundaryDescriptor(lpszBoundary, 0);  
  36.         if(NULL == m_hBoundary)  
  37.         {  
  38.             DUMPINFO(_T("CreateBoundaryDescriptor failed, error code : %d\r\n"), GetLastError());  
  39.             return ;  
  40.         }  
  41.     }  
  42.     if(NULL == m_hBoundary)  
  43.     {  
  44.         DUMPINFO(_T("m_hBoundarg is NULL\r\n"));  
  45.         return ;  
  46.     }  
  47.     // Create SID corresponding to the local Adminstrator group  
  48.     BYTE bySID[SECURITY_MAX_SID_SIZE] = {0};  
  49.     PSID pSID = bySID;  
  50.     DWORD dwSize = sizeof(bySID);  
  51.     if(!CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, pSID, &dwSize))  
  52.     {  
  53.         DUMPINFO(_T("CreateWellKnownSid failed, error code : %d\r\n"), GetLastError());  
  54.         return ;  
  55.     }  
  56.     // Associate the local admin SID to the boundary descriptor  
  57.     if(!AddSIDToBoundaryDescriptor(&m_hBoundary, pSID))  
  58.     {  
  59.         DUMPINFO(_T("AddSIDToBoundaryDescriptor failed, error code : %d\r\n"), GetLastError());  
  60.         return ;  
  61.     }  
  62.     // Create the namespace for local administrator only  
  63.     SECURITY_ATTRIBUTES sa = {0};  
  64.     sa.nLength = sizeof(sa);  
  65.     sa.bInheritHandle = FALSE;  
  66.     if(!ConvertStringSecurityDescriptorToSecurityDescriptor(_T("D:(A;;GA;;;BA)"), SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL))  
  67.     {  
  68.         DUMPINFO(_T("ConvertStringSecurityDescriptorToSecurityDescriptor failed, error code : %d\r\n"), GetLastError());  
  69.         return ;  
  70.     }  
  71.     if(lpszNamespace && _tcslen(lpszNamespace))  
  72.     {  
  73.         m_hNamespace = CreatePrivateNamespace(&sa, m_hBoundary, lpszNamespace);  
  74.         DWORD dwRet = GetLastError();  
  75.         LocalFree(sa.lpSecurityDescriptor);  
  76.         if(NULL == m_hNamespace)  
  77.         {  
  78.             if(ERROR_ALREADY_EXISTS != dwRet)  
  79.             {  
  80.                 DUMPINFO(_T("CreatePrivateNamespace failed, error code : %d\r\n"), dwRet);  
  81.                 return ;  
  82.             }  
  83.             else  
  84.             {  
  85.                 m_hNamespace = OpenPrivateNamespace(m_hBoundary, lpszNamespace);  
  86.                 if(NULL == m_hNamespace)  
  87.                 {  
  88.                     DUMPINFO(_T("OpenPrivateNamespace failed, error code : %d\r\n"), dwRet);  
  89.                     return ;  
  90.                 }  
  91.                 m_bNamespaceOpen = TRUE;  
  92.             }  
  93.         }  
  94.     }  
  95.       
  96.     if(NULL == lpszNamespace)  
  97.     {  
  98.         DUMPINFO(_T("lpszNamespace is NULL\r\n"));  
  99.         return ;  
  100.     }  
  101. }  
  102.   
  103.   
  104. CSingleton::~CSingleton(void)  
  105. {  
  106.     if(NULL != m_hSingleton)  
  107.     {  
  108.         CloseHandle(m_hSingleton);  
  109.         m_hSingleton = NULL;  
  110.     }  
  111.     if(NULL != m_hNamespace)  
  112.     {  
  113.         ClosePrivateNamespace(m_hNamespace, m_bNamespaceOpen ? 0 : PRIVATE_NAMESPACE_FLAG_DESTROY);  
  114.         m_hNamespace = NULL;  
  115.     }  
  116.     if(NULL != m_hBoundary)  
  117.     {  
  118.         DeleteBoundaryDescriptor(m_hBoundary);  
  119.         m_hBoundary = NULL;  
  120.     }  
  121. }  
  122.   
  123. void CSingleton::DUMPINFO(LPCTSTR lpszFormat, ...)  
  124. {  
  125.     TCHAR szText[MAX_BUFFER_LENGTH] = {0};  
  126.     va_list argList;  
  127.     va_start(argList, lpszFormat);  
  128.     _vstprintf_s(szText, _countof(szText), lpszFormat, argList);  
  129.     va_end(argList);  
  130.   
  131.     OutputDebugString(szText);  
  132. }  
  133.   
  134. BOOL CSingleton::IsInstanceExist(LPCTSTR lpszMutex)  
  135. {  
  136.     if(m_hBoundary && m_hNamespace && lpszMutex && _tcslen(lpszMutex))  
  137.     {  
  138.         m_hSingleton = CreateMutex(NULL, FALSE, lpszMutex);  
  139.         DWORD dwRet = GetLastError();  
  140.         if(NULL == m_hSingleton)  
  141.         {  
  142.             DUMPINFO(_T("CreateMutex failed, error code : %d\r\n"), dwRet);  
  143.             return FALSE;  
  144.         }  
  145.         if(ERROR_ALREADY_EXISTS == dwRet)  
  146.         {  
  147.             DUMPINFO(_T("The another instance of App is running ...\r\n"));  
  148.             return TRUE;  
  149.         }  
  150.         else  
  151.         {  
  152.             DUMPINFO(_T("The first instance of App ...\r\n"));  
  153.         }  
  154.     }  
  155.     return FALSE;  
  156. }  
[cpp]  view plain copy
  1. // 调用方法,主要修改App类的InitInstance函数  
  2. CSingleton* m_pSingleton = NULL; // 定义为App类的成员变量,并在App类的构造函数中初始化为NULL  
  3.   
  4. m_pSingleton = new CSingleton(_T("Chapter3Boundary"), _T("Chapter3Namesapce"));  
  5.   
  6. BOOL CXXXXXApp::InitInstance()  
  7. {  
  8.  // Check Instance of App  
  9.  if((NULL != m_pSingleton) && (m_pSingleton->IsInstanceExist(_T("Chapter3Mutex"))))  
  10.  {  
  11.    return FALSE;  
  12.  }  
  13.  ...  
  14. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值