15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


How do I use an C++ class with LoadLibrary

Carsten Schwartz -- csn@dhi.dk
Friday, March 07, 1997

Environment: VC++ 4.2-flat, NT 4.0 SP 2

How do you use a class from a dll with the load library call?
I want to load it as a class nad then be able to use all 
the functions without any further LoadLibrary related call.

Why? - Because i only want to load custom class DLLs when
I call their functions. I wnat to avoid an exaustive load
of all DLLs when the main part of a big DLL using app starts.

------------------------------------------------------
_\______/_ And they shall be kept
  \    /   in fear and awe....  
   \  /   
    \/     Clan Leader Smike
    /\     (Clan Envych, Nuln)

Carsten Schwartz
Engineer And Software Developer (DHI) Denmark
Email : csn@dhi.dk
www   : www.geocities.com/TimesSquare/Arcade/9022/
------------------------------------------------------





Norman C. Byers -- nbyers@intxxnet.com
Sunday, March 09, 1997

[Mini-digest: 2 responses]

This really isn't MFC related. My first suggestion would be to consider 
making your dll an Active/X control. Otherwise, to use LoadLibrary with a 
DLL containing classes your DLL must generally contain  'c' functions (use 
extern 'C' {  }) that will create and return a pointers to instances of the 
classes in the DLL, or return a pointer to a class static functions that 
will create one (gee - what does IMPLEMENT_DYNCREATE give you ?). The 
problem of doing having to do a GetProcAddress on a C++ mangled name is 
gone since you simply do a GetProcAddress for the new 'C' function and call 
it to get one of your objects.

Want to get even trickier ? Consider the case where you have several 
different report objects, all inheriting from an abstract base class called 
CReport. Write your main app so it only uses pointers to CReport objects, 
then create DLL's containing new classes derived from CReport objects. 
 Provide 2 'C' functions in each DLL, one to get the description for the 
contained report object, and another to create the CReport derived object, 
returning a pointer to the base class CReport. These 2 functions would be 
named the same in each DLL.  Now when your app loads, it can simply scan a 
directory for DLL files, see if the 2 report functions exist in each dll 
and if so call the description function and add the description to a list 
of available reports. When the user clicks on the report from the list, you 
re-load and address the appropriate DLL, get the CReport object and tell it 
to generate.  The neat thing is that a you add new reports, you need only 
send the user a new DLL - not a new app. This of course is also very handy 
if you want to customize each installation or charge for add-ins.

Of course, this type of late binding is partly what Ole is about. This is 
easier than generating ActiveX components, though I still prefer the 
ActiveX approach.

----------
From:  Carsten Schwartz[SMTP:csn@dhi.dk]
Sent:  Friday, March 07, 1997 1:06 AM
To:  mfc-l@netcom.com
Subject:  How do I use an C++ class with LoadLibrary

Environment: VC++ 4.2-flat, NT 4.0 SP 2

How do you use a class from a dll with the load library call?
I want to load it as a class nad then be able to use all
the functions without any further LoadLibrary related call.

Why? - Because i only want to load custom class DLLs when
I call their functions. I wnat to avoid an exaustive load
of all DLLs when the main part of a big DLL using app starts.

------------------------------------------------------
_\______/_ And they shall be kept
  \    /   in fear and awe....
   \  /
    \/     Clan Leader Smike
    /\     (Clan Envych, Nuln)

Carsten Schwartz
Engineer And Software Developer (DHI) Denmark
Email : csn@dhi.dk
www   : www.geocities.com/TimesSquare/Arcade/9022/
------------------------------------------------------



-----From: Mike Blaszczak 

At 07:06 3/7/97 +0000, Carsten Schwartz wrote:
>Environment: VC++ 4.2-flat, NT 4.0 SP 2

MFC 4.2-flat is incompatible with released versions of many
operating system components, and is able to only safely run
with beta versions of those components.  By using MFC 4.2-flat
with shipping versions of those components, you're just asking
for trouble. To avoid wasted time and suffering in the future,
please download the 4.2b patch from the Microsoft website and
install it.

>How do you use a class from a dll with the load library call?
>I want to load it as a class nad then be able to use all 
>the functions without any further LoadLibrary related call.

You can't.  LoadLibrary() just loads a DLL into your process.
You need to call GetProcAddress() on each and every function
you'd like to call within that library so you know where the code
for each function lives.  You also have to call FreeLibrary()
when you're done with the library.

If you relieve your constraint of "without any further
LoadLibrary-related call", you stand a chance: but you're still
looking at all sorts of tedium and pain.

>Why? - Because i only want to load custom class DLLs when
>I call their functions. I wnat to avoid an exaustive load
>of all DLLs when the main part of a big DLL using app starts.

MFC achieves this goal for some of the system DLLs that an MFC
application would otherwise be dependent upon.  You might try
looking at COMMIMPL.H and CCDATA.CPP in the MFC\SRC directory.
The DLL manipulated by MFC has a C interface, though, and not
a C++ interface, and that makes things considerably simpler.
And, unfortunately, this code still doesn't meet your requirement
of "without any further LoadLibrary related call".


.B ekiM
  Crotch Rocket   / Full-Body Rocket / http://www.nwlink.com/~mikeblas/
95 Honda VFR-750F /   94 Mazda RX-7  / Trip Report Central!
                 Less work, more hockey!
  These words are my own - I do not speak on behalf of Microsoft.




Become an MFC-L member | Вернуться в корень Архива |