修改注册表权限。。

4 篇文章 0 订阅
4 篇文章 0 订阅
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);
	} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值