Jonathan
Jonathan

Reputation: 535

Extracting from a Dll in c++

I have a DLL given named Lib.ddl. In the dll i have a function named add that takes an integer as a parameter.

Using the windows API and by using the following class.

class WindoswAPI
{
    public:
         WindowsAPI();//constructor 
         //helper functions
         ~WindowsAPI();
    private:
}

How do I load this library in the constructor of the class. Extract the function via helper functions, and unload the function in the destructor? I have looked on the internet for solutions and help but i cant find any.

Upvotes: 1

Views: 981

Answers (2)

Felice Pollano
Felice Pollano

Reputation: 33272

You should first look at LoadLibraryEx() in order to load the dll in your process, then you can use GetProcAddress() to obtain a pointer to a function by name. Note the HMODULE parameter requested from the second function is returned by the first. You have to carefully know the function signature to invoke it without causing GPFs, so don't be surprised if you have to do some debug before having it working. First easy thing to check anyway are the return values of both functions: the first one should return something different from zero ( it actually returns the virtual address where the dll is loaded ), if it returns NULL, use GetLastError to have some hints. The same for GetProcAddress, if it return zero something didn't work, and usually is the incorrect spelling of the function name. As I said before having back an address from GetProcAddress does not guarantee you have finished: you must know perfectly how to call the function. If you need help in discovering which name are exposed from the dll, you will find useful DUMPBIN.EXE, you should have it already available from the Visual Studio Tools command prompt. When your code finish with the dll, you can try to unload it by using FreeLibrary().

Upvotes: 1

Eugene Mamin
Eugene Mamin

Reputation: 749

Look at POCO C++ Libraries, it contains very nice crossplatfrom DLL-Loader to avoid hand-written workarounds and boilerplate.

Upvotes: 1

Related Questions