public static string GetAppropriateBitnessModuleModulePath(bool is64bit, RuntimeVersions runtimeVersion) { if (RuntimeVersions.V40 == runtimeVersion) { // Retrieve the regkey with Read permission. Note that on Windows 8 and later, only Trusted installer has write access to this key. using (RegistryHandle regKey = RegistryHandle.GetCorrectBitnessHKLMSubkey(is64bit, ServiceModelInstallStrings.WinFXRegistryKey, false)) { return(regKey.GetStringValue(ServiceModelInstallStrings.RuntimeInstallPathName).TrimEnd('\0') + "\\" + fileName); } } else { // Try to find the 3.0 version RegistryHandle regkey = null; try { if (SafeNativeMethods.ERROR_SUCCESS == RegistryHandle.TryGetCorrectBitnessHKLMSubkey(is64bit, Wcf30RegistryKey, out regkey)) { return(regkey.GetStringValue(Runtime30InstallPathName).TrimEnd('\0') + "\\" + fileName); } else { //We don't want to automatically roll forward to 4.0, so we throw an exception if we can't find the 3.0 reg key throw Tool.CreateException(SR.GetString(SR.FailedToGetRegistryKey, Wcf30RegistryKey, "3.0"), null); } } finally { if (regkey != null) { regkey.Dispose(); } } } }
static void RemoveClsidFromRegistry(bool is64bit, string clsid) { RegistryHandle regKey = RegistryHandle.GetBitnessHKCR(is64bit); string baseKey = "Clsid\\" + clsid; regKey.DeleteKey(baseKey + "\\InprocServer32"); regKey.DeleteKey(baseKey + "\\ProgID"); regKey.DeleteKey(baseKey); }
static RegistryHandle Get32bitHKCR() { RegistryHandle regHandle = null; int status = SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, @"Software\Classes", 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_32KEY, out regHandle); if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid) { throw Tool.CreateException(SR.GetString(SR.FailedToOpenRegistryKey, ""), null); } return(regHandle); }
public RegistryHandle CreateSubKey(string subKey) { RegistryHandle regHandle = null; int disposition; int status = SafeNativeMethods.RegCreateKeyEx(this, subKey, 0, null, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, IntPtr.Zero, out regHandle, out disposition); if (status != SafeNativeMethods.ERROR_SUCCESS || regHandle == null || regHandle.IsInvalid) { Tool.CreateException(SR.GetString(SR.FailedToCreateSubKey, subKey), null); } return(regHandle); }
static RegistryHandle Get32bitHKLMSubkey(string key, bool isWriteRequired) { RegistryHandle regHandle = null; int samDesired = SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WOW64_32KEY; if (isWriteRequired) { samDesired = samDesired | SafeNativeMethods.KEY_WRITE; } int status = SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, samDesired, out regHandle); if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid) { throw Tool.CreateException(SR.GetString(SR.FailedToOpenRegistryKey, ""), null); } return(regHandle); }
public static void CreateRegistryKey(bool is64bit, Guid clsid, string module) { using (RegistryHandle regKey = RegistryHandle.GetBitnessHKCR(is64bit)) { using (RegistryHandle clsidKey = regKey.CreateSubKey(@"clsid\" + clsid.ToString("B"))) { clsidKey.SetValue("", ListenerWSUName); using (RegistryHandle inprocServer32Key = clsidKey.CreateSubKey("InprocServer32")) { inprocServer32Key.SetValue("", module); inprocServer32Key.SetValue("ThreadingModel", "Both"); } using (RegistryHandle progID = clsidKey.CreateSubKey("ProgID")) { progID.SetValue("", ListenerWSUName); } } } }
public static int TryGetCorrectBitnessHKLMSubkey(bool is64bit, string key, out RegistryHandle regHandle) { if (is64bit && IntPtr.Size == 8) // No worries we are trying to open up a 64 bit hive just return { return(SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle)); } else if (is64bit && IntPtr.Size == 4) // we are running under wow get the 64 bit hive { return(SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_64KEY, out regHandle)); } else if (!is64bit && IntPtr.Size == 8) // we are running in 64 bit but need to open a 32 bit hive { return(SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_32KEY, out regHandle)); } else if (!is64bit && IntPtr.Size == 4) { return(SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle)); } throw Tool.CreateException(SR.GetString(SR.UnableToDetermineHiveBitness), null); }
internal static extern int RegOpenKeyEx(RegistryHandle hKey, String lpSubKey, int ulOptions, int samDesired, out RegistryHandle hkResult);
public static int TryGetCorrectBitnessHKLMSubkey(bool is64bit, string key, out RegistryHandle regHandle) { if (is64bit && IntPtr.Size == 8) // No worries we are trying to open up a 64 bit hive just return return SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle); else if (is64bit && IntPtr.Size == 4) // we are running under wow get the 64 bit hive return SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_64KEY, out regHandle); else if (!is64bit && IntPtr.Size == 8) // we are running in 64 bit but need to open a 32 bit hive return SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_32KEY, out regHandle); else if (!is64bit && IntPtr.Size == 4) return SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle); throw Tool.CreateException(SR.GetString(SR.UnableToDetermineHiveBitness), null); }
internal static extern int RegDeleteKey(RegistryHandle hKey, String lpValueName);
internal static extern int RegSetValueEx(RegistryHandle hKey, String lpValueName, int Reserved, int dwType, String val, int cbData);
internal static extern int RegCreateKeyEx(RegistryHandle hKey, String lpSubKey, int Reserved, String lpClass, int dwOptions, int samDesigner, IntPtr lpSecurityAttributes, out RegistryHandle hkResult, out int lpdwDisposition);
internal static extern int RegQueryValueEx(RegistryHandle hKey, String lpValueName, int[] lpReserved, ref int lpType, [Out] byte[] lpData, ref int lpcbData);