程序单实例运行 2010-07-24
/***********************************************************************
Module : Singleton.cpp
Notice: Copyright ( C )
***********************************************************************/
#include"stdafx.h"
#include "resource.h"
#include<windowsx.h>
#include<Sddl.h>
#include<tchar.h>
#include<strsafe.h>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HWND g_hDlg;
HANDLE g_hSingleton = NULL;
HANDLE g_hBoundary = NULL;
HANDLE g_hNamespace = NULL;
BOOL g_bNamespaceOpened = FALSE;
PCTSTR g_szBoundary = TEXT("3-Boundary");
PCTSTR g_szNamespace = TEXT("3-Namespace");
#define DETAILS_CTRL GetDlgItem(g_hDlg,IDC_EDIT_DETAILS)
void AddText(PCTSTR pszFormat,...){
va_list argList;
va_start(argList,pszFormat);
TCHAR sz[20 * 1024];
Edit_GetText(DETAILS_CTRL,sz,_countof(sz));
_vstprintf_s(
_tcschr(sz,TEXT('/0')),_countof(sz)-_tcslen(sz),
pszFormat,argList);
Edit_SetText(DETAILS_CTRL,sz);
va_end(argList);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Dlg_OnCommand(HWND hwnd,int id,HWND hwndCtl,UINT codeNotify){
switch(id){
case IDOK:
case IDCANCEL:
EndDialog(hwnd,id);
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CheckInstance()
{
g_hBoundary = CreateBoundaryDescriptor(g_szBoundary,0);
BYTE localAdminSID[SECURITY_MAX_SID_SIZE];
PSID pLoacalAdminSID =&localAdminSID;
if(!CreateWellknownSid(
WinBuildinAdministratorsSid,NULL,pLocalAdminSID,&cbSID)
){
AddText(TEXT("AddSIDToBoundaryDescriptor failed:%u/r/n"),GetLastError());
return;
}
if(!AddSIDToBoundaryDescriptor(&g_hBoundary,pLocalAdminSID))
{
AddText(TEXT("AddSIDToBoundaryDescriptor failed:%u/r/n"),GetLastError());
return;
}
}