//Sets the dialog box iconsInlinevoidCmfc_core_1dlg::chsetdlgicons (HWND hwnd,intidi) {:: SendMessage (hwnd, Wm_seticon, Icon_big, (LPARAM) LoadIcon (hinstance):: Getwindowlongptr (hwnd, GW lp_hinstance), Makeintresource (IDI)); :: SendMessage (hwnd, Wm_seticon, Icon_small, (LPARAM) LoadIcon ((hinstance):: Getwindowlongptr (hwnd, gwlp_hinstance), Makeintresource (IDI)));}voidCmfc_core_1dlg::ondestroy () {Cdialogex::ondestroy (); //TODO: Add Message Handler code here//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 NamespaceCloseprivatenamespace (G_hnamespace,0); } Else{//Created Namespacecloseprivatenamespace (G_hnamespace, Private_namespace_flag_destroy); } } if(G_hboundary! =NULL) {Deleteboundarydescriptor (g_hboundary); }}//Main DialogHWND G_hdlg;//Mutex, boundary and namespace used to detect previous running instanceHANDLE G_hsingleton =NULL; HANDLE g_hboundary=NULL; HANDLE G_hnamespace=NULL;//Keep Track Whether or not the namespace is created or open for clean-upBOOL g_bnamespaceopened =FALSE;//Names of boundary and private namespacePctstr g_szboundary = TEXT ("3-boundary"); Pctstr G_sznamespace= TEXT ("3-namespace");voidcmfc_core_1dlg::checkinstances () {//Create the boundary descriptorG_hboundary =:: Createboundarydescriptor (G_szboundary,0); //Create a SID corresponding to the Local Administrator groupBYTE Localadminsid[security_max_sid_size]; PSID Plocaladminsid= &Localadminsid; DWORD Cbsid=sizeof(LOCALADMINSID); if(!:: Createwellknownsid (Winbuiltinadministratorssid, NULL, Plocaladminsid,&Cbsid)) { //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Addsidtoboundarydescriptor failed:%u\r\n"), GetLastError ()); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Addsidtoboundarydescriptor failed:%u\r\n"), GetLastError ()); return; } //Associate the Local Admin SID to the boundary descriptor//-- applications running under an administrator user//'ll be able to access the kernel objects in the same namespace if(!::addsidtoboundarydescriptor (&g_hboundary, Plocaladminsid)) { //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Addsidtoboundarydescriptor failed:%u\r\n"), GetLastError ()); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Addsidtoboundarydescriptor failed:%u\r\n"), GetLastError ()); return; } //Create the namespace for Local Administrators onlysecurity_attributes sa; Sa.nlength=sizeof(SA); Sa.binherithandle=FALSE; if(!:: ConvertStringSecurityDescriptorToSecurityDescriptor (TEXT ("D: (A;; GA;;; BA)"), sddl_revision_1,&Sa.lpsecuritydescriptor, NULL)) { //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Security Descriptor creation failed:%u\r\n"), GetLastError ()); Setdlgitemtext (idc_edit1,edit_tring); TRACE (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 resultDWORD Dwlasterror =GetLastError (); if(G_hnamespace = =NULL) { //Nothing to does if access is denied//--This code must run under a Local Administrator account if(Dwlasterror = =error_access_denied) { //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Access denied when creating the namespace.\r\n you must be running as administrator.\r\n\r\n")); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Access denied when creating the namespace.\r\n")); TRACE (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. //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Createprivatenamespace failed:%u\r\n"), dwlasterror); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Createprivatenamespace failed:%u\r\n"), dwlasterror); G_hnamespace=openprivatenamespace (G_hboundary, g_sznamespace); if(G_hnamespace = =NULL) {edit_tring. Format (TEXT ("And Openprivatenamespace failed:%u\r\n")); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("And Openprivatenamespace failed:%u\r\n"), dwlasterror); return; } Else{g_bnamespaceopened=TRUE; Edit_tring. Format (TEXT ("But openprivatenamespace succeeded\r\n\r\n")); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("But openprivatenamespace succeeded\r\n\r\n")); } } Else { //Using EditCString edit_tring; Edit_tring. Format (TEXT ("Unexpected error occured:%u\r\n\r\n")); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Unexpected error occured:%u\r\n\r\n"), dwlasterror); return; } } } //Try to create the mutex object with a name//based on the private namespaceTCHAR szmutexname[ -]; 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 a instance of this Singleton object//Using EditCString edit_tring; Edit_tring. Format (TEXT ("Another instance of Singleton is running:\r\n--> impossible to access application features.\r\n")); Setdlgitemtext (idc_edit1,edit_tring); TRACE (TEXT ("Another instance of Singleton is running:\r\n")); TRACE (TEXT ("-- impossible to access application features.\r\n")); } Else { //First time the Singleton object is created//Using EditCString edit_tring; Edit_tring. Format (TEXT ("First instance of Singleton:\r\n--> Access application features now.\r\n")); GetDlgItem (IDC_EDIT1)-SetWindowText (edit_tring); TRACE (TEXT ("First instance of singleton:\r\n")); TRACE (TEXT ("-- Access application features now.\r\n")); }}
Administrators are bound to namespaces, only one case can be run, MFC changes icons (Windows core programming)