procedure TForm1.FormCreate(Sender: TObject);
var
Reg : TRegistry;
TempKey: HKEY;
SD : SECURITY_DESCRIPTOR;
Ret : integer;
begin
// 我想把 “SOFTWARE\ACD Systems”这个键设成:administrator 也只能有读入的权限 ,不能写
if RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar('SOFTWARE\ACD Systems'), 0,
WRITE_OWNER, TempKey) = ERROR_SUCCESS then
begin
Ret:=RegSetKeySecurity(TempKey,
OWNER_SECURITY_INFORMATION, // 对要保存的信息进行描述的标志
@SD); // 这个结构包含了注册表项新的安全特性设置
end;
end;
关于“OWNER_SECURITY_INFORMATION” 和“SD” 完全不明。。。
网上大部分都是 VC++ 写的,看不明。。。。
void WINAPI SetRegPermission(char *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,
TEXT(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_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_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);
}
请大家帮一下!!!!
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!