Creating DLL with c/c++

Started by
7 comments, last by skwee 16 years, 12 months ago
Hello i want to learn how to create dll files and load them from my program. Lets image i have a game and all the AI of the game is stored in dll called AI.dll so i want to know how i can load this dll to my main program and use its functions? Any tutorials, references will help. BTW tried to search google nothing helpful. Thanks a lot.

I would love to change the world, but they won’t give me the source code.

Advertisement
Not too long ago I had to make a DLL to verify CD Keys in an Installshield dialog. This is the resouce I used: http://www.icynorth.com/development/createdlltutorial.html

It will teach you the basics of creating a DLL, and how to export your functions so that C++ and non C++ programs alike can use them.



- A momentary maniac with casual delusions.
This one is good but its very simple, i mean there is no code explanation and there is something about .def files :I need something deeper with explanation about DLLMain func, also it will be good if the code that written there will be explained line by line.
Thanks again

I would love to change the world, but they won’t give me the source code.

Ppl 68 views and 1 response?! Plz guys i need it!

I would love to change the world, but they won’t give me the source code.

There is source code with the tutorial:
http://www.icynorth.com/files/MakeTempFilenameDLL.zip

Take it apart and replace the function with your own. This isn't really a solution where you can expect to download a DLL that does exactly what you want, you'll have to work at it a little.

Check out Super Play, the SNES inspired Game Engine: http://www.superplay.info

Hey there,

I'll show you how I do it, and if you have any questions let me know.

Firstly, I use MS Visual Studio C++ Express, and all of the VC++ studios (as far as I know), create a definition to tell the source whether or not it is part of the DLL project or not. So, for instance, lets say I create a project called "AIEngine".


I'd create a header file as follows (or just put it at the top of my code).
#ifndef __DLLDEF_H__#define __DLLDEF_H__#ifdef AIENGINE_EXPORTS	#define AI_DLL_API __declspec(dllexport)#else	#define AI_DLL_API __declspec(dllimport)#endif#endif


That way, if my API is being exported, AI_DLL_API is replaced with __declspec(dllexport), and then if imported, __declspec(dllimport) The two __declspec's tell it whether to import or export the function definitions using C++.


Now, in order to use this, let's say we create another header file with a code definition to add two integers:
In the header file (don't forget to include the other header defining AI_DLL_API):
int AI_DLL_API add(int x1, int x2);


Then in the source file (note that it doesn't need the AI_DLL_API definition):
int add(int x1, int x2){return(x1+x2);}


Then, once you compile the DLL, the function will be exported in the DLL. You can check that it was exported correctly using a program called "Dependency Walker" (Google for it).

Then, when it comes time to use the DLL in another program, just make sure you tell the program to include the lib file (which will link the dll file), and include the header files needed (that have the definition of add(int,int) in it).

And there, you're using a DLL.



Now, it seems like you want to dynamically load different DLL files, correct? (Something like TA Spring does if you've ever played it).
To do that, look up the LoadLibrary() function (it takes a filename as an argument to load a library). Then you can use GetProcAddress() to get an address for a process (function) within the library. As far as I know, though, this only works for functions that are _cdecl calls, which can get annoying. If you need any help, let me know.

Hope that clears things up (more than confuses you)
~zix
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Nice, this one was helpful thanks =)

Actually yes i want to dynamic loading libs.
And i want to know is there another way instead of using GetProcAddress(), i mean can i load dll dynamically and use its function without using GetProcAddress()? Or this is the only way?
And also want to know about export and import:
I have to files one dll.h another one dll.cpp:
dll.h i have definition of function average(int x1, int x2) and after the return type of the function I'll put __declspec(dllexport) right? like
int __declspec(dllexport) average(int x1, int x2);
And where I use __declspec(dllimport)?

Oh one more issue i want to get info about is the DllMain or DllEntryPoint function, what should it do? I always must create this function?

One more question :)
Can i make assist functions inside dll? Lets say want to do dll for calculating roots of quadratic equation, but i want to do this function simple and short and the only thing it does it returns two roots. To make it small i want to make one more function to calculate Discriminant but i dont want this function will be using outside the dll. So what i need to do? Is it right to make inside dll function? Or its bad programming style?

Thanks again

I would love to change the world, but they won’t give me the source code.

Hi again,

To my knowledge, the only way to get an address of a function is by using GetProcAddress() (On win32 systems, anyway..). There may be a function I'm not aware of, consider checking MSDN.

1) You're correct about where to put __declspec(dllexport). You use __declspec(dllimport) when using the header file with the project you want to use the dll with. (So this can mean using the method I showed earlier, or replacing each dllexport with a dllimport in the header files you use).

2) To use DLLMain function, you must create it, yes. I've never used it, so I can't help much there.

3)Yes, you can. Just define them like any normal function, and just don't put the __declspec(dllexport/dllimport) in front of the function prototype.

~zix
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Thanks for the fast answer!! Your information was helpful =)

The question about another method without GetProcAddress() is still relevant

I would love to change the world, but they won’t give me the source code.

This topic is closed to new replies.

Advertisement