Klasse aus Dll



  • Kann ich eine Klasse aus einer Dll dynamisch laden?
    Ich meine ohne Importbibliothek.
    Wenn nein, könnte mir dann bitte jemand erklären wie ich so eine Importbibliothek erstelle?



  • klar kannst du
    header:

    class IMyClass
    {
    public:
       virtual void Release() = 0;
       virtual void DoIt() = 0;
    }
    
    typedef IMyClass* (*MyClassFactoryPtr)(void); 
    #define MyClassFactoryName TEXT("CreateMyClass")
    

    in deiner dll:

    class CMyClassImpl : public IMyClass
    {
    public:
       virtual void Release() { delete this; }
       virtual void DoIt()
       {
         ...
       }
    }
    
    __declspec( dllexport ) IMyClass* CreateMyClass()
    {
       return new CMyClassImpl;
    }
    

    in deiner exe

    int main()
    {
       HINSTANCE hDll = LoadLibaray("DeineDll.dll");
       if(hDll==NULL)
         return 1;
       MyClassFactoryPtr creator= (MyClassCreatorPtr)GetProcAddress(hDll, MyClassFactoryName); 
       if(creator==NULL)
         retrun 1;
    
       IMyClass *myClass = (creator)();
       myClass->DoIt();
       myClass->Release();
       return 0;
    }
    

Anmelden zum Antworten