private MapKeyPath ( string keyPath ) : string | ||
keyPath | string | The Windows Installer key path to map. |
return | string |
internal string MapKeyPath(string keyPath)
{
// Callers should not have to handle exceptions for control flow.
if (string.IsNullOrEmpty(keyPath) || 4 > keyPath.Length)
{
return null;
}
var root = keyPath.Substring(0, 2);
var path = keyPath.Substring(3);
var is64Bit = '2' == root[0];
var hive = root[1];
if (!this.Is64Bit)
{
// Simply return 32-bit paths as-is and null for 64-bit paths.
if (!is64Bit)
{
root = this.MapRoot(path, false, hive);
if (null != root)
{
return root + path;
}
}
}
else
{
// Return 64-bit paths as-is but map 32-bit paths to WOW64 node only if redirected.
if (is64Bit || !tree.Under(keyPath))
{
root = this.MapRoot(path, true, hive);
}
else
{
root = this.MapRoot(path, false, hive);
if ('3' == hive)
{
// Strip user SID from registry key.
var i = path.IndexOf(Path.DirectorySeparatorChar, 1);
path = path.Substring(i);
}
// The redirected root will be replaced.
if (0 == path.IndexOf(SOFTWARE, StringComparison.OrdinalIgnoreCase))
{
path = path.Substring(SOFTWARE.Length);
}
// Make sure the path doesn't reference the WOW64 node twice.
// Windows Installer seems to handle this correctly during installation.
if (0 == path.IndexOf(WOW64, StringComparison.OrdinalIgnoreCase))
{
path = path.Substring(WOW64.Length);
}
}
if (null != root)
{
return root + path;
}
}
return null;
}
public void MapKeyPathsIn64BitProcess() { var view = new RegistryView(true); Assert.IsTrue(view.Is64Bit); // Invalid paths. Assert.IsNull(view.MapKeyPath(null)); Assert.IsNull(view.MapKeyPath("TRE")); Assert.IsNull(view.MapKeyPath("TEST")); // 32-bit paths. Assert.AreEqual(@"HKEY_CLASSES_ROOT\Wow6432Node\CLSID\", view.MapKeyPath(@"00:\CLSID\"), true); Assert.AreEqual(@"HKEY_CURRENT_USER\Software\Wow6432Node\Classes\CLSID\", view.MapKeyPath(@"01:\Software\Classes\CLSID\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\TEST\", view.MapKeyPath(@"02:\Software\TEST\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\", view.MapKeyPath(@"02:\Software\Microsoft\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Microsoft\EventSystem\", view.MapKeyPath(@"02:\Software\Microsoft\EventSystem\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\TEST\", view.MapKeyPath(@"02:\Software\Wow6432Node\TEST\"), true); Assert.AreEqual(@"HKEY_USERS\S-1-5-18\Software\Wow6432Node\Classes\CLSID\", view.MapKeyPath(@"03:\S-1-5-18\Software\Classes\CLSID\"), true); // 64-bit paths. Assert.AreEqual(@"HKEY_CLASSES_ROOT\CLSID\", view.MapKeyPath(@"20:\CLSID\"), true); Assert.AreEqual(@"HKEY_CURRENT_USER\Software\Classes\CLSID\", view.MapKeyPath(@"21:\Software\Classes\CLSID\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\TEST\", view.MapKeyPath(@"22:\Software\TEST\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Microsoft\", view.MapKeyPath(@"22:\Software\Microsoft\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Microsoft\EventSystem\", view.MapKeyPath(@"22:\Software\Microsoft\EventSystem\"), true); Assert.AreEqual(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\TEST\", view.MapKeyPath(@"22:\Software\Wow6432Node\TEST\"), true); Assert.AreEqual(@"HKEY_USERS\S-1-5-18\Software\Classes\CLSID\", view.MapKeyPath(@"23:\S-1-5-18\Software\Classes\CLSID\"), true); }