# How to create and use a .dll with C++ ?

This topic is 4802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I create a program that accesses a .dll, I know how to create a .dll, but I do not know how to create a program that utilizes the dll.

##### Share on other sites
If you're using Windows, take a look at LoadLibrary() and GetProcAddress(). They should be enough to at least get you started.

EDIT: Forgot to mention that if you create a .lib when you make your dll, you can just include it in your project (at least in VS, your compiler may vary), and then you can skip the LoadLibrary() and GetProcAddress() stuff.

##### Share on other sites
Quote:
 Original post by vallesHow do I create a program that accesses a .dll, I know how to create a .dll, but I do not know how to create a program that utilizes the dll.

export ur function(__delcexport) in the *.dll file, create a header(*.h) file with that function prototype in the *.dll file u create. Then, build a new *.dsw or *.sln file, include the header file(*.h)(let's say: #include "blah.h") in the files view to the new *.exe file, and then add AT the very top of the main file(where the main entry point is or related *.cpp files or where the files that u r going to call that function) then u can use ur function from the *.dll file.(of course, use #pragma comment(lib, "blah") in the same *.cpp file and copy the *.dll file to the place where the *.exe is.

##### Share on other sites
Depends on the compiler.

For me, using GCC on windows, this works:

//**** SHARED HEADER ****void foo ( void );//**** DLL SOURCE FILE ****#include "header.h"void foo ( void ) {    //...implement foo...}//**** EXE SOURCE FILE ****#include "header.h"int main ( void ) {    foo();}

g++ -c dllsource.cpp -o dllsource.og++ -shared dllsource.o -o libexample.dllg++ -c exesource.cpp -o exesource.og++ -L/path/to/libexamples/directory -lexample exesource.o -o example.exe

Then the DLL will need to be in your %PATH% or the same directory as your EXE.

There are alternative methods which will deal with stripped libraries and so forth as well, usually involving .def files.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632149
• Total Posts
3004453

×