How to redirect registry access to a DLL loaded by my program

I have a DLL that I load into my program, which reads and writes its settings to the registry (hkcu). My program modifies these settings before loading the dll, so it uses the settings that my program wants to use, which works great.

Unfortunately, I need to run several instances of my program with different settings for the dll. Now the approach that I used so far no longer works reliably, because one instance of the program can overwrite the parameters that another instance just wrote before the dll can read them.

I do not have the source of this DLL, and I cannot ask the programmer who wrote it to modify it.

One of my ideas was to intercept registry access functions and redirect them to another registry branch that is specific to my program instance (for example, use the process identifier as part of the path). I think this should work, but maybe you have a different / more elegant one.

In case it matters: I'm using Delphi 2007 for my program, the dll is probably written in C or C ++.

+4
source share
3 answers

As an alternative to connecting an API, perhaps you can use the RegOverridePredefKey API.

+4
source

Instead of intercepting registry access for the dll, you can use the inter-process locking mechanism to write values ​​to the registry for your own application. The idea is that the lock obtained by the instance1 instance is not released until its "instance" DLL reads the value, so that when instance2 is started, it will not get the lock until instance1 is complete. You will need a locking mechanism that works between processes to do this. For example, mutexes.


To create mutexes:

procedure CreateMutexes(const MutexName: string); //Creates the two mutexes checked for by the installer/uninstaller to see if //the program is still running. //One of the mutexes is created in the global name space (which makes it //possible to access the mutex across user sessions in Windows XP); the other //is created in the session name space (because versions of Windows NT prior //to 4.0 TSE don't have a global name space and don't support the 'Global\' //prefix). const SECURITY_DESCRIPTOR_REVISION = 1; // Win32 constant not defined in Delphi 3 var SecurityDesc: TSecurityDescriptor; SecurityAttr: TSecurityAttributes; begin // By default on Windows NT, created mutexes are accessible only by the user // running the process. We need our mutexes to be accessible to all users, so // that the mutex detection can work across user sessions in Windows XP. To // do this we use a security descriptor with a null DACL. InitializeSecurityDescriptor(@SecurityDesc, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(@SecurityDesc, True, nil, False); SecurityAttr.nLength := SizeOf(SecurityAttr); SecurityAttr.lpSecurityDescriptor := @SecurityDesc; SecurityAttr.bInheritHandle := False; CreateMutex(@SecurityAttr, False, PChar(MutexName)); CreateMutex(@SecurityAttr, False, PChar('Global\' + MutexName)); end; 

To free a mutex, you must use the ReleaseMutex API and purchase the created mutex, you must use the OpenMutex API.

For CreateMutex see: http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx

For OpenMutex see: http://msdn.microsoft.com/en-us/library/ms684315(v=VS.85).aspx

For ReleaseMutex see: http://msdn.microsoft.com/en-us/library/ms685066(v=VS.85).aspx

+3
source

dirty method: open the dll in hex code and change / change the registry path from the source hive to any other that you want to use, or you have the appropriate settings.

0
source

Source: https://habr.com/ru/post/1313152/


All Articles