14

I want to load the same dll e.g. Lib.dll multiple times!

-> need creating a new process (CreateProcess function) for every LoadLibrary!

Anyone have an example or some hints?!

Thx and greets

leon22
  • 5,280
  • 19
  • 62
  • 100
  • 1
    Why do you want to load the same DLL multiple times? – Frerich Raabe Sep 21 '12 at 11:53
  • Why exactly you would want to load it multiple times? You cannot load it multiple times in the same process. You can create multiple processes, and in the "logical sense" it will be loaded once for each process. Note that in the "physical" sense it may still be loaded once as the operating system will try to load it once and the reuse the same memory in all the processes (only the read only parts, of course, like code and static immutable data, anything mutable will by necessity be process specific). – Analog File Sep 21 '12 at 11:55
  • The dll itself load another dll; but it depends on the parameter of the call which one is to load. So I need multiple instances! – leon22 Sep 21 '12 at 11:56
  • @AnalogFile I know. This is why I have written how can I realize this with CreateProcess (more than 1 process)! – leon22 Sep 21 '12 at 11:57
  • @leon22: Then you should rethink the architecture, maybe using "session" system or using COM object. – Deanna Sep 21 '12 at 12:00
  • @leon22 well, is you problem how to load the dll multiple times or is it how to create a process. If you do not know how to create a process, that's what you should ask (and post the code you tried to use). – Analog File Sep 21 '12 at 12:08
  • http://stackoverflow.com/questions/3497516/does-loadlibrary-create-distinct-instances – Bachor Apr 20 '15 at 08:17
  • 1
    I am not the OP, but I have a legit, if convoluted, business case for this. Third party algorithm library, single threaded, stores everything in global state. I'd like to run several instances of it on threads. Rewriting to use nonglobal state is costly. Ergo, several instances with independent global states. – Seva Alekseyev Oct 07 '21 at 14:52

3 Answers3

27

It sounds like you want each instance of the DLL to have separate data segments. That's the only reason I can think of for the question.

The only way to achieve this is to make sure that each time you call LoadLibrary, the DLL has a different filename. Copy the DLL to a temporary file each time you need to load it, making sure that the name you use is different from any loaded instance of the DLL.

I echo the comments above that encourage you to re-design the system architecture.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
8

You can not load the same DLL multiple times into a single process (or not and have any effect).

From your comments, the DLL does different things depending on one of the function calls so you will need to use a "session" system where you keep separate sets of data for each and create them as needed (via another call) and pass a handle or similar to each function call. This is the way most of the Win32 API works (file handles, window handles, GDI objects, etc)

If you make the DLL a COM host and use COM objects then this will be automatically handled by each class instance.

If you want to use a separate process then you can do just that by having a new process launched just to host the DLL and use one of the many forms of IPC to communicate with it.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • 5
    "You can not load the same DLL multiple times into a single process (or not and have any effect)." Actually you can so long as the filenames differ. – David Heffernan Apr 28 '13 at 07:00
1

You are treating a DLL like an object instance. That's not at all how DLLs work. DLLs are not objects, they are a bunch of code and resources. These things do not change, no matter how many times you could theoretically load a DLL. Thus there would be no point in having multiple instances of the DLL loaded in the same process.

This is a great example of why global variables tend to be a bad idea. Data needs to be able to be instantiated as needed.

So if you need multiple instances of an object to work with, you should design the DLL to do exactly that. As others have said, some kind of session, or just some object that you can instantiate whenever you want.

This is an abstract answer to an abstract question. It would help a LOT if you could explain more about what this DLL does exactly, and why you need multiple instances of it.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • 9
    This is not a place for an incompetent no-global variables rant. The concrete question is whether multiple calls to LoadLibrary also load multiple copies of the resources in the DLL - or only one copy. – Jo So Nov 23 '17 at 19:57