If you need to Add "Everyone" to a registry key's Permissions you can use the following chunk of code (this is a "very old code". It was written back in the ages where we had to support Win95/98 clients together with Win2k/WinXP clients. You may need to adjust the code to statically link to the functions that are used by
GetProcAddress
below):
typedef BOOL(WINAPI* AllocateAndInitializeSidType)(
PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, BYTE nSubAuthorityCount, DWORD dwSubAuthority0, DWORD dwSubAuthority1, DWORD dwSubAuthority2, DWORD dwSubAuthority3, DWORD dwSubAuthority4, DWORD dwSubAuthority5, DWORD dwSubAuthority6, DWORD dwSubAuthority7, PSID *pSid );
typedef DWORD(WINAPI* SETENTRIESINACL)(
ULONG cCountOfExplicitEntries, PEXPLICIT_ACCESS pListOfExplicitEntries, PACL OldAcl, PACL *NewAcl );
typedef BOOL(WINAPI* InitializeSecurityDescriptorType)(
PSECURITY_DESCRIPTOR pSecurityDescriptor, DWORD dwRevision );
typedef LONG(WINAPI* REGSETKEYSECURITY)(
HKEY hKey, SECURITY_INFORMATION SecurityInformation, PSECURITY_DESCRIPTOR pSecurityDescriptor );
typedef BOOL(WINAPI* SetSecurityDescriptorDaclType)(
PSECURITY_DESCRIPTOR pSecurityDescriptor, BOOL bDaclPresent, PACL pDacl, BOOL bDaclDefaulted );
typedef PVOID(WINAPI* FreeSidType)(
PSID pSid );
BOOL SetRegFullAccessPermission(HKEY hKey)
{
DWORD dwRes = ERROR_SUCCESS;
BOOL bRet = TRUE;
PSID pEveryoneSID = NULL;
PACL pACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea[1];
SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
TCHAR szErr[1024] = {0};
int i = 0;
TCHAR szSubKey[1024] = {0};
HMODULE hDll = NULL;
AllocateAndInitializeSidType AllocateAndInitializeSidFunc;
SETENTRIESINACL SetEntriesInAclFunc;
InitializeSecurityDescriptorType InitializeSecurityDescriptorFunc;
REGSETKEYSECURITY RegSetKeySecurityFunc;
SetSecurityDescriptorDaclType SetSecurityDescriptorDaclFunc;
FreeSidType FreeSidFunc;
OSVERSIONINFO versionInfo = {0};
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
GetVersionEx(&versionInfo);
if(versionInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
{
bRet = FALSE;
goto SAFE_EXIT;
}
hDll = LoadLibrary(_T("advapi32.dll"));
if(hDll == NULL)
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(AllocateAndInitializeSidFunc =(AllocateAndInitializeSidType)GetProcAddress(hDll, _T("AllocateAndInitializeSid"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(SetEntriesInAclFunc =(SETENTRIESINACL)GetProcAddress(hDll, _T("SetEntriesInAclA"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(InitializeSecurityDescriptorFunc =(InitializeSecurityDescriptorType)GetProcAddress(hDll, _T("InitializeSecurityDescriptor"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(SetSecurityDescriptorDaclFunc =(SetSecurityDescriptorDaclType)GetProcAddress(hDll, _T("SetSecurityDescriptorDacl"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(RegSetKeySecurityFunc =(REGSETKEYSECURITY)GetProcAddress(hDll, _T("RegSetKeySecurity"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(FreeSidFunc =(FreeSidType)GetProcAddress(hDll, _T("FreeSid"))))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(*AllocateAndInitializeSidFunc)(&SIDAuthWorld, 1, SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0, &pEveryoneSID))
{
bRet = FALSE;
goto SAFE_EXIT;
}
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = KEY_ALL_ACCESS;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance= NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName =(LPTSTR) pEveryoneSID;
dwRes =(*SetEntriesInAclFunc)(1, ea, NULL, &pACL);
if(ERROR_SUCCESS != dwRes)
{
bRet = FALSE;
goto SAFE_EXIT;
}
pSD =(PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
if(pSD == NULL)
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(*InitializeSecurityDescriptorFunc)(pSD, SECURITY_DESCRIPTOR_REVISION))
{
bRet = FALSE;
goto SAFE_EXIT;
}
if(!(*SetSecurityDescriptorDaclFunc)(pSD, TRUE, pACL, FALSE))
{
bRet = FALSE;
goto SAFE_EXIT;
}
dwRes =(*RegSetKeySecurity)(hKey, DACL_SECURITY_INFORMATION, pSD);
if(dwRes == ERROR_SUCCESS)
bRet = TRUE;
else
{
ASSERT(FALSE);
bRet = FALSE;
}
SAFE_EXIT:
if(pEveryoneSID)
(*FreeSidFunc)(pEveryoneSID);
if(pACL)
LocalFree(pACL);
if(pSD)
LocalFree(pSD);
if(hDll)
FreeLibrary(hDll);
return bRet;
}