public StdRegProviderWrapper(RegistryHive registryHive, string subKey, string machineName) { switch (registryHive) { case RegistryHive.ClassesRoot: this.hiveValue = 0x80000000; break; case RegistryHive.CurrentUser: this.hiveValue = 0x80000001; break; case RegistryHive.LocalMachine: this.hiveValue = 0x80000002; break; default: // We do not support other values here throw new ArgumentException("remoteHive"); } registryExceptionHelper = new RegistryExceptionHelper(machineName, registryHive, subKey); try { ConnectionOptions co = null; if (Utilities.IsLocalMachineName(machineName)) { machineName = "localhost"; } else { co = new ConnectionOptions(); co.Authentication = AuthenticationLevel.PacketPrivacy; co.Impersonation = ImpersonationLevel.Impersonate; } ManagementScope managementScope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", co); ManagementPath managementPath = new ManagementPath("StdRegProv"); ObjectGetOptions options = new ObjectGetOptions(new ManagementNamedValueCollection(), TimeSpan.FromSeconds(15), false); this.regClassInstance = new ManagementClass(managementScope, managementPath, options); this.subKey = subKey; } catch (ManagementException e) { throw registryExceptionHelper.CreateRegistryAccessException(e); } catch (COMException e) // for RPC_S_SERVER_UNAVAILABLE sort of errors { throw registryExceptionHelper.CreateRegistryAccessException(e); } }
object DoReadData(string name, string valueKey, object defaultValue, string readMethod) { EnsureReadAccess(); try { ManagementBaseObject inParams = regClassInstance.GetMethodParameters(readMethod); inParams[InputParameters.DefKey] = this.hiveValue; inParams[InputParameters.SubKeyName] = subKey; inParams[InputParameters.ValueName] = name; ManagementBaseObject outParams = regClassInstance.InvokeMethod(readMethod, inParams, null); uint ret = (uint)outParams[OutputParameters.ReturnValue]; if (ret == 0) // zero means success { return(outParams[valueKey]); } return(defaultValue); } #pragma warning suppress 56500 catch (Exception e) { // MSDN does not have a spec of possible exceptions for the APIs used above. // To be safe, we should be a bit more generic in catching exceptions if (Utilities.IsCriticalException(e)) { throw; } throw registryExceptionHelper.CreateRegistryAccessException(name, e); } }
// // LH: Cluster\Resources\GUID_OF_DTC\MSDTCPrivate\MSDTC // W2k3: Cluster\Resources\GUID_OF_DTC\SOME_GUID\, here SOME_GUID is the default value of GUID_OF_DTC\DataPointer\ // string GetClusterMstdcRegistryKey() { Debug.Assert(IsClustered); if (Utilities.OSMajor > 5) { return(WsatKeys.MsdtcClusterRegKey_OS6); } ClusterRegistryConfigurationProvider clusterReg = new ClusterRegistryConfigurationProvider(this.hClusterDtcResource, WsatKeys.MsdtcClusterDataPointerRegKey_OS5); using (clusterReg) { //the default value string subKey = clusterReg.ReadString(string.Empty, string.Empty); if (!string.IsNullOrEmpty(subKey)) { return(subKey); } } RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(WsatKeys.MsdtcClusterDataPointerRegKey_OS5); throw registryExceptionHelper.CreateRegistryAccessException(null); }
internal static SafeCertificateStore GetCertificateStorePointer(string machineName) { SafeCertificateStore storeHandle; RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(machineName, RegistryHive.LocalMachine, certificateStore); if (Utilities.IsLocalMachineName(machineName)) { SafeRegistryKey hive = new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false); SafeRegistryKey regKey = null; try { int ret = SafeNativeMethods.RegOpenKeyEx( hive, certificateStore, 0, SafeNativeMethods.KEY_READ, out regKey); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } storeHandle = SafeNativeMethods.CertOpenStore_ptr( SafeNativeMethods.CERT_STORE_PROV_REG, 0, 0, SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG | SafeNativeMethods.CERT_STORE_READONLY_FLAG, regKey); if (storeHandle.IsInvalid) { throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error())); } return storeHandle; } finally { if (regKey != null) { regKey.Close(); } hive.Close(); } } #if WSAT_UI else { SafeRegistryKey remoteBase = null; SafeRegistryKey finalKey = null; try { int ret = SafeNativeMethods.RegConnectRegistry( machineName, new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false), out remoteBase); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } ret = SafeNativeMethods.RegOpenKeyEx( remoteBase, certificateStore, 0, SafeNativeMethods.KEY_READ, out finalKey); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } storeHandle = SafeNativeMethods.CertOpenStore_ptr( SafeNativeMethods.CERT_STORE_PROV_REG, 0, 0, SafeNativeMethods.CERT_REGISTRY_STORE_REMOTE_FLAG | SafeNativeMethods.CERT_STORE_READONLY_FLAG | SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG, finalKey); if (storeHandle.IsInvalid) { throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error())); } return storeHandle; } finally { if (remoteBase != null) { remoteBase.Close(); } if (finalKey != null) { finalKey.Close(); } } } #else else { throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, 0)); } #endif }
public StdRegProviderWrapper(RegistryHive registryHive, string subKey, string machineName) { switch (registryHive) { case RegistryHive.ClassesRoot: this.hiveValue = 0x80000000; break; case RegistryHive.CurrentUser: this.hiveValue = 0x80000001; break; case RegistryHive.LocalMachine: this.hiveValue = 0x80000002; break; default: // We do not support other values here throw new ArgumentException("remoteHive"); } registryExceptionHelper = new RegistryExceptionHelper(machineName, registryHive, subKey); try { ConnectionOptions co = null; if (Utilities.IsLocalMachineName(machineName)) { machineName = "localhost"; } else { co = new ConnectionOptions(); co.Authentication = AuthenticationLevel.PacketPrivacy; co.Impersonation = ImpersonationLevel.Impersonate; } ManagementScope managementScope = new ManagementScope("\\\\" + machineName + "\\root\\DEFAULT", co); ManagementPath managementPath = new ManagementPath("StdRegProv"); ObjectGetOptions options = new ObjectGetOptions(new ManagementNamedValueCollection(), TimeSpan.FromSeconds(15), false); this.regClassInstance = new ManagementClass(managementScope, managementPath, options); this.subKey = subKey; } catch (ManagementException e) { throw registryExceptionHelper.CreateRegistryAccessException(e); } catch (COMException e) // for RPC_S_SERVER_UNAVAILABLE sort of errors { throw registryExceptionHelper.CreateRegistryAccessException(e); } }
internal static SafeCertificateStore GetCertificateStorePointer(string machineName) { SafeCertificateStore storeHandle; RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(machineName, RegistryHive.LocalMachine, certificateStore); if (Utilities.IsLocalMachineName(machineName)) { SafeRegistryKey hive = new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false); SafeRegistryKey regKey = null; try { int ret = SafeNativeMethods.RegOpenKeyEx( hive, certificateStore, 0, SafeNativeMethods.KEY_READ, out regKey); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } storeHandle = SafeNativeMethods.CertOpenStore_ptr( SafeNativeMethods.CERT_STORE_PROV_REG, 0, 0, SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG | SafeNativeMethods.CERT_STORE_READONLY_FLAG, regKey); if (storeHandle.IsInvalid) { throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error())); } return(storeHandle); } finally { if (regKey != null) { regKey.Close(); } hive.Close(); } } #if WSAT_UI else { SafeRegistryKey remoteBase = null; SafeRegistryKey finalKey = null; try { int ret = SafeNativeMethods.RegConnectRegistry( machineName, new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false), out remoteBase); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } ret = SafeNativeMethods.RegOpenKeyEx( remoteBase, certificateStore, 0, SafeNativeMethods.KEY_READ, out finalKey); if (ret != SafeNativeMethods.ERROR_SUCCESS) { throw registryExceptionHelper.CreateRegistryAccessException(ret); } storeHandle = SafeNativeMethods.CertOpenStore_ptr( SafeNativeMethods.CERT_STORE_PROV_REG, 0, 0, SafeNativeMethods.CERT_REGISTRY_STORE_REMOTE_FLAG | SafeNativeMethods.CERT_STORE_READONLY_FLAG | SafeNativeMethods.CERT_STORE_OPEN_EXISTING_FLAG, finalKey); if (storeHandle.IsInvalid) { throw new WsatAdminException(WsatAdminErrorCode.CERT_STORE_ACCESS, SR.GetString(SR.ErrorAccessCertStore, Marshal.GetLastWin32Error())); } return(storeHandle); } finally { if (remoteBase != null) { remoteBase.Close(); } if (finalKey != null) { finalKey.Close(); } } }
// // LH: Cluster\Resources\GUID_OF_DTC\MSDTCPrivate\MSDTC // W2k3: Cluster\Resources\GUID_OF_DTC\SOME_GUID\, here SOME_GUID is the default value of GUID_OF_DTC\DataPointer\ // string GetClusterMstdcRegistryKey() { Debug.Assert(IsClustered); if (Utilities.OSMajor > 5) { return WsatKeys.MsdtcClusterRegKey_OS6; } ClusterRegistryConfigurationProvider clusterReg = new ClusterRegistryConfigurationProvider(this.hClusterDtcResource, WsatKeys.MsdtcClusterDataPointerRegKey_OS5); using (clusterReg) { //the default value string subKey = clusterReg.ReadString(string.Empty, string.Empty); if (!string.IsNullOrEmpty(subKey)) { return subKey; } } RegistryExceptionHelper registryExceptionHelper = new RegistryExceptionHelper(WsatKeys.MsdtcClusterDataPointerRegKey_OS5); throw registryExceptionHelper.CreateRegistryAccessException(null); }