I can compile DLLs properly using mingw and do the exports/imports stuff. What I am looking for is defining the dll onload function properly as you would in MS VC products. Google didn't turn up anything. Anyone have any ideas or a link to a tutorial?
-
1By "dll onload" you referring to the DllMain? Are you sure it has the right signature? It should be like this: extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID LPV). Try adding a code snippet to your question. – Vitor Py Jun 21 '11 at 20:01
-
Yes that's exactly what I mean, thanks. – RobotHumans Jun 21 '11 at 20:05
-
@aking1012 I didn't say that you should add that DllMain signature to the question but for you to add your own DllMain signature. – Vitor Py Jun 21 '11 at 20:17
-
Oh, I don't need it to take any arguments. I just need to execute function contents with no arguments and no return value on dll load. – RobotHumans Jun 21 '11 at 20:21
-
Have you tried compiling this with MinGW? I don't see a reason this shouldn't work. – rubenvb Jun 21 '11 at 20:27
-
@aking1012 You don't need to use the arguments but your function should have this exact signature or it will not work. – Vitor Py Jun 21 '11 at 20:32
-
If it's because I was using onLoad instead of DllMain I'm going to feel really retarded...checking thx – RobotHumans Jun 21 '11 at 20:32
-
Got it working. Exact code example below – RobotHumans Jun 22 '11 at 00:14
2 Answers
Okay, so after some fiddling...it's working. For anyone else that is having issues here it is. My issues weren't related to compiling in instead of loading dynamically. It was a mash-up of a couple of tutorial/question/how-tos that got me to this point.
dll.c
#include <stdio.h>
#include <windows.h>
#include "dll.h"
//extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID LPV) {
//This one was only necessary if you were using a C++ compiler
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// Code to run when the DLL is loaded
printf ("Load working...\n");
break;
case DLL_PROCESS_DETACH:
// Code to run when the DLL is freed
printf ("Unload working...\n");
break;
case DLL_THREAD_ATTACH:
// Code to run when a thread is created during the DLL's lifetime
printf ("ThreadLoad working...\n");
break;
case DLL_THREAD_DETACH:
// Code to run when a thread ends normally.
printf ("ThreadUnload working...\n");
break;
}
return TRUE;
}
EXPORT void hello(void) {
printf ("Hello\n");
}
dll.h
#ifndef DLL_H_
#define DLL_H_
#ifdef BUILD_DLL
/* DLL export */
#define EXPORT __declspec(dllexport)
#else
/* EXE import */
#define EXPORT __declspec(dllimport)
#endif
EXPORT void hello(void);
#endif /* DLL_H_ */
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;
}
when compiled with
gcc -c -DBUILD_DLL dll.c
gcc -shared -o message.dll dll.o -Wl,--out-implib,libmessage.a
gcc -c hello.c
gcc -o hello.exe hello.o message.dll
produces the expected output of
Load working...
Hello
Unload working...

- 15,050
- 18
- 63
- 96

- 807
- 10
- 25
Since mingw is just a windows port of GCC and associated tools, you can use GCC constructor and destructor attributes. These work for both shared and static libraries, and execute code before and after main is run, respectively. Additionally, you can specify multiple constructor and destructor functions per library.
static void __attribute__((constructor))
your_lib_init(void)
{
fprintf(stderr, "library init\n");
}
static void __attribute__((destructor))
your_lib_destroy(void)
{
fprintf(stderr, "library destroy\n");
}

- 2,863
- 23
- 15
-
Thanks. This is helpful for the cross-platform stuff if I need to do something similar on the nix side. – RobotHumans Jun 22 '11 at 19:54
-
I use previous dll.c,dll.h,hello.c and compile method, just add these functions, and it can not work. I guess because DLL won't call main() – Daniel YC Lin Apr 29 '13 at 11:45
-
please be more specific than 'not work'. DLLs never call main, the dynamic linker loads & executes library constructors, then main(), then destructors. – Bobby Powers Apr 29 '13 at 13:47
-