`hm<o;Kwtwp8x1%`xk#^WVog_jkhP_Kl92tG9)2?DM`>}6mb~YWPJB~gY
zn7Z-V#Lgjh2l`LWA(nGSQvUw9j(#qNvx}VoDgRQZ_Zg8$I+djJvGalD-*lP)J2C&;
o{LAv=
+#include // for SID management
+#include
+#include
+
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+// Main dialog
+HWND g_hDlg;
+
+// Mutex, boundary and namespace used to detect previous running instance
+HANDLE g_hSingleton = NULL;
+HANDLE g_hBoundary = NULL;
+HANDLE g_hNamespace = NULL;
+
+// Keep track whether or not the namespace was created or open for clean-up
+BOOL g_bNamespaceOpened = FALSE;
+
+// Names of boundary and private namespace
+PCTSTR g_szBoundary = TEXT("3-Boundary");
+PCTSTR g_szNamespace = TEXT("3-Namespace");
+
+
+#define DETAILS_CTRL GetDlgItem(g_hDlg, IDC_EDIT_DETAILS)
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+// Adds a string to the "Details" edit control
+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:
+ // User has clicked on the Exit button
+ // or dismissed the dialog with ESCAPE
+ EndDialog(hwnd, id);
+ break;
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+void CheckInstances() {
+
+ // Create the boundary descriptor
+ g_hBoundary = CreateBoundaryDescriptor(g_szBoundary, 0);
+
+ // Create a SID corresponding to the Local Administrator group
+ BYTE localAdminSID[SECURITY_MAX_SID_SIZE];
+ PSID pLocalAdminSID = &localAdminSID;
+ DWORD cbSID = sizeof(localAdminSID);
+ if (!CreateWellKnownSid(
+ WinBuiltinAdministratorsSid, NULL, pLocalAdminSID, &cbSID)
+ ) {
+ AddText(TEXT("AddSIDToBoundaryDescriptor failed: %u\r\n"),
+ GetLastError());
+ return;
+ }
+
+ // Associate the Local Admin SID to the boundary descriptor
+ // --> only applications running under an administrator user
+ // will be able to access the kernel objects in the same namespace
+ if (!AddSIDToBoundaryDescriptor(&g_hBoundary, pLocalAdminSID)) {
+ AddText(TEXT("AddSIDToBoundaryDescriptor failed: %u\r\n"),
+ GetLastError());
+ return;
+ }
+
+ // Create the namespace for Local Administrators only
+ SECURITY_ATTRIBUTES sa;
+ sa.nLength = sizeof(sa);
+ sa.bInheritHandle = FALSE;
+ if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
+ TEXT("D:(A;;GA;;;BA)"),
+ SDDL_REVISION_1, &sa.lpSecurityDescriptor, NULL)) {
+ AddText(TEXT("Security Descriptor creation failed: %u\r\n"), GetLastError());
+ return;
+ }
+
+ g_hNamespace =
+ CreatePrivateNamespace(&sa, g_hBoundary, g_szNamespace);
+
+ // Don't forget to release memory for the security descriptor
+ LocalFree(sa.lpSecurityDescriptor);
+
+
+ // Check the private namespace creation result
+ DWORD dwLastError = GetLastError();
+ if (g_hNamespace == NULL) {
+ // Nothing to do if access is denied
+ // --> this code must run under a Local Administrator account
+ if (dwLastError == ERROR_ACCESS_DENIED) {
+ AddText(TEXT("Access denied when creating the namespace.\r\n"));
+ AddText(TEXT(" You must be running as Administrator.\r\n\r\n"));
+ return;
+ } else {
+ if (dwLastError == ERROR_ALREADY_EXISTS) {
+ // If another instance has already created the namespace,
+ // we need to open it instead.
+ AddText(TEXT("CreatePrivateNamespace failed: %u\r\n"), dwLastError);
+ g_hNamespace = OpenPrivateNamespace(g_hBoundary, g_szNamespace);
+ if (g_hNamespace == NULL) {
+ AddText(TEXT(" and OpenPrivateNamespace failed: %u\r\n"),
+ dwLastError);
+ return;
+ } else {
+ g_bNamespaceOpened = TRUE;
+ AddText(TEXT(" but OpenPrivateNamespace succeeded\r\n\r\n"));
+ }
+ } else {
+ AddText(TEXT("Unexpected error occured: %u\r\n\r\n"),
+ dwLastError);
+ return;
+ }
+ }
+ }
+
+ // Try to create the mutex object with a name
+ // based on the private namespace
+ TCHAR szMutexName[64];
+ StringCchPrintf(szMutexName, _countof(szMutexName), TEXT("%s\\%s"),
+ g_szNamespace, TEXT("Singleton"));
+
+ g_hSingleton = CreateMutex(NULL, FALSE, szMutexName);
+ if (GetLastError() == ERROR_ALREADY_EXISTS) {
+ // There is already an instance of this Singleton object
+ AddText(TEXT("Another instance of Singleton is running:\r\n"));
+ AddText(TEXT("--> Impossible to access application features.\r\n"));
+ } else {
+ // First time the Singleton object is created
+ AddText(TEXT("First instance of Singleton:\r\n"));
+ AddText(TEXT("--> Access application features now.\r\n"));
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) {
+
+ chSETDLGICONS(hwnd, IDI_SINGLETON);
+
+ // Keep track of the main dialog window handle
+ g_hDlg = hwnd;
+
+ // Check whether another instance is already running
+ CheckInstances();
+
+ return(TRUE);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+INT_PTR WINAPI Dlg_Proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
+
+ switch (uMsg) {
+ chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
+ chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
+ }
+
+ return(FALSE);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+int APIENTRY _tWinMain(HINSTANCE hInstance,
+ HINSTANCE hPrevInstance,
+ LPTSTR lpCmdLine,
+ int nCmdShow)
+{
+ UNREFERENCED_PARAMETER(hPrevInstance);
+ UNREFERENCED_PARAMETER(lpCmdLine);
+
+ // Show main window
+ DialogBox(hInstance, MAKEINTRESOURCE(IDD_SINGLETON), NULL, Dlg_Proc);
+
+ // Don't forget to clean up and release kernel resources
+ if (g_hSingleton != NULL) {
+ CloseHandle(g_hSingleton);
+ }
+
+ if (g_hNamespace != NULL) {
+ if (g_bNamespaceOpened) { // Open namespace
+ ClosePrivateNamespace(g_hNamespace, 0);
+ } else { // Created namespace
+ ClosePrivateNamespace(g_hNamespace, PRIVATE_NAMESPACE_FLAG_DESTROY);
+ }
+ }
+
+ if (g_hBoundary != NULL) {
+ DeleteBoundaryDescriptor(g_hBoundary);
+ }
+
+ return(0);
+}
+
+
+//////////////////////////////// End of File //////////////////////////////////
diff --git a/Singleton.ico b/Singleton.ico
new file mode 100644
index 0000000000000000000000000000000000000000..b96aa0eff2d1499e38c455e99d330aa30ba939c1
GIT binary patch
literal 2238
zcmchXc~DbF9LK*-FYMv8wrExUP}|`eoYq=fbi8o9I7B5Xr;1|nASy>hk$^!Xf|3$J
zE}>lIj!{B55(7fyD2SR6KtN-mTty+&1DqGrKKk+kiuF%t`rY^2_jW(~{q4@~&I6`U
zW$s*1GD4maFoXIsr(!M@tEiYl_0xLS@S^J}RILw6ix!w+(ZX+FwQw<(F0qFF^6#+5
zW(9Wc{}x*hEyL2~tFUs_YFOL3VzJ{sEZ_1IOdW$^?Ggx^)oZY3-3F}N-~b1Edu(yE
zg~P_puy=9>gTcTyXIFT*yTNmZI|4%Nu-kJdeD=7(>)lfZgh${J
z*n5Y-%_j(6UVE_L+Y1K|c!SAg!tV$Z{=SD1;O~#*t?z?lGXvr4=RmO22m;pU5QP|{
zFw_J*PfMH%@dM{18*W@{_@>$5Xx@5+3T;u!w?t*~0!S|{gDiaoeva}$3fBdf_#P0(
ztVQ`RtB}Fl0j_|7h9mG24CcY(%kKkX1gPt6NuAcp$Lm$Bl6-=
zaFYFTF7^Zx5(1E$;EmkWV>mC|jl@bO;sgN@=LaKCasuT=CsCcZ7S+N{kYC=6TG2K%
zinpU)?2cO!4>VW0p-$|LhLXLwDcOq~MF-F*I|yY36UnvSNUaJ$syqm3^}$GQJc7(d
ze`MV}j=Tmo1gc}mSF%yu9E8IAY;;ubfeAotM!Jo^+@wfmwki+I^~MC~ERh*Kid#)H!IpU5VD)GPHG;;hs{8rVbf~Ix=v7_zK#3BpB$D
zqHCZM;ROxg6)M0lx{Y|rU1U~v;Q9?Ya;nuxt?7fHu@ADQeuz|0AeYN=>sCFQ8f&0Z
z)T6Dr9<8lPblg=!-L6ERx&}S>ZlX)wjP9;B4E3}?^Qaj;-S;rq-+{qLop?HQAEQs&
zL5}>6n)ZiK{_z)TddHybevILv9%u#!@I=#(C&Q2M{BbYH(H@Ku{do3RgBQ;SF{T~D
zvu961JbwyOH-fRTF~bkwt!iWA*|T2LY!eex6H`+&vw5@hiA_hfqoZ1__U}o0q1BFz
zXobdlZmH9eZ%Rk%$olCtC*BY>Tt~SK+?>YxHc1TBrp7TCE&6nXj;y2oX5)Mym$Bs#
zi{)c${o@b+kW?pG_nAJwxkf)=B+c2*IkSQ#7BSZ@F`_Mmfp4ODbwJ<|*DKc!i_66>
zOFx**Q)qt2m*tn-)95RyDfhFTLvvF7sXl)S-T0F!5@)FE{KVxFmisUnRZ5}
z8=UZWPJOjLs!tIe9l@FZklIPZ0J8JL$^4;05yLY%`V)$Y2Tztt5
z`LpT$(GUae!>vvHa~~VdgW7(wi#~q?Cp3?ShuiNe!lr&P?kCBM@AEZoT8{pFCi~PF+y4+ps=@_E8m83>85Vdfuz&d5Ldc{_&m`6N=xnG`Fy@
nqGUl8>QgUs3sXw-EUonW^LC>C0ko=scm9vyQ1j?xV%~oNVx&P{
literal 0
HcmV?d00001
diff --git a/Singleton.rc b/Singleton.rc
new file mode 100644
index 0000000..8b4db2d
--- /dev/null
+++ b/Singleton.rc
@@ -0,0 +1,111 @@
+// Microsoft Visual C++ generated resource script.
+//
+#include "resource.h"
+
+#define APSTUDIO_READONLY_SYMBOLS
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 2 resource.
+//
+#define APSTUDIO_HIDDEN_SYMBOLS
+#include "windows.h"
+#undef APSTUDIO_HIDDEN_SYMBOLS
+
+/////////////////////////////////////////////////////////////////////////////
+#undef APSTUDIO_READONLY_SYMBOLS
+
+/////////////////////////////////////////////////////////////////////////////
+// English (U.S.) resources
+
+#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
+#ifdef _WIN32
+LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
+#pragma code_page(1252)
+#endif //_WIN32
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Icon
+//
+
+// Icon with lowest ID value placed first to ensure application icon
+// remains consistent on all systems.
+IDI_SINGLETON ICON "Singleton.ico"
+
+#ifdef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// TEXTINCLUDE
+//
+
+1 TEXTINCLUDE
+BEGIN
+ "resource.h\0"
+END
+
+2 TEXTINCLUDE
+BEGIN
+ "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
+ "#include ""windows.h""\r\n"
+ "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
+ "\0"
+END
+
+3 TEXTINCLUDE
+BEGIN
+ "\r\n"
+ "\0"
+END
+
+#endif // APSTUDIO_INVOKED
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Dialog
+//
+
+IDD_SINGLETON DIALOGEX 0, 0, 195, 95
+STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_MINIMIZEBOX | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
+EXSTYLE WS_EX_APPWINDOW
+CAPTION "Singleton"
+FONT 8, "MS Shell Dlg", 400, 0, 0x1
+BEGIN
+ DEFPUSHBUTTON "E&xit",IDOK,140,76,50,14
+ EDITTEXT IDC_EDIT_DETAILS,4,5,186,67,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// DESIGNINFO
+//
+
+#ifdef APSTUDIO_INVOKED
+GUIDELINES DESIGNINFO
+BEGIN
+ IDD_SINGLETON, DIALOG
+ BEGIN
+ LEFTMARGIN, 7
+ RIGHTMARGIN, 188
+ TOPMARGIN, 7
+ BOTTOMMARGIN, 88
+ END
+END
+#endif // APSTUDIO_INVOKED
+
+#endif // English (U.S.) resources
+/////////////////////////////////////////////////////////////////////////////
+
+
+
+#ifndef APSTUDIO_INVOKED
+/////////////////////////////////////////////////////////////////////////////
+//
+// Generated from the TEXTINCLUDE 3 resource.
+//
+
+
+/////////////////////////////////////////////////////////////////////////////
+#endif // not APSTUDIO_INVOKED
+