代码之家  ›  专栏  ›  技术社区  ›  dummzeuch Stijn Sanders

如何重定向由我的程序加载的dll的注册表访问

  •  4
  • dummzeuch Stijn Sanders  · 技术社区  · 15 年前

    我有一个动态链接库,我在我的程序中加载它的设置读写到注册表(hkcu)。我的程序在加载dll之前会更改这些设置,因此它使用我的程序希望它使用的设置,这样可以正常工作。

    不幸的是,我需要用不同的DLL设置来运行我的程序的几个实例。现在,到目前为止我使用的方法不再可靠,因为程序的一个实例可能会覆盖另一个实例在DLL有机会读取它们之前刚刚写入的设置。

    我还没有找到有问题的dll的来源,我不能要求编写它的程序员更改它。

    我的一个想法是挂接注册表访问函数,并将它们重定向到注册表的另一个分支,该分支是特定于我的程序实例的(例如,将进程ID用作路径的一部分)。我认为这应该有效,但也许你有一个不同的/更优雅的。

    万一有问题:我用Delphi 2007来编写程序,DLL可能是用C或C++编写的。

    3 回复  |  直到 15 年前
        1
  •  4
  •   Ondrej Kelle    15 年前

    作为API挂钩的替代方案,也许您可以使用 RegOverridePredefKey 应用程序编程接口。

        2
  •  3
  •   RRUZ    15 年前

    您可以使用进程间锁定机制将自己应用程序的值写入注册表,而不是挂接dll的注册表访问。这种想法是,在Instance1的dll“Instance”读取值之前,不会释放Instance1获取的锁,这样,当Instance2启动时,在Instance1完成之前,它不会获取锁。您需要一个在进程之间工作的锁定机制,这样才能工作。例如互斥体。


    要创建互斥体:

    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;
    

    要释放互斥体,您将使用release mutex API,要获取创建的互斥体,您将使用openmutex API。

    有关CreateMutex,请参见: http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx

    有关OpenMutex,请参见: http://msdn.microsoft.com/en-us/library/ms684315(v=VS.85).aspx

    有关releasemutex,请参见: http://msdn.microsoft.com/en-us/library/ms685066(v=VS.85).aspx

        3
  •  0
  •   lImbus    15 年前

    脏方法:在HexEditor中打开dll,并将注册表路径从原始配置单元更改/更改为您要使用或具有正确设置的任何其他配置单元。

    推荐文章