public static ImplementationInterface GetInterface(string guid)
            {
                ImplementationInterface result = new ImplementationInterface();

                result.Guid = guid;
                if (result.Guid == null)
                {
                    return(null);
                }
                result.Name = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Interface\\" + guid);
                if (result.Name == null)
                {
                    return(null);
                }
                result.ProxyStubClsid32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Interface\\" + guid + "\\ProxyStubClsid32");
                if (result.ProxyStubClsid32 == null)
                {
                    return(null);
                }
                result.TypeLib = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Interface\\" + guid + "\\TypeLib");
                if (result.TypeLib == null)
                {
                    return(null);
                }
                result.TypeLibVersion = RegistryManipulator.GetStringValue("HKEY_CLASSES_ROOT\\Interface\\" + guid + "\\TypeLib", "Version");
                if (result.TypeLibVersion == null)
                {
                    return(null);
                }

                //success
                return(result);
            }
            private static bool AppendComponentRegistry(string keyName, ref byte[] bytes)
            {
                //verify if key exists
                if (!RegistryManipulator.KeyExists(keyName))
                {
                    return(false);
                }

                //get registry content of the key
                byte[] tmpBytes = null;
                RegistryManipulator.ExportKey(keyName, ref tmpBytes);
                if (tmpBytes == null)
                {
                    return(false);
                }

                //remove the header
                RegistryManipulator.RemoveRegistryFileHeader(ref tmpBytes);

                //append to existing content
                var newContent = new byte[bytes.Length + tmpBytes.Length];

                bytes.CopyTo(newContent, 0);
                tmpBytes.CopyTo(newContent, bytes.Length);

                //assigned the new array
                bytes = newContent;
                return(true);
            }
            public static bool ExportComponentRegistry(string className, string filePath)
            {
                byte[] bytes = null;

                if (!RegistryManipulator.HasClass(className))
                {
                    return(false);
                }

                // className
                RegistryManipulator.ExportKey("HKEY_CLASSES_ROOT\\" + className, ref bytes);

                // CurVer
                string CurVer = GetClassCurVer(className);

                AppendComponentRegistry("HKEY_CLASSES_ROOT\\" + CurVer, ref bytes);

                // AppID
                string componentFilePath = QueryManager.GetComponentFilePath(className);

                if (componentFilePath != null)
                {
                    string execFilename = Path.GetFileName(componentFilePath);
                    string appid        = RegistryManipulator.GetStringValue("HKEY_CLASSES_ROOT\\AppID\\" + execFilename, "AppID");
                    if (appid != null)
                    {
                        AppendComponentRegistry("HKEY_CLASSES_ROOT\\AppID\\" + execFilename, ref bytes);
                        AppendComponentRegistry("HKEY_CLASSES_ROOT\\AppID\\" + appid, ref bytes);
                        AppendComponentRegistry("HKEY_CLASSES_ROOT\\Wow6432Node\\AppID\\" + execFilename, ref bytes);
                        AppendComponentRegistry("HKEY_CLASSES_ROOT\\Wow6432Node\\AppID\\" + appid, ref bytes);
                    }
                }

                // CLSID
                string CurVerCLSID = GetClassID(CurVer);

                AppendComponentRegistry("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + CurVerCLSID, ref bytes);

                // TypeLib
                string typelib = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + CurVerCLSID + "\\TypeLib");

                if (typelib != null)
                {
                    AppendComponentRegistry("HKEY_CLASSES_ROOT\\TypeLib\\" + typelib, ref bytes);
                    AppendComponentRegistry("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + typelib, ref bytes);

                    // interface
                    string[] interfaces = GetTypeLibInterfaces(typelib);
                    foreach (string interfaceGuid in interfaces)
                    {
                        AppendComponentRegistry("HKEY_CLASSES_ROOT\\Interface\\" + interfaceGuid, ref bytes);
                    }
                }

                //write to output file
                File.WriteAllBytes(filePath, bytes);

                return(true);
            }
 public void GetRegistryHiveTest()
 {
     Assert.AreEqual(Registry.ClassesRoot, RegistryManipulator.GetRegistryHive("HKEY_CLASSES_ROOT\\._sln"));
     Assert.AreEqual(Registry.CurrentConfig, RegistryManipulator.GetRegistryHive("HKEY_CURRENT_CONFIG\\Software"));
     Assert.AreEqual(Registry.CurrentUser, RegistryManipulator.GetRegistryHive("HKEY_CURRENT_USER\\Software"));
     Assert.AreEqual(Registry.LocalMachine, RegistryManipulator.GetRegistryHive("HKEY_LOCAL_MACHINE\\SOFTWARE"));
     Assert.AreEqual(Registry.Users, RegistryManipulator.GetRegistryHive("HKEY_USERS\\.DEFAULT"));
 }
        public void GetSubKeysTest()
        {
            string keyName = "HKEY_LOCAL_MACHINE\\HARDWARE";

            string[] expected = new string[] { "ACPI", "DESCRIPTION", "DEVICEMAP", "RESOURCEMAP" };
            string[] actual;
            actual = RegistryManipulator.GetSubKeys(keyName);
            CollectionAssert.AreEqual(expected, actual);
        }
        public void GetKeyPathTest()
        {
            string expected = "Software\\foobar";

            Assert.AreEqual(expected, RegistryManipulator.GetKeyPath("HKEY_CLASSES_ROOT\\" + expected));
            Assert.AreEqual(expected, RegistryManipulator.GetKeyPath("HKEY_CURRENT_CONFIG\\" + expected));
            Assert.AreEqual(expected, RegistryManipulator.GetKeyPath("HKEY_CURRENT_USER\\" + expected));
            Assert.AreEqual(expected, RegistryManipulator.GetKeyPath("HKEY_LOCAL_MACHINE\\" + expected));
            Assert.AreEqual(expected, RegistryManipulator.GetKeyPath("HKEY_USERS\\" + expected));
        }
            public static string GetClassID(string className)
            {
                string guid = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\" + className + "\\CLSID");

                if (guid == null)
                {
                    return(null);
                }
                return(guid);
            }
        public void GetStringValueTest()
        {
            string keyName   = "HKEY_CLASSES_ROOT\\.txt";
            string valueName = "Content Type";
            string expected  = "text/plain";
            string actual;

            actual = RegistryManipulator.GetStringValue(keyName, valueName);
            Assert.AreEqual(expected, actual);
        }
            public static string GetClassCurVer(string className)
            {
                if (!RegistryManipulator.HasClass(className))
                {
                    return(null); //not such class
                }
                string CurVer = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\" + className + "\\CurVer");

                if (CurVer == null)
                {
                    return(className);//className is the last version
                }
                return(CurVer);
            }
            public static TypeLib GetTypeLib(string guid, string version)
            {
                TypeLib result = new TypeLib();

                result.Guid = guid;
                if (result.Guid == null)
                {
                    return(null);
                }
                result.Name = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version);
                if (result.Name == null)
                {
                    return(null);
                }
                result.Win32Path = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version + "\\0\\win32");
                if (result.Win32Path == null)
                {
                    //sub key under version may be something different than "0".
                    string[] subVersions = RegistryManipulator.GetSubKeys("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version);
                    if (subVersions.Length == 0)
                    {
                        return(null);
                    }
                    string firstSubVersion = subVersions[0];
                    result.Win32Path = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version + "\\" + firstSubVersion + "\\win32");
                    if (result.Win32Path == null)
                    {
                        return(null); //failed
                    }
                }
                result.Flags = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version + "\\FLAGS");
                if (result.Flags == null)
                {
                    return(null);
                }
                result.HelpDir = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + guid + "\\" + version + "\\HELPDIR");
                if (result.HelpDir == null)
                {
                    return(null);
                }

                //success
                return(result);
            }
            public static string[] GetTypeLibInterfaces(string guid)
            {
                var typelibInterfaces = new List <string>();

                //get all interfaces of the system
                string[] interfaces = RegistryManipulator.GetSubKeys("HKEY_CLASSES_ROOT\\Interface");

                //for each interfaces
                foreach (string interfaceGuid in interfaces)
                {
                    string interfaceTypeLib = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Interface\\" + interfaceGuid + "\\TypeLib");
                    if (guid.Equals(interfaceTypeLib))
                    {
                        typelibInterfaces.Add(interfaceGuid);
                    }
                }

                return(typelibInterfaces.ToArray());
            }
            public static AppID GetAppID(string execName)
            {
                AppID result = new AppID();

                result.Guid = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\AppID\\" + execName);
                if (result.Guid == null)
                {
                    return(null);
                }
                result.Name = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\AppID\\" + result.Guid);
                if (result.Name == null)
                {
                    return(null);
                }

                //success
                result.ExecName = execName;
                return(result);
            }
            public static CLSID GetCLSID(string guid)
            {
                CLSID result = new CLSID();

                result.Guid = guid;
                if (result.Guid == null)
                {
                    return(null);
                }
                result.Name = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid);
                if (result.Name == null)
                {
                    return(null);
                }
                result.LocalServer32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\LocalServer32");
                //if (result.LocalServer32 == null)
                //  return null;
                result.InProcServer32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\InProcServer32");
                //if (result.InProcServer32 == null)
                //  return null;
                result.ProgID = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\ProgID");
                if (result.ProgID == null)
                {
                    return(null);
                }
                result.TypeLib = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\TypeLib");
                if (result.TypeLib == null)
                {
                    return(null);
                }
                result.VersionIndependentProgID = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\TypeLib");
                if (result.VersionIndependentProgID == null)
                {
                    return(null);
                }
                result.Version = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + guid + "\\Version");
                //if (result.Version == null)
                //  return null;

                //success
                return(result);
            }
        public void ExportKeyTest()
        {
            string keyName  = "HKEY_CLASSES_ROOT\\.txt";
            string filePath = Environment.GetEnvironmentVariable("TEMP") + "\\ExportKeyTest.tmp";
            bool   expected = true;
            bool   actual;

            System.IO.File.Delete(filePath);

            actual = RegistryManipulator.ExportKey(keyName, filePath);
            Assert.AreEqual(expected, actual);

            Assert.IsTrue(System.IO.File.Exists(filePath));

            //------------------------------------------------------

            byte[] data = null;
            actual = RegistryManipulator.ExportKey(keyName, ref data);
            Assert.AreEqual(expected, actual);
            Assert.IsNotNull(data);
            Assert.IsTrue(data.Length > 0);
        }
            public static string GetComponentFilePath(string className)
            {
                string CurVer = GetClassCurVer(className);
                string clsid  = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\" + CurVer + "\\CLSID");

                if (clsid == null)
                {
                    return(null);
                }

                string LocalServer32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + clsid + "\\LocalServer32");

                if (LocalServer32 != null)
                {
                    if (File.Exists(LocalServer32))
                    {
                        return(LocalServer32);
                    }
                    else
                    {
                        //try to clean the path to find the file
                        string cleanPath = CleanUpFilePath(LocalServer32);
                        if (cleanPath != null)
                        {
                            return(cleanPath);
                        }
                    }
                }

                string InProcServer32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + clsid + "\\InProcServer32");

                if (InProcServer32 != null)
                {
                    if (File.Exists(InProcServer32))
                    {
                        return(InProcServer32);
                    }
                    else
                    {
                        //try to clean the path to find the file
                        string cleanPath = CleanUpFilePath(InProcServer32);
                        if (cleanPath != null)
                        {
                            return(cleanPath);
                        }
                    }
                }

                //try to resolve with typelib
                string typelib = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + clsid + "\\TypeLib");

                if (typelib != null)
                {
                    string win32_x86 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\TypeLib\\" + typelib + "\\1.0\\0\\win32");
                    if (win32_x86 != null)
                    {
                        if (File.Exists(win32_x86))
                        {
                            return(win32_x86);
                        }
                        else
                        {
                            //try to clean the path to find the file
                            string cleanPath = CleanUpFilePath(win32_x86);
                            if (cleanPath != null)
                            {
                                return(cleanPath);
                            }
                        }
                    }

                    string win32_x64 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\TypeLib\\" + typelib + "\\1.0\\0\\win32");
                    if (win32_x64 != null)
                    {
                        if (File.Exists(win32_x64))
                        {
                            return(win32_x64);
                        }
                        else
                        {
                            //try to clean the path to find the file
                            string cleanPath = CleanUpFilePath(win32_x64);
                            if (cleanPath != null)
                            {
                                return(cleanPath);
                            }
                        }
                    }
                }
                //failed resolving with typelib

                //try to resolve with InprocServer32
                string InprocServer32 = RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\Wow6432Node\\CLSID\\" + clsid + "\\InprocServer32");

                if (InprocServer32 != null)
                {
                    if (File.Exists(InprocServer32))
                    {
                        return(InprocServer32);
                    }
                    else
                    {
                        //try to clean the path to find the file
                        string cleanPath = CleanUpFilePath(InprocServer32);
                        if (cleanPath != null)
                        {
                            return(cleanPath);
                        }
                    }
                }


                return(null);
            }
 public void GetKeyDefaultValueTest()
 {
     Assert.AreEqual("txtfile", RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\.txt"));
     Assert.AreEqual(null, RegistryManipulator.GetKeyDefaultValue("HKEY_CLASSES_ROOT\\foobar"));
 }
 public void KeyExistsTest()
 {
     Assert.IsTrue(RegistryManipulator.KeyExists("HKEY_CLASSES_ROOT\\.txt"));
     Assert.IsFalse(RegistryManipulator.KeyExists("HKEY_CLASSES_ROOT\\foobar"));
 }
 public void HasClassTest()
 {
     Assert.AreEqual(true, RegistryManipulator.HasClass("ADODB.Connection"));
     Assert.AreEqual(false, RegistryManipulator.HasClass("foobar"));
 }