Hi guys I got a generic question (looking for a generic answer):

I have to load a DLL into C++, I've been playing around all day with it trying to access the methods (I have a big manual for the DLL), so far I have tried the next:
1.-
#import "path\NameofDLL.dll"
using namespace NameofDLLlib;

2.-
turning the DLL to Lib (with DLL to Lib) and just calling a #include "NameofDLL.h" and adding the "NameofDLL.lib" to the linker paths

Now I am doing a lot of things wrong but I got no one to go here except lots of internet info.
The DLL has a class, when I declare the class in my program I just do:
ClassName Class1; and then start calling the methods with Class1.method(); but that is not working

So my questions are:
What method do you recommend me to import he DLL?
How do I call the methods of the DLL class?

I tried googling my questions, but I have the excess information syndrome right now

Oh and right now I'm trying to use AfxLoadLibrary(), Its going to take me a while since i'm new to MFCs

I'm assuming you have access to the source code of the DLL, otherwise you would not be attempting to use class methods that are exported from it (there are reasons why this would be impossible). I don't know where you got your info, but the methods you tested are all very wacky and definitely wrong. I will try to put a few options that are typical:

1. Compile your DLL code directly into a .lib file (not just changing the extension nor using a third party software to convert). You will need to select as build option something like "static library". Then all you need is to include the header in your main program and specify this static library in the command line. Note that this option will make all the code that you use from this library part of the final executable (i.e. bigger exe file, but no need to distribute the DLL along with the executable).

2. If you want to export classes from the DLL, you have to use the clause "__declspec(dllexport)" between the keyword "class" and the class' name. In the header file that imports the class, you use the keyword "__declspec(dllimport)". Basically, this should be done with a #define:

//in the myDLLClass.h
#ifdef MY_DLL_MODULE
#define MY_CLASS_API __declspec(dllexport)
#else
#define MY_CLASS_API __declspec(dllimport)
#endif

class MY_CLASS_API myDLLClassName {
  //.. everything will be exported or imported
};

Simply defining MY_DLL_MODULE when compiling the dll will export the class and any other use of the header file in another program will import the class. But this is not all. When you compile the DLL, you should be getting a static link library file as well (this is the .lib file that is required to statically link to the dll). If that file is not getting created, there is something wrong with your compilation process or settings (you might need a module definition file too). With that, the process is the same as for case 1, you just include the header and provide the .lib to the compiler (to the linker actually) via the command line or build options.

3. If you don't have access to the source code of the DLL and have "lost" the static link library, then you will need to link dynamically to the library, using LoadLibrary(), GetProcAddress(), and FreeLibrary(). You can look at MSDN for the documentation. As far as I know, this will not allow you to import classes or any class data member or method, it only allows C-functions (usually with the stdcall calling convention). This is usually quite straight forward. You declare global or class scope function pointers (C-style), call LoadLibrary to get a Handle to the dll, then you call GetProcAddress to give an address for each function pointer you need, and when you are done using the functions you can use FreeLibrary to unload the DLL (after that you can no longer execute any of the function pointers you have, they are no longer valid).

commented: Excellent insight and recommendations +1

1. For converting to a .lib file I used a thid party program (sorry) because I have a manual for the DLL ("programming guide") and in it are listed classes, methods and properties.
2. I have only been reading for one day about this and I am pretty new to C++ so this method is a bit too hard for me "time wise"

3. I started reading about LoadLibrary Function, and will get more into and respond later on when I have more info about it.

Thanks man, I'll keep you posted

Ok, this is what I've done so far:

I used the loadlibrary approach, the first thing I want to do is get a message saying "DLL Loaded"

The code compiles, but the DLL was not loaded, so I added the path of the DLL to my reference (i copied the dll to my folder, i removed it after adding the path)

Still, no load
So I started a Windows Form Application to use the "Add New Reference" since the reference is in the COM libraries

Does it affect in some way that is an ActiveX reference?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.