• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2424
  • Last Modified:

Dynamically loading DLL twice from same application


i have a dll that i load it dynamically using LoadLibrary.

is there a way i can use to load it twice from the same application?
when i am doing loadlibrary twice from 2 different classes in the same application i receive the same instance/module.

i know i can re-copy the dll to another name and load it by 2 different names but i would like to know how can i load it with the same name and use it with different values.

1 Solution
No you can not load the same dll multiple times into the same application. Each additional call to LoadLibrary() will just increase reference counter on dll handle.
You may want to provide initialization function for you dll which takes parameter and user would call it after call to LoadLibrary()

  I was wondering why you might want to do this?
talspAuthor Commented:
i have a dll that it's functions and classes need to have different values for different sessions from same application

  Well you could return a new version of the class to each caller in the session...

  Say you have the class CMyClass in the DLL. Create a function to return a new version of your class.

  CMyClass* EXPORT GetClass ()
     return new CMyCLass;
  In your Application

  FARPROC pNewClass ( VOID );
  pNewClass = GetProcAddress ( hLib, ... "GetClass" ... );

  Not sure of GetProcAddress syntax

  CMyClass* pMyClass1 = pNewClass ();

  CMyClass* pMyClass2 = pNewClass ();

  Now you have two seperate classes then can have different values.  

  Dont forget the client has to delete the classes.

I have exactly the same proble, Each DLL is a "module" that is loaded into the main application, a bit like LabView's elements. The same module can of course be instantiated several times.

When i have two times the same module instantiated, all calls that i do on it go to the last instantiated one, it doesn't matter if i call it on the first one or on the last one.

The whole application was developed this way, a bit late to change so i definitely need to keep on using the modules in that way. How can i tell the application which instance of the same DLL should receive the data/command?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now