Load the same dll multiple times [closed]

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.