Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Quote:
Original post by valles
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.


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 this post


Link to post
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.o
g++ -shared dllsource.o -o libexample.dll

g++ -c exesource.cpp -o exesource.o
g++ -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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this