Click to See Complete Forum and Search --> : How do I export a class?


Magnus Ohlin
April 13th, 1999, 07:28 AM
Hiya!

I have created a MFC extension dll and in that dll I would like to have a class that I would like to export to my main program.
Can I just send in an reference and assign it to my class in the dll and then use it in my main program, of course delete it as well in the main program.

How do I load my dll in the main program?

TIA
Magnus

Franky Braem
April 13th, 1999, 08:45 AM
To export your class use the following :

#define DllExport __declspec( dllexport )

class DllExport CMyClass
{
};

Now CMyClass can be used by other applications when you link your application with the generated .lib file of your dll.

Karl
April 13th, 1999, 08:57 AM
a way to do this is to use dllexport / dllimport.
for example:
class __declspec(dllimport) CMyEdit: public CEdit{...} //used in your main program
class __declspec(dllexport) CMyEdit: public CEdit{...} // used in your DLL.

Don't forget to add the lib file of your DLL in the setting of your main project.

dllimport / dllexport "loads" and "unloads" the DLL implicitly in your main prog., so you don't have to care about this.

you may find more infos in the docs.

HTH.

K.



Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.

Gomez Addams
April 13th, 1999, 12:04 PM
To expand a little on what Karl wrote, I often do this in DLLs.

#define DllExport __declspec(dllexport)
#define DllImport __declspec(dllimport)

#ifdef MY_LIBRARY_CODE // define this in project of DLL
#define DllClass DllExport
#else
#define DllClass DllImport
#endif

class DllClass CMyClass
{
...
};

When MY_LIBRARY_CODE is defined, which should only be in the library's
project, then DllClass is exported. All other times it is imported.
Also, check out post 1286 on this board. It was on page 44 for me.
You can then use the class just like any other class.

June 4th, 1999, 11:50 AM
I had the same question about Importing Classes from a DLL. Thanks!

Now my question is: Can this import be done explicitly (i.e. using GetProcAddress etc...) and not requiring the .LIB file?

Thanks,

Jeff

Gomez Addams
June 4th, 1999, 12:05 PM
Technically, yes but it is far from easy.
Each member of the class will usually be exported individually
and if you throw in name-mangling things get very difficult.
Try "dumpbin /exports SomeLibrary.dll" to see what I mean.
There have been several posts and a few KB articles on this
topic. They might be more encouraging than I am.

amitg90
March 28th, 2001, 02:28 PM
Hi,
I have a question.
what about exporting class from main program to DLL..
How should that work...

Thanks
amit

IndikaNiva
March 28th, 2001, 03:08 PM
extern "C" AFX_EXT_API void WINAPI YourDllName();


class AFX_EXT_CLASS CYourClassName : public xxxx
{
}

Then in your main program, you include the .lib from the dll,
include the header for the class taht you are exporting and just use it as normal.

Rate this post if helped.

jbschumacher
March 28th, 2001, 04:44 PM
well as a *.dll.

#ifdef YOUR_EXE_BUILD
#define YOUR_EXPORT_API __declspec(dllexport) // works with *.exe's too
#else
#define YOUR_EXPORT_API __declspec(dllimport)
#endif // YOUR_EXE_BUILD

class YOUR_EXPORT_API CExample
{
// normal
};



And an import library will be made for your *.exe just like one for a *.dll. You can also export from an *.exe using a .def file, just like a *.dll as well.