Dynamically loading DLL twice from same application

Posted on 2003-03-13
Medium Priority
Last Modified: 2013-11-20

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.

Question by:talsp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

gelbert earned 200 total points
ID: 8130017
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()
LVL 12

Expert Comment

ID: 8131966

  I was wondering why you might want to do this?

Author Comment

ID: 8134504
i have a dll that it's functions and classes need to have different values for different sessions from same application
LVL 12

Expert Comment

ID: 8138301

  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.


Expert Comment

ID: 25907762
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question