static bool RegistryEntryExists() { bool regKeyExists = false; using (SafeRegistryKey hive = new SafeRegistryKey(new IntPtr((int)Microsoft.Win32.RegistryHive.LocalMachine), false)) { SafeRegistryKey regKey = null; int ret = SafeNativeMethods.RegOpenKeyEx( hive, RegKeyClassIdRoot + CLSID_CGatewayProtocol, 0, SafeNativeMethods.KEY_WOW64_64KEY | SafeNativeMethods.KEY_QUERY_VALUE, out regKey); if (ret == 0 && regKey != null && !regKey.IsInvalid) { regKeyExists = true; regKey.Close(); } } return(regKeyExists); }
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 }
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(); } } }