I've done my first DLL in C, it's very simple since it contains only two functions (sum and rest), I've tested it with Visual Basic and Delphi and it works, but I don't know how to use this DLL from a C program (console), is it possible to use a DLL from a console program or do I have to use a Windows program (WinMain)?
Anyone has an example of this (console or windows)? Thanks in advance for your help.
Geo.
You can call a dll from a console just like from a GUI. Either using a lib and compile time linkage or runtime linkage. Here's an example for runtime linkage
| CODE |
#include <windows.h> int main() { HINSTANCE dll; double (*MyFunction)(double,double);
//We load the dll dll=LoadLibrary("hello.dll"); //We assign the function pointer MyFunction=(double(*)(double,double))GetProcAddress(dll,"hello"); //We check if it worked if(MyFunction==NULL) return 0; //We call it (MyFunction)(115,546); //We free the dll FreeLibrary(dll); system("pause"); return 0; } |
Note that I only tested it for C++ but it should be C compatible
thanks for the comment, I'll try it and let you know how it works :).
------------------
Edit:
I've tried your example in a C program and it works great! Thanks a lot for your help. Yes, it works the same in C, here's my code:
| CODE |
#include <windows.h>
int main(int argc, char *argv[]) { HINSTANCE dll; void (*HelloWorld)(void);
dll = LoadLibrary("Cdll.dll"); HelloWorld = (void(*)(void))GetProcAddress(dll,"HelloWorld");
if(HelloWorld==NULL || Suma == NULL) return 0;
(HelloWorld)(); FreeLibrary(dll); system("PAUSE"); return 0; }
|
The HelloWorld function looks like this (extracted from Dev-C++'s DLL template):
| CODE |
void HelloWorld () { MessageBox (0, "Hello World from DLL!\n", "Hi", MB_ICONINFORMATION); }
|
this function shows a Dialog using Win32 Api's MessageBox function.
I've also found info on how to link the DLL in compile time, thanks a lot dr voodoo!
José Jorge (Geo).