UnauthorizedAccessException en ComRegisterFunction al acceder registro en Win 7 64
-
19-09-2019 - |
Pregunta
Tengo un [ComRegisterFunction] que estoy usando para registrar una extensión del Internet Explorer BHO. Durante el registro en Windows de 64 bits 7 máquinas, un UnauthorizedAccessException se lanza en la llamada a subKey.SetValue ( "NoExplorer", 1).
El registro parece tener de BHO ubicada @ \ HKLM \ SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Browser Helper Objects, sin embargo, los consigo misma excepción al intentar registrar allí. Cualquier ayuda sería muy apreciada.
[ComRegisterFunction]
public static void RegisterBho(Type type) {
string BhoKeyName= "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BhoKeyName, true) ??
Registry.LocalMachine.CreateSubKey(BhoKeyName);
if(registryKey == null) throw new ApplicationException("Unable to register Bho");
registryKey.Flush();
string guid = type.GUID.ToString("B");
RegistryKey subKey = registryKey.OpenSubKey(guid) ?? registryKey.CreateSubKey(guid);
if (subKey == null) throw new ApplicationException("Unable to register Bho");
subKey.SetValue("NoExplorer", 1);
registryKey.Close();
subKey.Close();
}
Solución 2
descubierto. He tenido que añadir lo siguiente para que funcione. No estoy seguro de por qué funcionaba en otras versiones del sistema operativo
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.FullControl,
InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow));
RegistryKey subKey = registryKey.OpenSubKey(guid) ?? registryKey.CreateSubKey(guid, RegistryKeyPermissionCheck.Default, rs);
Otros consejos
Tiene que ejecutar con privilegios administrativos.