Results 1 to 7 of 7

Thread: Using Functions INside my DLL

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Using Functions INside my DLL

    Hello

    This is probably pretty simple, but i don't know what to do!
    I have a win32 application and a DLL that I am trying create that will hold functions for various routines etc...


    How do I get my program to use functions that are inside the DLL?

    Cheers in Advance

    Andy

  2. #2
    amac
    Guest
    When you build your DLL there should be a .lib file that is created.

    In your application #include the headers that have the definitions of the functions you wanna use.

    Next, go into your project settings and on the link tab and in the Object/library modules put the path to the .lib file.

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Using Run-Time Dynamic Linking
    You can use the same DLL in both load-time and run-time dynamic linking. The following source code produces the same output as the load-time example in the previous section. The program uses the LoadLibrary function to get a handle to MYPUTS.DLL. If LoadLibrary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL. 
    
    The following example illustrates an important difference between run-time and load-time dynamic linking. If the MYPUTS.DLL file is not available, the application using load-time dynamic linking simply terminates. The run-time dynamic linking example, however, can respond to the error. 
    
    // File:  RUNTIME.C
    // A simple program that uses LoadLibrary and 
    // GetProcAddress to access myPuts from MYPUTS.DLL. 
     
    #include <stdio.h> 
    #include <windows.h> 
     
    typedef VOID (*MYPROC)(LPTSTR); 
     
    VOID main(VOID) 
    { 
        HINSTANCE hinstLib; 
        MYPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary("myputs"); 
     
        // If the handle is valid, try to get the function address.
     
        if (hinstLib != NULL) 
        { 
            ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 
     
            // If the function address is valid, call the function.
     
            if (fRunTimeLinkSuccess = (ProcAdd != NULL)) 
                (ProcAdd) ("message via DLL function\n"); 
     
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
     
        // If unable to call the DLL function, use an alternative.
     
        if (! fRunTimeLinkSuccess) 
            printf("message via alternative method\n"); 
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    Using Load-Time Dynamic Linking
    After you have created a DLL, you can use it in an application. The following file, LOADTIME.C, is the source code for a simple console application that uses the myPuts function exported from MYPUTS.DLL. 
    
    // File: LOADTIME.C. 
    // A simple program that uses myPuts from MYPUTS.DLL. 
     
    #include <windows.h> 
     
    VOID myPuts(LPTSTR);   // a function from a DLL
     
    VOID main(VOID) 
    { 
        myPuts("message printed using the DLL function\n"); 
    } 
     
    Because LOADTIME.C calls the DLL function explicitly, the module for the application must be linked with the import library MYPUTS.LIB. For more information about building DLLs, see the documentation included with your development tools.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Attached Files Attached Files
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question so parksie...

    ok, so what is this for? :
    Code:
    #ifdef _DLL_INTERNAL_
    #define DLLEXPORT __declspec(dllexport)
    #else
    #define DLLEXPORT __declspec(dllimport)
    #endif
    also, what if i want to have globals, and a class, in my dll?
    thanks...
    Amon Ra
    The Power of Learning.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you're using classes, you basically have to use __declspec. I did it this way so that you can use the same header for creating and using the DLL.

    In the project settings for creating the DLL, add _DLL_INTERNAL to the list of definitions

    I've posted how to do classes before. Globals, yep, you can do that. You just need to use extern __declspec(dllexport) or something in the definition.

    Play with it!
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width