您的位置:首页 > 大数据 > 人工智能

修改注册表权限。。

2012-07-16 14:41 369 查看
void WINAPI SetRegPermission(wchar_t *KeyStr)
{
// TODO: Add your control notification handler code here
HKEY hKey = 0;
SID_IDENTIFIER_AUTHORITY sia = SECURITY_NT_AUTHORITY;
PSID pInteractiveSid = NULL;
PSID pAdministratorsSid = NULL;
SECURITY_DESCRIPTOR sd;
PACL pDacl = NULL;
DWORD dwAclSize;
LONG lRetCode;
BOOL bRet;
//char KeyStr[200];

//strcpy(KeyStr,"SOFTWARE\\LEGEND\\test");

lRetCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
KeyStr,
0,
WRITE_DAC,
&hKey);
//open key
/*lRetCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\LEGEND\\test"),
0,
WRITE_DAC,
&hKey);*/
//
// prepare a Sid representing any Interactively logged-on user
//
bRet = AllocateAndInitializeSid(
&sia,
1,
SECURITY_INTERACTIVE_RID,
0, 0, 0, 0, 0, 0, 0,
&pInteractiveSid
);

//
// preprate a Sid representing the well-known admin group
//
bRet = AllocateAndInitializeSid(
&sia,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdministratorsSid
);

//
// compute size of new acl
//
dwAclSize = sizeof(ACL) +
2 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) +
GetLengthSid(pInteractiveSid) +
GetLengthSid(pAdministratorsSid) ;

//
// allocate storage for Acl
//
pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);

bRet = InitializeAcl(pDacl, dwAclSize, ACL_REVISION);

//
// grant the Interactive Sid KEY_READ access to the perf key
//
bRet = AddAccessAllowedAce(
pDacl,
ACL_REVISION,
KEY_READ/*KEY_ALL_ACCESS*/,
pInteractiveSid
);
/*bRet = AddAccessAllowedAce(
pDacl,
ACL_REVISION,
samDesired,
pInteractiveSid
);*/

//
// grant the Administrators Sid KEY_ALL_ACCESS access to the perf key
//
bRet = AddAccessAllowedAce(
pDacl,
ACL_REVISION,
KEY_READ/*KEY_ALL_ACCESS*/,
pAdministratorsSid
);

bRet = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);

bRet = SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE);

//
// apply the security descriptor to the registry key
//
lRetCode = RegSetKeySecurity(
hKey,
(SECURITY_INFORMATION)DACL_SECURITY_INFORMATION,
&sd
);

//clean up
RegCloseKey(hKey);
RegCloseKey(HKEY_LOCAL_MACHINE);

//
// free allocated resources
//
if(pDacl != NULL)
HeapFree(GetProcessHeap(), 0, pDacl);

if(pInteractiveSid != NULL)
FreeSid(pInteractiveSid);

if(pAdministratorsSid != NULL)
FreeSid(pAdministratorsSid);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息