loading a dll with classes

This is a discussion on loading a dll with classes within the C++ Programming forums, part of the General Programming Boards category; Hi, ive been programming c++ (up from C) for two days now, and i want to use a class in ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    loading a dll with classes

    Hi, ive been programming c++ (up from C) for two days now, and i want to use a class in a dll. Right now im just using the skeleton dll provided by dev-cpp with an add function in there, so i hope i havent stuffed up what i did change.

    Code:
    /* Replace "dll.h" with the name of your header */
    #include "dll.h"
    #include <windows.h>
    
    DllClass::DllClass()
    {
    
    }
    
    int DllClass::add(int a, int b)
    {
       return(a + b);
    }
    
    DllClass::~DllClass ()
    {
    
    }
    
    
    BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                           DWORD reason        /* Reason this function is being called. */ ,
                           LPVOID reserved     /* Not used. */ )
    {
        switch (reason)
        {
          case DLL_PROCESS_ATTACH:
            break;
    
          case DLL_PROCESS_DETACH:
            break;
    
          case DLL_THREAD_ATTACH:
            break;
    
          case DLL_THREAD_DETACH:
            break;
        }
    
        /* Returns TRUE on success, FALSE on failure */
        return TRUE;
    }
    and then my executable code

    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef int (*AddFunc)(int,int);
    
    
    using namespace std;
    int main(int argc, char *argv[])
    {
       AddFunc _AddFunc;
       HINSTANCE hInstLibrary = LoadLibrary("dllLoadingTestdll.dll");
    
       if (hInstLibrary == NULL)
       {
          printf("o o");
          FreeLibrary(hInstLibrary);
       }
    
       _AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "DllClass::add");
    
       if ((_AddFunc == NULL))
       {
          printf("O O");
          FreeLibrary(hInstLibrary);
       }
       
       
       //printf("%d\n", _AddFunc(23, 43));
       getchar();
    
       FreeLibrary(hInstLibrary);
    
       return(0);
    }
    which i also copied for the most part off the net, so i hope i didnt stuff this up either.

    anyway, what i want to know is how to get the function address for my add function.
    _AddFunc == NULL is returning true, and its ouputting "O O" so thats where the problem is.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    This should probably be in the Windows programming forum . . .

    I don't know any Windows programming, but GetProcAddress() seems to return a MYPROC. http://msdn2.microsoft.com/en-us/library/ms686944.aspx
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    When compiling class member function - its name is transforemed, so it cannot be requested with your original name...

    try dumpbin /exports or some dll explorer to see the real names...

    More over - class members have as a hidden parameter the pointer to this, so caling them by pointer is a lot more tricky than the regular functions...

    One of the common practices though is to write some C-style wrapper that will be exported from the dll with from the dll
    etern "C" type
    inside the wrapper the poionter will be converted to pointer to DllClass and the correct member function will be called
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a DLL that is not in the same directory as the executable
    By starcatcher in forum Windows Programming
    Replies: 10
    Last Post: 12-13-2008, 06:05 AM
  2. Explicit DLL loading
    By true_organs in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2008, 01:26 PM
  3. Loading a DLL at runtime
    By filler_bunny in forum Windows Programming
    Replies: 9
    Last Post: 02-23-2003, 07:03 AM
  4. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

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