DLL Creation in MingWI've always considered DLLs to be esoterically cool stuff - somehow the idea of one program running another makes my imagination run wild. I've now discovered that in many cases DLLs are a bad idea(TM), but for a fledgling programmer, learning how to create a DLL, especially in a free development environment such as MingW would mean an instant familiarity with a lot of development tools, and a lot of the seemingly hidden options of the incredible gcc compiler. As usual, we're diving in. You should have a copy of mingw handy. Get the MingW installer from the Current branch in the Mingw download page, that sets you off easy. You should also be familiar with IDE-less coding, or should know enough to set up an IDE to work with Mingw, I'm not going to cover that here. What are DLLs?First off, what are DLLs? DLLs are dynamically linked libraries. How are they different from static libraries? In static libraries, the linking is done at compile time: all the library functions are combined with the main fragment of program code to create the executable. When the linking is done at runtime, it is called dynamic linking. Since the linking is done at runtime, it is obvious that the operating system will have something to do with it. That is why most DLL implementations are non-portable. When a compiled executable that references a DLL is loaded, the OS looks into the file and sees that the executable references a set of "imports" from a DLL file. This is simply a situation equivalent to finding that "This program uses the following functions which are contained in this dll". The OS then looks into the particular DLL. The DLL has a corresponding and matching set of export functions that the OS then maps from the functions referenced in the main executable to the functions in the DLL. Thus, when the executable calls a referenced function, the code in the DLL is executed. Viola! Dynamic linking! Hello DLL!I'm now going to describe a standard "Hello world" implementation. The code is in three files: hello.c, dll.h and dll.c. The code is listed and explained below: hello.c
#include <stdio.h>
#include "dll.h"
int main () {
hello();
return 0;
}
Hello.c is a standard hello world C program except that the hello() function is going to be dynamically linked. The only special thing here is the inclusion of dll.h. dll.h #ifdef BUILD_DLL /* DLL export */ #define EXPORT __declspec(dllexport) #else /* EXE import */ #define EXPORT __declspec(dllimport) #endif EXPORT void hello(void); DLL.h is where most of the magic happens. It begins with checking the BUILD_DLL macro. We manually set this macro when building so that the macro EXPORT is set to __declspec(dllexport), so that gcc can build the dll. When the OS calls up the dll from the executable, BUILD_DLL is not set and therefore EXPORT is set to __declspec(dllimport) which is a nice set of macro routines to expose our function to the calling scope. Note that __declspec(dllexport) and __declspec(dllimport) are mingw macros to faciliate DLL creation. They are mapped to their equivalent WinAPI headers. dll.c #include This is the actual code of the hello world routine. There should be nothing special here. Compiling and Linking the filesDLL creation used to be a tiresome process. Recent advances in gcc and mingw engines have meant that it takes only four steps now to create a dll. They are:
gcc -c hello.c gcc -c -DBUILD_DLL dll.c Notice the use of the -D param by which we set the macro BUILD_DLL. It is used for setting export to __declspec(dllexport) so that compilation can take place. gcc -shared -o message.dll dll.o -Wl,--out-implib,libmessage.a
The third step requires more explanation. gcc -o hello.exe hello.o message.dll The fourth step is actually a bit of gcc magick. The actual step is something like this: gcc -o hello.exe hello.o -L./ -lmessage
The -L param means that the linker checks the ./ path for imports Once you've finished these steps, run the program! C:\>hello Hello! Wait, that's not dynamic!You're right, that's not *really* dynamic. Of course, you're loading the function at run-time, but what is the purpose of that if you need to reference the dll at compile-time? One of the reasons why a dll is used is to enable a plugin architecture for your program. Other people can write code that your program can use, and you can't really anticipate other people's dlls, and if you are going to recompile your program every time a new dll comes along, then why use dynamic linking at all? Solve this by using two functions from the Windows API: LoadLibrary() and GetProcAddress(). We modify the codes for the main module like this: hello.c
#include <windows.h>
#include <stdio.h>
int main () {
/*Typedef the hello function*/
typedef void (*pfunc)();
/*Windows handle*/
HANDLE hdll;
/*A pointer to a function*/
pfunc hello;
/*LoadLibrary*/
hdll = LoadLibrary("message.dll");
/*GetProcAddress*/
hello = (pfunc)GetProcAddress(hdll, "hello");
/*Call the function*/
hello();
return 0;
}
The code should be self explanatory, there are just some things that you should remember:
handle = LoadLibrary("path to dll file");
handle is <= HINSTANCE_ERROR if there is an error loading the dll. pointer_to_function = (pointer_to_function_type)GetProcAddress(dll_handle, "resource_name"); We can also use global variables inside the dll that are prefixed with export as a valid resource name, it needn't be a function. That is, variable = (type)GetProcAddress(dll_handle, "global_variable_name_inside_dll"); is also valid. To compile it, don't change a thing for the dlls. For compiling hello.c, a simple... gcc -o hello.exe hello.c...would do. More informationThis doesn't solve the problem of implementing a plugin arch, but we can work towards it. One of the ways in which this is done is to reference a function with a unique name from every dll called. For example, a program searches for all dll files beginning with a particular header in the file name and loads it via LoadLibrary. For example, a music program called foobar might look at all dll files which begin with foo. Thus foo_looks.dll will be loaded, while gym.dll won't be. Then inside the dll, it searches for a particular resource say "play", and loads and runs it. An implementation of the above method, branched off from sortalg, can be found here.
free swinger exotic
Submitted by Drugan888 on Fri, 2008-09-26 12:53.
free swinger exotic stories
sexy swinger couple stories
stories swinger party
stories swinger sex
swinger personal stories
porn teen titans
teen titans porn
pink porn
porn forum
forum porn
vanity shemale porn star
vanity transexual porn
vanity transsexual porn
varda porn
down loading porn
down loading porn with davo
down loading pornography
vassar porn
down low porn
down on the farm porn
vcd porno sma bandung
70s porn stars
70s porn movies
70s porn
vedete porno
vega vixen porn
vegas porn star escorts
vegas pornstar escorts
70's porno music ringtones
70's porn stars
70 year old porn
vegetarian porn
veggie porn
down under porn
veil porn
free full length porn videos
free lesbian porn videos
free gay porn videos
free teen porn videos
free amateur porn videos
free online porn videos
free long porn videos
free porn flash videos
porn videos free
free homemade porn videos
free hardcore porn videos
free porn movies
free full length porn movies
free gay porn movies
free teen porn movies
free full porn movies
porn movies free
free online porn movies
free long porn movies
free latin porn movies
free mature porn movies
free hardcore porn movies
free porn clips
free porn video clips
free porn movie clips
porn clips free
free gay porn clips
free lesbian porn clips
free online porn clips
free teen porn clips
free porn videos trailers clips
free streaming porn clips
free amature porn clips
free gay porn
free gay porn movies
free gay porn videos
free gay porn clips
free gay porn video
free 15 minutes gay porn
free gay black porn
free black gay porn
gay porn free
free gay porn sites
free gay porn pic post
free young gay porn
free gay porn galleries
free gay porn pics
free porn videos
free porn video
free massage porn video
free full length porn videos
free lesbian porn videos
free gay porn videos
free teen porn videos
free amateur porn videos
free online porn videos
free long porn videos
free porn flash videos
porn videos free
free homemade porn videos
free hardcore porn videos
free porn movies
free full length porn movies
free gay porn movies
free teen porn movies
free full porn movies
porn movies free
free online porn movies
free long porn movies
free latin porn movies
free mature porn movies
free hardcore porn movies
free porn clips
free porn video clips
free porn movie clips
porn clips free
free gay porn clips
free lesbian porn clips
free online porn clips
free teen porn clips
free porn videos trailers clips
free streaming porn clips
free amature porn clips
free gay porn
free gay porn movies
free gay porn videos
free gay porn clips
free gay porn video
free 15 minutes gay porn
free gay black porn
free black gay porn
gay porn free
free gay porn sites
free gay porn pic post
free young gay porn
free gay porn galleries
free gay porn pics
free porn videos
free porn video
free massage porn video
|
TopicsRecent blog posts
|