Creating DLL in DevC++

This is a discussion on Creating DLL in DevC++ within the C Programming forums, part of the General Programming Boards category; I have a long .C file full of functions.Now I want to group those functions and create them as a ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    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............

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    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...........
    Last edited by NogginTheNog; 03-13-2006 at 04:31 PM. Reason: extraneous stuff removed...

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    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?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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
    Last edited by NogginTheNog; 03-14-2006 at 07:48 AM.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    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

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Or you can use QuickView (Windows) or ld or file (Linux) or a dependency viewer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    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..................

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a simple DLL
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2009, 09:41 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 03:20 AM
  4. Creating and using a Dll in Dev-C++
    By Aidman in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 03:07 PM
  5. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21