Mohammad Karmi
Mohammad Karmi

Reputation: 1445

Export :DLL linking using c++

I know how to use explicit linking for a dll that doesn't contain class but how can link a dll contains class, example:

class math{
public:
int sum(int,int);
};

to load it:

    typedef int(*func)(int,int);
    int main{
    HINSTANCE hDLL;               // Handle to DLL
    hDLL = LoadLibrary("math.dll");
        func add=(func)GetProcAddress(hDLL, "sum");
    add(4,5);
 return 0;
    }

if i do this it stop working and exit the program , if i remove the class it works

Upvotes: 2

Views: 123

Answers (2)

Captain Obvlious
Captain Obvlious

Reputation: 20063

C++ compilers use name mangling to uniquely distinguish identifiers in your program. This mangling causes the name to be significantly different than the plain identifiers you use in your program. Because of this using GetProcAddress is typically impractical for accessing code written in C++ that resides in a DLL. Instead I recommend using __declspec(dllexport) and __declspec(dllimport) to provide painless access to the code residing in your DLL.

In your DLL's project you will want to add a preprocessor definition with a name such as "EXPORT_CLASSES" or one unique to the DLL. This will be used by your DLL and program to determine if a particular declaration should be exported by the DLL or imported by the program.

dllstuff.h

#ifdef EXPORT_CLASSES
#define IMPORT_EXPORT __declspec(dllexport)
#else
#define IMPORT_EXPORT __declspec(dllimport)
#endif

You will then change the class declaration for math to use this. When the DLL is compiled IMPORT_EXPORT will be equal to __declspec(dllexport) and will instruct the compiler and linker that the definitions for this class should be made publicly available (ie. through the DLL's export table).

mathclass.h

#include "dllstuff.h"

class IMPORT_EXPORT math
{
public:
    int sum(int, int);
};

Now all you have to do in your main application is include the mathclass.h any time you want to use the math class. You can now instantiate an instance of math and access it's member functions.

#include "mathclass.h"
int main()
{
    math m;
    int result = m.sum(1, 2);
}

This of course is just a basic description of the process. There are plenty of articles floating around the web (including SO) with provide much more detailed information.

Upvotes: 1

Balog Pal
Balog Pal

Reputation: 17163

GetProcAddress can load up exported symbols form the DLL, provided you called it with the correct name.

In your case the function is not exported in the first place. And if it was, it is surely not called "sum" but some gibberish with 20-40 characters.

For using DLLs with C++ code you'll want to drop the GetProcAddress way entirely, and rely only on the implib that maps the names fine.

For that you add __declspec(dllexport) to the class (preferably with dllimport at the client), and add your DLL project as project reference. Alternatively add the .lib that was created along with the DLL to the client project.

Upvotes: 2

Related Questions