Share configuration between DLLs

c++ / delphi package - dll injection and api hooking
Post Reply
mutantc0der
Posts: 11
Joined: Thu Jun 17, 2004 1:48 pm

Share configuration between DLLs

Post by mutantc0der »

Hi

I want to send a set of instructions to each injected DLL. My first approach is to send them via IPC messages, but as madshi stated before, it isnt that fast. What is the best solution ? e.g is it possible to share a global block of memory each DLL can access. or should i use:

* config files
* sockets (winsock.dll might not be loaded in each process?)
* IPC
* registry (is advapi.dll always loaded?)

What is the most fastest and non-resource-eating way of sharing config to DLLs in certain intervals??

thanks!
nildo
Posts: 249
Joined: Mon Mar 22, 2004 11:32 am
Contact:

Post by nildo »

Use a file mapping. Its fast, like access to memory (coz it is :-x )

You need to create a pointer to a record that has your configurations.

Code: Select all

   FHandle := CreateFileMapping($FFFFFFFF, nil, 4, 0, $ffff, 'Ipd_DLL');
   // This creates the mapping

   if FHandle = 0 then
   begin
      if GetLastError = 183 then
      begin
         FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'Ipd_DLL');
         // This open the mapping

         if FHandle = 0 then
            Exit;
      end
      else
         Exit;
   end;

   PointerToYourRecord := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
This you must do on your DLL and on your program, when loading. Then, to read information in your DLL is easy:

Code: Select all

   Blablabla := PointerToYourRecord^.YourInfo;
And to write your information by your EXE is easy too.

Code: Select all

   PointerToYourRecord^.YourInfo := 'testing';
mutantc0der
Posts: 11
Joined: Thu Jun 17, 2004 1:48 pm

Post by mutantc0der »

thanks nildo, this sure looks good! i was wondering, what happends when , say, ~ 100 DLL instances write to this mapped memory file , who syncs the access, windows ? or do i have to work with mutexes/critical sections ?
mutantc0der
Posts: 11
Joined: Thu Jun 17, 2004 1:48 pm

Post by mutantc0der »

dohh nevermind, i just figured out i can name each mapped similair as the process exe + path with a random integer at the end, and will report it to the controlling app with an IPC message

thanks nildo!
nildo
Posts: 249
Joined: Mon Mar 22, 2004 11:32 am
Contact:

Post by nildo »

mutantc0der wrote:dohh nevermind, i just figured out i can name each mapped similair as the process exe + path with a random integer at the end, and will report it to the controlling app with an IPC message

thanks nildo!
Thats it!
But if its the same configuration for ALL those 100 DLLs, I think it will have no problem if you put a CriticalSection there.
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Hey, I think I can stop giving advices here. nildo is doing it just fine... :D

Just some comments:

(1) Using madCodeHook.Create/OpenGlobalFileMapping is recommend, because those functions work over the boundaries of XP fast user switching sessions.

(2) mutantc0der, first you ask how you can send config info to the dlls. Then you ask what happens if all dlls write to the config block at the same time. Heh? :confused: If your application wants to send config data to the dlls, then use a global memory block (as suggested by nildo). But it doesn't make ANY sense for the dlls to write to such a config block. Use something else (e.g. SendIpcMessage; or a different global memory block) if the dlls want to report something back to the application. You shouldn't mix the config block with dll-to-exe information transports.
nildo
Posts: 249
Joined: Mon Mar 22, 2004 11:32 am
Contact:

Post by nildo »

madshi wrote:Hey, I think I can stop giving advices here. nildo is doing it just fine... :D
Doing this I can learn more!
Thats because I am from a the best Delphi Fórum here from Brazil, I am a moderator. So I think its instinct. Ahh, don't forget that every thing that was explained, I've learned with you...
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

:D

I'm glad if you help here, cause that saves me time!
Post Reply