如何在Windows中模拟注册表进行程序测试

How to emulate registry in Windows for program testing?

本文关键字:程序 测试 注册表 模拟 Windows      更新时间:2023-10-16

我的应用程序大量读取和更改windows注册表。由于应用程序的性质,有可能破坏系统。

为了避免系统破坏,我想创建一个注册表的临时副本,并使用copy()。所有的注册文件?或任何其他合适的导出格式)在我的应用程序。我更愿意保持所有的窗口函数相同,所以,为了测试,我想挂钩我自己的应用程序的注册表函数与DLL,将所有注册表访问重定向到一个文件。

我确实找了一些库,但是没有这样的东西可以做这个,或者我不知道我在找什么。在这种情况下我能做些什么?

简而言之:

我想模拟windows注册表

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

钩子dll将钩子所有的windows注册表函数并重定向到dll目录下的一个文件。

是否有windows注册表功能的开源实现?我只有标题,但我需要完全相同的行为作为windows提供彻底测试应用程序。

在这种情况下我能做些什么?

在注册表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,它将被注入到我自己的应用程序

这听起来很复杂(与x-y问题相似)。对您来说,实现上面的解决方案是否令人望而却步?