Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-18-2005, 08:18 AM   #1 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
Dynamicly load DLL classes

In all my years of coding i never dynamicly loaded classes from a DLL.
However the last weeks i've discovered this is a "must have" in certain circumstances.

For example i wrote a MP3 player with a flat database system, but somehow my users pushed it over the limit by adding 100.000+ music files in the database which made it very slow. So i needed to figure out a way how to control a database in a server/client behavior in many ways like SQLite, MySQL, Postgre and flatfile.
Now there's an issue because each database engine has his own function calls but your program only wants to load one of them but don't want the overhead of everything in your system.

The solution is DLL's and SO's with functions. But functions are limited and don't allow multiple instances easily unless you identify each so i've decided to use classes in libraries.

But how do you export them ?

Actualy i've found a good examples on the internet which made my task complete. The example was called 'expclass' but i totally forgot where i found it so credits to author will be attached when i find it again

But i have an problem, i can't add attachments in here so could i have permission or something ?
DJMaze is offline   Reply With Quote
Old 09-22-2005, 12:30 PM   #2 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
What is your actual question?

Dynamically loading C++ classes isn't really something you want to do directly, due to "name-mangling", among other things. However, there is another way.

What you generally do is to create functions that create and delete an instance of your class.

Suppose you've got a class "MyClass"
Code:
extern "C" MyClass *create(int some_parm)
{
    return new MyClass(some_parm);
}

extern "C" void destroy(MyClass *p)
{
    delete p;
}
The "destroy" function is needed because you can't guarantee that the implementation of new/delete in the application and the plugin are compatible.

Of course when writing plugins, you usually derive from a common base class for all your plugins, so you'd get something more like this:
Code:
extern "C" MyBaseClass *create(int some_parm)
{
    return new MyClass(some_parm);
}

extern "C" void destroy(MyBaseClass *p)
{
    delete p;
}
Most of the time people wrap all that into a macro, so all you'd have to do in the implementation of your plugin would be something like:
Code:
EXPORT_PLUGIN(MyClass)
http://www.tldp.org/HOWTO/C++-dlopen/ has some more information on the subject.
Locutus is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can not load php_ldap.dll sde PHP 2 03-28-2006 03:25 PM
Classes started with C++ classes and now I'm lost...HELP cleverest Standard C, C++ 15 04-24-2005 04:29 PM
classes and servlets .. oh my sde Java 3 04-27-2004 08:00 AM
DLLs and Classes moremonks Standard C, C++ 6 07-13-2003 12:27 AM


All times are GMT -8. The time now is 11:43 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting