How To Export A class From Dll

May 26, 2010 at 8:34am
Hello All

i can export a class from dll with implicit linking,

however i face a problem when i want to do explicit linking

(using LoadLibrary function), with LoadLibrary i can call only functions in

dll (defined in *.Def) . is there any way by which i can explicetly link to dll

and create a exported class object in exe. i am using vc6.

thanks in advance.
May 26, 2010 at 9:16am
May 26, 2010 at 9:16am
Use Depends to look at the DLL. You can see all the exported symbols.

I don't think you have a problem exporting your class if you can link to if implicitly. The problem is in naming. The exported class name and methods will be mangled. There is no standard name mangling scheme.
May 26, 2010 at 11:41am

i face problem when i do following

class_name * ptr = (class_name *)LoadLibrary(hmod , "class_name");

(this works well with functions & other variables )

(i have class declared in header file & i use extern "C" to stop name mangling .)

& when i try to call any function , i got linking error as linker will not get function defination

am i going right ?

or i am doing some basic mistake ?


May 26, 2010 at 12:12pm
Again, use Depends to look at the DLL. You can see all the exported symbols.

Once you see what you've produced, you'll get a better handle on the problem.

You can't stop name mangling on a class, as a class isn't a C concept to begin with.
May 27, 2010 at 4:04am
hello kbw ,

thanks for showing interesr & helping me .

when i use depends it shows me all function & class name as exported but

when i try to use following code i get linking error

HMODULE h = LoadLibrary(" path \\ dll_name.dll ");

class_name * ptr = (class_name *)GetProcAddress(h,"class_name");

ptr->some_function(); //public function in class

here i get linking error (as some_function defination might not visible to linker)

am i going right ? if i am doing some mistake , is there any way to get object of class in dll

& call it's public functions ?

thanks.




May 27, 2010 at 8:13am
When viewing the symbol names in Depends, you need to see the "undecorated" C++ names. Depends can show you decorated name, i.e. names you'd recognise; and undecorated names, the names as they really exist.
May 27, 2010 at 11:09am
??0CDllx@@QAE@XZ
??4CDllx@@QAEAAV0@ABV0@@Z
?fnDllx@@YAHXZ
?my_fun2@CDllx@@QAEXXZ
?my_fun@CDllx@@QAEPAV1@XZ
?nDllx@@3HA

this is visible to me in depends as function coloum , is it undecorated name ? if not how can i get it ?
May 27, 2010 at 11:43am
Those are the real undecorated (mangled) names.

You should be aware that there is no standard for mangling names, so it's unwise to try to use them yourself. If you can get the tools to use them automatically, fine; but you shouldn't be trying to use these names yourself.
May 28, 2010 at 3:53am
thanks kbw ,

is there any other way to create a object of a class which is inside dll , in exe. ?

i am also trying to create a function which will return object but same linking problems

occours.
May 28, 2010 at 8:12am
One way around the problem is to use a C interface. It's easy to think of it as resorting to the lowest common denominator, a sort of race to the bottom, but it allows your DLL to be used from any language available on Windows. Remember C++ was designed for library creation and Windows' native interface is C.

One way is to create open/close functions that create/destroy the object. The handle that's passed around can be the this pointer reinterpreted as an int.

e.g.
1
2
3
int mything_open(const char* param1, int param2);
void mything_dowork(int handle);
void mything_close(int handle);


Implemented as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
extern "C"
int mything_open(const char* param1, int param2)
{
    if (MyThing* p = new MyThing(param1, param2))
        return reinterpret_cast<int>(p);

    return 0;
}

extern "C"
void mything_dowork(int handle)
{
    if (MyThing* p = reinterpret_cast<MyThing*>(handle))
        p->DoWork();
}

extern "C"
void mything_close(int handle)
{
    if (MyThing* p = reinterpret_cast<MyThing*>(handle))
        delete p;
}

Last edited on May 28, 2010 at 8:35am
May 28, 2010 at 9:11am
sorry kbw , i tried above ( use of reinterpret_cast ) but linker error exists .

i think implicit linking might be the only possible way .
May 28, 2010 at 10:00am
That code goes in your DLL. The ideas is that it becomes the new and only interface. The C++ stuff isn't exported and the new C interface is.

It's just a suggestion that will work technically, although it may not be a suitable solution for you.
Last edited on May 28, 2010 at 2:59pm
May 31, 2010 at 10:40am
hello kbw,

while serching on google i got following link ,

http://www.codeguru.com/cpp/w-p/dll/importexportissues/article.php/c123

well , it is quite difficult for me to understand.

but i still trying above solution.

hope it will help.
Jun 1, 2010 at 7:13am
Interesting, I wouldn't have thought about remapping function names in the .def file. Thanks for posting the link.
Last edited on Jun 1, 2010 at 8:50am
Jun 2, 2010 at 7:58am

i think this will work for me

thanks kbw , thanks to all.
Topic archived. No new replies allowed.