views:

2142

answers:

6

I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++. The other is a simple c++ console application created from a blank project. I would like know how to call the functions in the DLL from the application.

Assume I am starting with a blank C++ project and that i want to call a function called "int IsolatedFunction(int someParam)"

Any references to this would be apprecated as well. I have been interested in dll import and export for some time and now have a change to learn more about it.

Thanks,

A: 

Presuming you're talking about dynamic runtime loading of DLLs, you're looking for LoadLibrary and GetProAddress. There's an example on MSDN.

Harper Shelby
+4  A: 

Can also export functions from dll and import from the exe, it is more tricky at first but in the end is much easier than calling LoadLibrary/GetProcAddress http://msdn.microsoft.com/en-us/library/16ya5xae(VS.80).aspx.

When creating the project with the VS wizard there's a check box in the dll that let you export functions.

Then, in the exe application you only have to #include a header from the dll with the proper definitions, and add the dll project as a dependency to the exe application.

Check this other question if you want to investigate this point further http://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport.

Ismael
+2  A: 

You can either go the LoadLibrary/GetProcAddress route (as Harper mentioned in his answer, here's link to the run-time dynamic linking MSDN sample again) or you can link your console application to the .lib produced from the DLL project and include the hea.h file with the declaration of your function (as described in the load-time dynamic linking MSDN sample)

In both cases, you need to make sure your DLL exports the function you want to call properly. The easiest way to do it is by using __declspec(dllexport) on the function declaration (as shown in the creating a simple dynamic-link library MSDN sample), though you can do it also through the corresponding .def file in your DLL project.

For more information on the topic of DLLs, you should browse through the MSDN About Dynamic-Link Libraries topic.

Franci Penov
A: 

When the DLL was created an import lib is usually automatically created and you should use that linked in to your program along with header files to call it but if not then you can manually call windows functions like LoadLibrary and GetProcAddress to get it working.

Tim Matthews
A: 

In order to avoid loading at runtime, you have to link against the .LIB provided by DLL and use .H files for function declarations. This will cause an import table to be created in your executable, making the Windows image loader to bind DLL functions in your code at startup, saving you the effort for LoadLibrary/GetProcAddress.

If you don't have the header files you can rewrite them with using __declspec(dllimport) directive, if you know how they are declared.

If you don't have the .LIB file you might need to resort to LoadLibrary/GetProcAddress method mentioned above.

http://en.wikipedia.org/wiki/Dynamic-link_library#C_and_C.2B.2B_2

ssg
+1  A: 

There are many ways to do this but I think one of the easiest options is to link the application to the DLL at link time and then use a definition file to define the symbols to be exported from the DLL.

CAVEAT: The definition file approach works bests for undecorated symbol names. If you want to export decorated symbols then it is probably better to NOT USE the definition file approach.

Here is an simple example on how this is done.

Step 1: Define the function in the export.h file.

int WINAPI IsolatedFunction(const char *title, const char *test);

Step 2: Define the function in the export.cpp file.

#include <windows.h>

int WINAPI IsolatedFunction(const char *title, const char *test)
{
    MessageBox(0, title, test, MB_OK);
    return 1;
}

Step 3: Define the function as an export in the export.def defintion file.

EXPORTS    IsolatedFunction          @1

Step 4: Create a DLL project and add the export.cpp and export.def files to this project. Building this project will create an export.dll and an export.lib file.

The following two steps link to the DLL at link time. If you don't want to define the entry points at link time, ignore the next two steps and use the LoadLibrary and GetProcAddress to load the function entry point at runtime.

Step 5: Create a Test application project to use the dll by adding the export.lib file to the project. Copy the export.dll file to ths same location as the Test console executable.

Step 6: Call the IsolatedFunction function from within the Test application as shown below.

#include "stdafx.h"

// get the function prototype of the imported function
#include "../export/export.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    // call the imported function found in the dll
    int result = IsolatedFunction("hello", "world");

    return 0;
}
jussij