1 //------------------------------------------------------------------------------
2 // <copyright file="Package.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
9 using Microsoft
.VisualStudio
.Shell
.Interop
;
11 using ErrorHandler
= Microsoft
.VisualStudio
.ErrorHandler
;
12 using IServiceProvider
= System
.IServiceProvider
;
13 using IOleServiceProvider
= Microsoft
.VisualStudio
.OLE
.Interop
.IServiceProvider
;
15 namespace Microsoft
.VisualStudio
.Shell
{
17 /// Helper class to handle the registry of the instance of VS that is
18 /// hosting this code.
20 [System
.CLSCompliant(false)]
21 public static class VSRegistry
{
23 private static ServiceProvider GlobalProvider
{
25 return ServiceProvider
.GlobalProvider
;
30 /// Returns a read-only RegistryKey object for the root of a given storage type.
31 /// It is up to the caller to dispose the returned object.
33 /// <param name="registryType">The type of registry storage to open.</param>
34 public static RegistryKey
RegistryRoot(__VsLocalRegistryType registryType
) {
35 return RegistryRoot(GlobalProvider
, registryType
, false);
39 /// Returns a RegistryKey object for the root of a given storage type.
40 /// It is up to the caller to dispose the returned object.
42 /// <param name="registryType">The type of registry storage to open.</param>
43 /// <param name="writable">Flag to indicate is the key should be writable.</param>
44 public static RegistryKey
RegistryRoot(__VsLocalRegistryType registryType
, bool writable
) {
45 return RegistryRoot(GlobalProvider
, registryType
, writable
);
49 /// Returns a RegistryKey object for the root of a given storage type.
50 /// It is up to the caller to dispose the returned object.
52 /// <param name="provider">The service provider to use to access the Visual Studio's services.</param>
53 /// <param name="registryType">The type of registry storage to open.</param>
54 /// <param name="writable">Flag to indicate is the key should be writable.</param>
55 public static RegistryKey
RegistryRoot(IServiceProvider provider
, __VsLocalRegistryType registryType
, bool writable
) {
56 if (null == provider
) {
57 throw new ArgumentNullException("provider");
60 // The current implementation of the shell supports only RegType_UserSettings and
61 // RegType_Configuration, so for any other values we have to return not implemented.
62 if ((__VsLocalRegistryType
.RegType_UserSettings
!= registryType
) &&
63 (__VsLocalRegistryType
.RegType_Configuration
!= registryType
))
65 throw new NotSupportedException();
68 // Try to get the new ILocalRegistry4 interface that is able to handle the new
70 ILocalRegistry4 localRegistry
= provider
.GetService(typeof(SLocalRegistry
)) as ILocalRegistry4
;
71 if (null != localRegistry
) {
74 if (ErrorHandler
.Succeeded(localRegistry
.GetLocalRegistryRootEx((uint)registryType
, out rootHandle
, out rootPath
))) {
75 // Check if we have valid data.
76 __VsLocalRegistryRootHandle handle
= (__VsLocalRegistryRootHandle
)rootHandle
;
77 if (!string.IsNullOrEmpty(rootPath
) && (__VsLocalRegistryRootHandle
.RegHandle_Invalid
!= handle
)) {
78 // Check if the root is inside HKLM or HKCU. Note that this does not depends only from
79 // the registry type, but also from instance-specific data like the RANU flag.
80 RegistryKey root
= (__VsLocalRegistryRootHandle
.RegHandle_LocalMachine
== handle
) ? Registry
.LocalMachine
: Registry
.CurrentUser
;
81 return root
.OpenSubKey(rootPath
, writable
);
86 // We are here if the usage of the new interface failed for same reason, so we have to fall back to
87 // the ond way to access the registry.
88 ILocalRegistry2 oldRegistry
= provider
.GetService(typeof(SLocalRegistry
)) as ILocalRegistry2
;
89 if (null == oldRegistry
) {
90 // There is something wrong with this installation or this service provider.
94 NativeMethods
.ThrowOnFailure(oldRegistry
.GetLocalRegistryRoot(out registryPath
));
95 if (string.IsNullOrEmpty(registryPath
)) {
99 RegistryKey regRoot
= (__VsLocalRegistryType
.RegType_Configuration
== registryType
) ? Registry
.LocalMachine
: Registry
.CurrentUser
;
100 return regRoot
.OpenSubKey(registryPath
, writable
);