Put the definition of the class in a header file and include it in whatever you want to share the class with. If you want an instance of the class (an object), you can put that in a DLL and share it.
Printable View
Put the definition of the class in a header file and include it in whatever you want to share the class with. If you want an instance of the class (an object), you can put that in a DLL and share it.
Yes, with vc++ you best create a sample win32 dll that exports a few symbols and see how it's done.
With others I don't know how, but it's surely possible.
All the buzzt
CornedBee
Vlatko: You mean just include it like any normal file and compile it into the program?
Umm do you mean me?
Anyway, yeah just include the header file in whatever C++ project with which you want to access the DLL containing the shared object.
Yeah, sorry, for some reason I thought you were him. :o
Anyway, I don't know what you mean. I know how to include the file, link it, etc., but how would you set it up to export the class?
What do you mean 'export the class' ? You can put an instance of a class in a DLL and share it with your exe code. You don't get the class definition from the DLL.
I think if you made the class COM-compatible you could probably register it with Windows and let Windows deal with it.
Use dllimport for the import header file, and dllexport when creating the DLL. You'd need to link with the created .lib file when you make the DLL.Code:class __declspec(dllexport) MyClass {
// Contents here
};
Oh, and I think you need to use the same version of VC++ for the DLL and the using program.
What would you put in the header file if you're exporting the class definition from the DLL?
__declspec(dllexport) as I said earlier ;)
Ok, the DLL compiles fine, but I get a link error when I try to write a simple program using the class. I attached my source so you can see where it went wrong.
Quote:
error LNK2001: unresolved external symbol "__declspec(dllimport) class MyClass inst" (__imp_?inst@@3VMyClass@@A)
You need two copies of the header. One to create the DLL, and one to use it. I use a preprocessor directive to decide what to use, that way I can get by with the same header. A specific symbol is defined in project settings for the DLL project.
What preprocessor directve would that be?
Well, something like this:Then in project settings add _IKDLL_INT to the list of predefined symbols. Pretty simple really :rolleyes:Code:#ifdef _IKDLL_INT
#define IKDLL __declspec(dllexport)
#else
#define IKDLL __declspec(dllimport)
#endif
class IKDLL MyClass {
// blah
};
error C2071: 'inst' : illegal storage class
note that i set the library to single threaded, because I forgot to install the multithread CRT dlls
All the buzzt
CornedBee
forgot again
Ah right that code was meant for the headers not the DLL.....
Hang on a minute... that's what I was suggesting in the first place, minus the __declspec bit. What's that for that just including the class definition wouldn't do?
It tells the linker not to search for the class in a normal library or module, but rather the import library. (I think)
Also, I'm not sure if I posted the right files before. Tell me if not.
Don't you still give the class definition in the header though? Or do you only define the interface and leave the implementation in the library?
The implementation is in the library.
Gotcha. Thanks :)