-
Creating DLL in DevC++
I have a long .C file full of functions.Now I want to group those functions and create them as a dll. I am working in DevC++.
So please help me to create dll in DevC++ and also to access the dll in another DevC++ console application.
Please help me............
-
Dev-C++ DLL's
open dev-cpp
File-->New Project
choose DLL and 'c or c++'
click OK
click Save in save dialog
add an include to your source file
before you exportable functions (& their prototypes) add "__declspec (dllexport)"
like
__declspec (dllexport) DWORD ud (void){return updated;}
Off you go ! you can change the executable produced (the .dll) in project properties
I don't know how to do 'import libraries', you can call the exported funcs from an exe like this:
Code:
typedef DWORD (* LPFNDLL_hp2) (DWORD *d );
LPFNDLL_hp2 lpfn_hp2;
HINSTANCE Hdllhp;
//get dll func address
Hdllhp = LoadLibrary("your_DLL_name.dll");
if(!Hdllhp){
ssmsg("Missing DLL: your_DLL_name.dll");return FALSE;
}
lpfn_hp2 = (LPFNDLL_hp)GetProcAddress(Hdllhp , "dw");
//call func in DLL
(lpfn_hp2)(0x1234abcd);
I use this extensively calling DLL funcs from DLLs produced in both Dev-C++ and Borland 5.5,
called from an EXE made with Borland, or another in assembler.
when you create the dll project, dev-cpp will create these lines
Code:
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
you just define BUILDING_DLL for your dll and not for the app that calls the functions, so
Code:
__declspec (dllexport) DWORD ud (void){return updated;}
becomes
Code:
DLLIMPORT DWORD ud (void){return updated;}
Note the DWORD above is the return type, and this example has no params, i.e.void. 'updated' was just a global from where I copy/pasted from.
Hope this helps...........
-
hi,
thanks for ur reply...but stiil i am not able to access the dll functions.........
This is my dll.h file:
Code:
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
#endif /* _DLL_H_ */
this is my dllmain.c file
Code:
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld ()
{
printf("\n hai");
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
I am able to compile my dll without any erros.
This is my .c file in which i invoke the dll:
Code:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include "dll.h"
int main(int argc, char *argv[])
{
HelloWorld();
system("PAUSE");
return 0;
}
when i compile this .c file i get a compile time error in the makefile.win...this is the exact error:
C:\Dev-Cpp\test4\Makefile.win [Build Error] [test4.exe] Error 1
Please help me out...Should i change any of the project settings while accessing the dll?
-
The .c file knows the name and signature(return type & params) of your function, but hasnt loaded the dll yet. Read back in my earlier post about = LoadLibrary("your_DLL_name.dll"); and GetProcAddress(Hdllhp , "dll_function");
if you have a test func in the dll like
void Hello(void)
add a call to MessageBox() in it
then you can test the func like this from the command line
rundll32 MyDll.dll,Hello
-
If you get the free command-line Borland 5.5 compiler package, it includes a utility call tdump
This will display info about exe's and dll's, including exported names
start here
http://community.borland.com/cgi-bin...vey.cgi?sid=33
-
Or you can use QuickView (Windows) or ld or file (Linux) or a dependency viewer.
-
thanks for ur reply
Hi thanks for ur reply..........
I used the APIs "LoadLibrary" to load the dll and "GetProcAddress " to load the functions , and it worked.........
thanks a lot..................