Windows Access Control List (ACL) Example 34

 

 

 

 

 

Creating A Well Known SID Program Example

 

The following example shows how to create a SID for the Everyone group.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

Creating A Well Known SID Program Example: Creating new C++ empty console mode application project in Visual C++

 

Then, add the source file and give it a suitable name.

 

Creating A Well Known SID Program Example: Adding the C++ source  file to the existing C++ project

 

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

#include <Sddl.h>

 

int wmain(int argc, WCHAR *argv[])

{

      DWORD SidSize;

      PSID TheSID;

      LPTSTR p;

     

      SidSize = SECURITY_MAX_SID_SIZE;

 

      // Allocate enough memory for the largest possible SID.

      if(!(TheSID = LocalAlloc(LMEM_FIXED, SidSize)))

      {   

            fwprintf(stderr, L"Could not allocate memory for TheSID.\n");

            exit(1);

      }

      else

            wprintf(L"Memory allocated for TheSID!\n");

     

      // Create a SID for the Everyone group on the local computer.

      // http://msdn.microsoft.com/en-us/library/aa379649%28VS.85%29.aspx

      // http://support.microsoft.com/kb/243330

      // http://msdn.microsoft.com/en-us/library/aa379650%28VS.85%29.aspx

      if(!CreateWellKnownSid(WinWorldSid, NULL, TheSID, &SidSize))

      {

            fwprintf(stderr, L"CreateWellKnownSid() failed, error %u", GetLastError());

      }

      else

      {

            wprintf(L"WinWorldSid,  a well known SID for Everyone group was successfully created!\n");

 

            ////////////////////////////////////////

            // TODO: Use the binary SID as needed.

            ///////////////////////////////////////

 

            // Get the string version of the SID (S-1-1-0).

            if(!(ConvertSidToStringSid(TheSID, &p)))

            {

                  fwprintf(stderr, L"Error during ConvertSidToStringSid().\n");

                  exit(1);

            }

            else

                  wprintf(L"The WinWorldSid (Everyone group) string is: %s\n", p);

           

            //////////////////////////

            // TODO: Use the string SID as needed.

            //////////////////////////////////////

           

            // When done, free the memory used.

            if(fclose(stderr) == 0)

                  wprintf(L"Closing the stderr stream!\n");

            else

                  wprintf(L"Failed to close stderr stream, error %u\n", GetLastError());

 

            if(LocalFree(p) == NULL)

                  wprintf(L"Freeing the p!\n");

            else

                  wprintf(L"Failed to free p, error %u\n", GetLastError());

 

            if(LocalFree(TheSID) == NULL)

                  wprintf(L"Freeing the TheSID!\n");

            else

                  wprintf(L"Failed to free TheSID, error %u\n", GetLastError());

           

            return 0;

      }

}

 

Build and run the project. The following screenshot is a sample output.

 

Creating A Well Known SID Program Example: A sample console output showing the SID string for the Windows well known SID

 

 

 

 

< Windows ACL Example 33 | Windows Access Control List (ACL) Main | Win32 Programming | Windows ACL Example 35 >