2

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gizmo
  • 1,990
  • 1
  • 24
  • 50
  • Simply create your own set of functions for accessing the registry, and use those. There you have full control and can redirect requests to a faux registry. For which you decide the format. – Cheers and hth. - Alf May 13 '14 at 09:05
  • Do I really need to re-create all windows registry functions? Well then, if that's my only option then that's the way to go. – Gizmo May 13 '14 at 09:07
  • If your machine can take it, you could test the thing inside a well-sandboxed windows emulator. Emulators for XP do exist. – Bathsheba May 13 '14 at 09:09
  • Can you temporarily change the path used by your application? I'm thinking that you could mount a new registry hive -- look at RegLoadKey. Then you could operate on that without breaking anything. – Roger Lipscombe May 13 '14 at 09:09
  • Install a VM, install Windows on it, configure VS for remote debugging, debug on VM. – Alex K. May 13 '14 at 10:25
  • Yes I do use VMWare Workstation, but debugging on my physical PC seems faster (I don't know why but it is). As for changing the path: Can it be done remotely without editing the code of the main program? Also, what if there is a certain component (DLL) I dont't have the source for and it uses registry? How can I redirect that? I think hooking is my only option? – Gizmo May 13 '14 at 17:01

1 Answers1

4

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?

Community
  • 1
  • 1
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • This is indeed a very neat partial solution (+1 for that!), but what about closed source DLL's which perform registry tasks which some of the programs require? That is why I *would* like to make some custom DLL which redirects all registry acces, if that's possible at all.. If it means hooking/rewriting every single function of the windows registry headers then it shall be so. – Gizmo May 13 '14 at 17:01