Click to See Complete Forum and Search --> : Use class from DLL?


Magnus Ohlin
May 11th, 1999, 08:49 AM
Hiya!

I want in my main program use a class that is in a DLL. Is that possible?
What kind of dll should my class be and how do I use it from the main program?
I would like to do something like this.
myClassFromDll myClass;
myClass.SomeMethod();

Thanks
Magnus

BrianOG
May 11th, 1999, 10:29 AM
If you are using an MFC entension DLl declare your class as follows:class AFX_EXT_CLASS CMyClassInDll
{
....
};

If a normal dll use:class _declspec(dllexport) CMyClassInDll
{
....
};

Andrew
May 11th, 1999, 10:32 AM
Essentially you need to declare your class with the __declspec( dllexport ) modifier.

This is what I do:

//header file for class

#ifdef MYCLASS_DLL_BUILD
#define EXPORTED_DLL __declspec( dllexport )
#else
#define EXPORTED_DLL
#endif

class EXPORTED_DLL MyClass
{
//class declaration etc
};




I put the header file in a central location. I make sure that MYCLASS_DLL_BUILD is defined in the project where I build the .dll file and make sure that is not defined in the project where I actually use an insance of the class.

Hope this helps

Andrew

Magnus Ohlin
May 12th, 1999, 02:32 AM
Okej thanks!

But how do I use this class from my main program? Shall I link the obj file?

/Magnus

BrianOG
May 12th, 1999, 02:57 AM
You will need to include the header file for the class and also link your application with the .lib file created when you compiled your dll. (See VC help on linking to lib's)