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.
Then, add the source file and give it a suitable name.
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, LCould not allocate memory for TheSID.\n);
exit(1);
}
else
wprintf(LMemory allocated for TheSID!\n);
// Create a SID for the Everyone group on the local computer.
// https://msdn.microsoft.com/en-us/library/aa379649%28VS.85%29.aspx
// https://support.microsoft.com/kb/243330
// https://msdn.microsoft.com/en-us/library/aa379650%28VS.85%29.aspx
if(!CreateWellKnownSid(WinWorldSid, NULL, TheSID, &SidSize))
{
fwprintf(stderr, LCreateWellKnownSid() failed, error %u, GetLastError());
}
else
{
wprintf(LWinWorldSid, 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, LError during ConvertSidToStringSid().\n);
exit(1);
}
else
wprintf(LThe 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(LClosing the stderr stream!\n);
else
wprintf(LFailed to close stderr stream, error %u\n, GetLastError());
if(LocalFree(p) == NULL)
wprintf(LFreeing the p!\n);
else
wprintf(LFailed to free p, error %u\n, GetLastError());
if(LocalFree(TheSID) == NULL)
wprintf(LFreeing the TheSID!\n);
else
wprintf(LFailed to free TheSID, error %u\n, GetLastError());
return 0;
}
}
Build and run the project. The following screenshot is a sample output.