Question

My application extensively reads and changes the windows registry. Because of the nature of the application there is possibility to destroy the system.

To avoid system destruction I want to create a temporary copy of my registry and use the copy (.reg file of everything? Or any other suitable export format) in my application. I'd prefer to keep all the windows functions the same, so, for testing I want to hook my own application's Registry functions with a DLL which would redirect all registry accesses to a file.

I did look for a few libraries but there is no such thing to do this, or maybe I don't know what so search for. What can I do in my situation?

In short:

I want to emulate the windows registry

I want to create a hook DLL, which will be injected into my own application

The hook dll will hook all windows registry functions and redirect them to a file in the DLL directory.

Is there an open source implementation of the windows Registry functions? I only have the headers but I need exact the same behaviour as windows offers to test the application thoroughly.

Était-ce utile?

La solution

What can I do in my situation?

Implement an abstraction layer on top of the registry API and access the API through the abstraction layer. Then, inject an implementation into the code that needs registry access.

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
};

After this, implement your code in terms of an injected SettingsStore reference.

Your test code then can depend on TestStore (that extends SettingsStore) or whatever.

In short:

I want to emulate the windows registry

I want to create a hook DLL, which will be injected into my own application

This sounds complicated (and similar to the x-y problem). Is it prohibitive for you to implement the solution above?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top