Classes in DLL's?

This is a discussion on Classes in DLL's? within the Windows Programming forums, part of the Platform Specific Boards category; I've just created a reasonably large DLL using classes, but have just realised I can't (or don't know how to ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Classes in DLL's?

    I've just created a reasonably large DLL using classes, but have just realised I can't (or don't know how to ) load the member functions using GetProcAddress() in the program to use the DLL.

    I'll try and explain better like this:

    Code:
    // the DLL
    class MyClass
    {
    public:
        MyClass () {}
        ~MyClass () {}
    
        bool    Func (int, int);
    };
    
    // the DLL class def.
    bool MyClass::Func (int a, int b)
    {
        (a > b) ? return true : return false;
    }
    
    // the Other Program
    typedef (*DLL_Func) (int, int);
    
    int main ()
    {
        DLL_Func    Func;
        HMODULE    hLib = LoadLibrary ("thedll.dll");
    
        // omitted error checking...
    
        Func = (DLL_Func) GetProcAddress (hLib, "Func");
        // more error checking and rest of program      
    }
    Now I've read over the function pointer tutorial site but it doesn't really help because the DLL and other program are separate projects so if I declare a function pointer like this:

    Code:
    typedef (MyClass::*DLL_Func) (int, int);
    I get linker errors when it tried to find MyClass::

    This is a pretty deep problem compared to what I'm usually faced with and could really use some guru-esque advice
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    To use GetProcAddress like this you will need to use the mangled function name as the second parameter - how it's mangled is compiler specific. For example, if you're using an ms compiler use dumpbin on the dll to get the mangled name of the exported function(s) or check the map file if you use the /map linker option to generate the dll.

    Further reading:

    The Old New Thing: Why can't I GetProcAddress a function I dllexport'ed?
    Explicitly linking to classses in dll's
    port code that uses GetProcAddress from C to C++
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks for the links they'll really help a lot
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ok I've followed the Codeguru link and I'm pretty certain I understand it. However I'm now getting compile errors. Here's the client program - are there any obvious mistakes?

    Code:
    #include <windows.h>
    #include ".\main.h"
    
    typedef PVWindow* (*DLL_CreatePVWindowA_t) ();
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
            HMODULE                 hPVWMod = LoadLibrary ("PV.dll");
            DLL_CreatePVWindowA_t   DLL_CreatePVWindowA = 0;
            PVWindow                *PVWnd = 0;
    
            if (hPVWMod == NULL)
            {
                    MessageBox (0, "Unable to load library [PV.dll]", "ERROR", MB_OK | MB_ICONERROR);
                    return 1;
            }
    
            DLL_CreatePVWindowA = (DLL_CreatePVWindowA_t) GetProcAddress (hPVWMod, TEXT("CreatePVWindowA"));
    
            if (DLL_CreatePVWindowA == NULL)
            {
                    MessageBox (0, "Unable to load CreatePVWindowA", "ERROR", MB_OK | MB_ICONERROR);
                    return 1;
            }
    
            
            
            delete PVWnd;
            return 0;
    }
    Here's the errors I get:

    Code:
    ------ Build started: Project: TestGame, Configuration: Debug Win32 ------
    
    Compiling...
    Main.cpp
    x:\PV1\TestGame\Main.cpp(10) : error C2143: syntax error : missing ';' before '*'
    x:\PV1\TestGame\Main.cpp(10) : error C2501: 'DLL_CreatePVWindowA_t' : missing storage-class or type specifiers
    x:\PV1\TestGame\Main.cpp(15) : error C2146: syntax error : missing ';' before identifier 'DLL_CreatePVWindowA'
    x:\PV1\TestGame\Main.cpp(15) : error C2065: 'DLL_CreatePVWindowA' : undeclared identifier
    x:\PV1\TestGame\Main.cpp(24) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
    x:\PV1\TestGame\Main.cpp(24) : error C3861: 'DLL_CreatePVWindowA': identifier not found, even with argument-dependent lookup
    x:\PV1\TestGame\Main.cpp(26) : error C3861: 'DLL_CreatePVWindowA': identifier not found, even with argument-dependent lookup
    
    Build log was saved at "file://x:\Pv1\TestGame\Debug\BuildLog.htm"
    TestGame - 7 error(s), 0 warning(s)
    I suppose I'd better post the class declaration too:

    Code:
    class PVWindow
    {
    public:
            PVWindow (void);
            ~PVWindow (void);
    
            virtual bool    Initialise ();
            virtual bool    NewWindow (WNDPROC wndProc, const char *cCaption, int iWidth, 
                            int iHeight, int iBits, bool bFullScreen);
            virtual void    Shutdown ();
    
    private:
            // Functions
            void    ResizeScene (int iWidth, int iHeight);
    
            // Variables
            HGLRC           m_hRC;          // Rendering context
            HDC             m_hDC;          // GDI Device Context
            HWND            m_hWnd;         // Window handle
            HINSTANCE       m_hInstance;
            char            *m_cClassName;
    
            bool    m_bInitialised;
            bool    m_bFullScreen;
    };
    Sorry to dump all this code on whomever is reading this, but I really need some help understanding this.
    Last edited by cboard_member; 12-17-2005 at 03:25 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. standart dlls
    By keeper in forum C++ Programming
    Replies: 3
    Last Post: 07-05-2006, 08:32 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21