   class function dll help
  Hey guys,can I compile a class function into a .dll without losing its recognition of other class members, i.e.:class Window{   int xSize;public:    void InitWindow(int x);    int getSize();};Window::InitWindow(int x){    xSize = x;}Window::getSize(){    return xSize;}Can I export InitWindow and Resize as .dll functions to import and retain the value of xSize?So from another file, as a standard function:InitWindow(10);    // importedint n = getSize();    // importedDo I have to include the header of the class? I'm sorry if I sound a bit confusing, I'm just beginning learning about dll's and am working on a game engine where I can compile the graphics code into a dll and import it into a game project that uses the graphics engine and simply call the funcs without having to include the actual graphics code, kind of like the quake mod code (you can edit the game without seeing the raw source).Thanks guys for any help!-stealthgate   
  You can't export a C++ class member as a regular C function - they are not the same at all. The C function is rather context free, while the C++ method will wait for a special init (namely, init for the implicit this pointer). But you can export a class as a whole - using the same syntax you used to export a function:#ifdef EXPORT_SYMBOLS#define MY_DLL_EXPORT __declspec(dllexport)#else #define MY_DLL_EXPORT __declspec(dllimport)#endifclass MY_DLL_EXPORT MyClass { ... };You should define EXPORT_SYMBOLS in the project settings of your DLL. EXPORT_SYMBOLS is not defined for those who want to use the DLL. In a client program, you can them define/use instances of MyClass as if it was defined in the project itself.HTH,   ;  That's great that you can export a class as well; that also exports all of its member functions? That's perfect!Do I still have to produce a .def file? And what would it's syntax be? Same as:EXPORTS    MyClass 1??Also, how would I import this dll in the client prog, say the game code that uses the functions from the dll?thanks again!EDIT:Quote:Original post by Emmanuel DelogetYou should define EXPORT_SYMBOLS in the project settings of your DLL. EXPORT_SYMBOLS is not defined for those who want to use the DLL. In a client program, you can them define/use instances of MyClass as if it was defined in the project itself.Does this mean I can put #define EXPORT_SYMBOLS in the source file of a project that wants to use the dll without calling LoadLibrary().... or something like it?   ;Quote:Original post by stealthgateThat's great that you can export a class as well; that also exports all of its member functions? That's perfect!Do I still have to produce a .def file? And what would it's syntax be? Same as:EXPORTS    MyClass 1??Nope. The __declspec stuff is the same as using an exports.def (Sort of, an exports.def exports the names unmangled).Quote:Original post by stealthgateAlso, how would I import this dll in the client prog, say the game code that uses the functions from the dll?thanks again!You link the client program to the .lib that the compiler generates. That causes the DLL to be loaded into the client program when it starts up. Then you should be able to create the class just as if it was implemented in the client prog. Although you'll still need to include the classes header file.Quote:Original post by stealthgateDoes this mean I can put #define EXPORT_SYMBOLS in the source file of a project that wants to use the dll without calling LoadLibrary().... or something like it?You only define EXPORT_SYMBOLS when you're building the DLL. That causes the compiler to export anything flagged as MY_DLL_EXPORT from the DLL - such as your class, and allows the client program to use them. In a client progtam, EXPORT_SYMBOLS won't be defined, so the symbols will be imported from the DLL.You can't use LoadLibrary with classes. Well, LoadLibrary will work, but GetProcAddress won't. That's the problem with exporting classes. If you want to do runtime linking, then you'll need to export a function that creates an interface to the class, and derive the real class from the interface. I'll go into this in more detail if you need runtime linking.   ;  @Evel Steve:I'm interested in runtime loading of classes from dll, so could you explain more detailed? A way I've already seen works like this://I declare the class that is to be imported in myclass.h:__declspec(dllimport) class myclass{    public:   void func1();   void func2();};// Then I declare a C-Interface for the class methods in interface_exp.h:__declspec(dllexport) void func1();__declspec(dllexport) void func2();// I implement the functions of the C-Interface in source file interface.cpp, // in that I create a global instance of myclass: #include "myclass.h"#include "interface_exp.h"static myclass g_instance;__declspec(dllexport) void func1(){    g_instance.func1();}__declspec(dllexport) void func2(){    g_instance.func2();}// from interface.cpp, I compile a dll interface.dll, that can be loaded by // my application at runtime using LoadLibrary() and GetProcAddress() to access// the C-interface functions func1() and func2()// therefore, I create a header interface_imp.h:__declspec(dllimport) void func1();__declspec(dllimport) void func2();// in my main programm, I include interface_imp.h:#include interface_imp.htypedef void (FUNC1) ();HINSTANCE hDLL = LoadLibrary("interface.dll");FUNC1 *pfunc1() = (FUNC1*) GetProcAdress(hDLL, "func1);When loading interface.dll at runtime, the dll containing implementation of myclass is loaded automatically. Maybe global automatic instantiation of myclass in interface.cpp does not work when interface.dll is loaded at run-time (?), so one could introduce a function init_class() to C-Interface that instantiates myclass dynamically.Is your concept of exporting a function that creates an interface to the class and deriving the real class from the interface different from that? Or is that what you meant?   