如何在Windows中模拟registry进行程序testing?

我的应用程序广泛读取和更改Windowsregistry。 由于应用程序的性质,可能会破坏系统。

为了避免系统破坏,我想创build我的registry的临时副本,并在我的应用程序中使用copy(.reg文件的所有内容?或任何其他合适的导出格式)。 我宁愿保持所有的窗口函数相同,所以,为了testing我想钩我自己的应用程序的registry函数与DLL将redirect到一个文件的所有registry访问。

我找了几个图书馆,但没有这样的事情,或者我不知道如何search。 我可以在我的情况下做什么?

简而言之:

我想模拟Windowsregistry

我想创build一个钩子DLL,它将被注入到我自己的应用程序中

钩子DLL将钩住所有的Windowsregistry函数,并将它们redirect到DLL目录中的文件。

有没有Windowsregistryfunction的开源实现? 我只有标题,但我需要与Windows提供的完全相同的行为来彻底testing应用程序。

我可以在我的情况下做什么?

在注册表API的顶部实现一个抽象层,并通过抽象层访问API。 然后,将实现注入到需要注册表访问的代码中。

class SettingsStore { public: SettingsStore(const std::string&); // receive path or "unique key" for your settings virtual ~SettingsStore() = 0; virtual std::string GetValue(const std::string& key) = 0; virtual void SetValue(const std::string& key, const std::string& value) = 0; // ... }; class RegistryStore: public SettingsStore { public: SettingsStore(const std::string&); // receive path or "unique key" for your settings virtual ~SettingsStore(); // implement in terms of Windows Registry API virtual std::string GetValue(const std::string& key) override; virtual void SetValue(const std::string& key, const std::string& value) override; // ... private: // registry handle here }; 

之后,根据注入的SettingsStore引用来实现您的代码。

你的测试代码可以依赖于TestStore(扩展了SettingsStore)等等。

简而言之:

我想模拟Windows注册表

我想创建一个钩子DLL,它将被注入到我自己的应用程序中

这听起来很复杂(和xy问题类似)。 以上实施解决方案是否令人望而却步?