Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RegSetKeySecurity避免注册表重定向_C#_.net 2.0_Registry_Advapi32 - Fatal编程技术网

C# 使用RegSetKeySecurity避免注册表重定向

C# 使用RegSetKeySecurity避免注册表重定向,c#,.net-2.0,registry,advapi32,C#,.net 2.0,Registry,Advapi32,为了避免注册表重定向到Wow64项,如何翻译以下使用Microsoft.Win32API的代码 public void SetKeyAccessControl( RegistryKey rootKey, string subKeyName, string identity, RegistryRights rights, InheritanceFlags inheritanceFlags, PropagationFlags

为了避免注册表重定向到Wow64项,如何翻译以下使用
Microsoft.Win32
API的代码

public void SetKeyAccessControl(
            RegistryKey rootKey, string subKeyName, string identity, 
            RegistryRights rights, InheritanceFlags inheritanceFlags,
            PropagationFlags propagationFlags, AccessControlType accessType)
{
   using (RegistryKey regKey = rootKey.OpenSubKey(subKeyName, true))
   {
       RegistrySecurity acl = new RegistrySecurity();
       RegistryAccessRule rule = new RegistryAccessRule(identity, rights, inheritanceFlags, propagationFlags, accessType);
       acl.AddAccessRule(rule);

       regKey.SetAccessControl(acl);
   }            
}
使用advapi32 RegSetKeySecurity API

[DllImport(@"advapi32.dll", EntryPoint = "RegSetKeySecurity", SetLastError = true)]
internal static extern int RegSetKeySecurity(IntPtr handle, uint securityInformation, IntPtr pSecurityDescriptor);

要避免注册表重定向,可以执行以下操作

SafeRegistryHandle handle = rootKey.Handle;

RegistryKey rootKey32 = RegistryKey.FromHandle(handle, RegistryView.Registry32);

RegistryKey rootKey64 = RegistryKey.FromHandle(handle, RegistryView.Registry64);
然后,您可以使用rootKey32或rootKey64打开一个子项,您将获得所请求视图的子项

至少在我尝试过的几个测试用例中它是有效的。根据FromHandle的数据

此方法的视图参数用于后续操作, 例如打开子键


需要涉及另一个本机方法并提供SDDL,以下代码在右侧注册表项上设置ACL:


[DllImport("Advapi32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(string stringSecurityDescriptor, int stringSDRevision, out IntPtr ppSecurityDescriptor, ref int securityDescriptorSize);

string sddl = "...";
IntPtr secDescriptor = IntPtr.Zero;
int size = 0;
ConvertStringSecurityDescriptorToSecurityDescriptor
   (
      sddl,
      1,                              // revision 1
      out secDescriptor,
      ref size
   );

// get handle with RegOpenKeyEx

RegSetKeySecurity
(
     handle,
     0x00000004,                      // DACL_SECURITY_INFORMATION
     secDescriptor
);

如果您正在使用2,那么在C++中使用公共语言运行库支持来实现您的SETKEYAccess控件功能将更容易。这样,您就可以使用本机C++调用ReGubKEYX,提供在<代码> SAMVAIDS参数中。