Thread: Working with DLL files...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Working with DLL files...

    How do I create a dll file, and how can I read from it?


    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Creating a DLL file is different depending which compiler you are using. Usually it is just an option that gets passed to the linker that tells it to create a library file instead of an executable.

    You dont read from DLL files, you use them. This is usually done with a call to LoadLibrary() to load the DLL into memory, and then GetProcAddress() to get the address of a function in the library.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    what do you mean by "function address"?
    don't i use the functions from the dll like any other function?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, you dont. A DLL is a library of functions, but in order to call one of these functions - your program needs to know where the function is located in the DLL. That is where the GetProcAddress() comes in.

    Now you can use a .lib file to make things a little easier for you. A .lib file is created from a dll file, and it allows you to call dll functions normally without using GetProcAddress(). The way you create or link to .lib files differs from compiler to compiler, but since it appears you are using Borland...

    You create a .lib file with Borland's implib tool.
    implib mydll.lib mydll.dll

    To tell the compiler to link to this file, choose Project -> Add To Project from the menu. Now in the New Unit tab, choose Browse. In the dialog box, change Files of type to 'Library file (*.lib)', find the lib file and click OK

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    yeah i thought about using libs, and i do know how to create one, with borland

    but correct me if i'm wrong, the main difference between lib and dll is the fact that i don't link the dll to the exe, unlike with lib, so i always have the exe and dll files. so using a dll file makes it easier for me to up-date my software, i can simply patch the dll file instead the entire exe.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, even if you use a .lib file, the DLL file is still required. Let's say your dll file has a function that looks like:
    Code:
    int dllfn(char *cArray);
    Without a lib file, you would have to do:
    Code:
    typedef int (*DLLFN)(char*);
    DLLFN dllfn;
    char someArray[10];
    HMODULE hMod = LoadLibrary("mydll.dll");
    dllfn = (DLLFN)GetProcAddress(hMod,"dllfn");
    dllfn(someArray);
    If you link to the .lib file, then all you would have to do is:
    Code:
    #include "mydll.h"
    ...
    int i = dllfn(someArray);
    In both cases, the dll file is needed and must be included with the .exe file. If you use the .lib file to link, you do not need to include it with the .exe file when you distribute it. The .lib file is only used in the linking process to tell the linker where the dll function are located.
    Last edited by bithub; 11-14-2004 at 02:37 PM.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i'm sorry but i don't understand the connection of lib here...
    if i could simply put the next code in a function, why would i need to create a lib and link it...

    Code:
    typedef int (*DLLFN)(char*);
    DLLFN dllfn;
    char someArray[10];
    HMODULE hMod = LoadLibrary("mydll.dll");
    dllfn = (DLLFN)GetProcAddress(hMod,"dllfn");
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    if i could simply put the next code in a function, why would i need to create a lib and link it
    You dont need a .lib file. They are just used to make life a little easier for the programmer. Imagine if you are calling 100 different functions from a DLL file. Do you really want to go through the process of declaring a function pointer and calling GetProcAddress() for each one?

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>i'm sorry but i don't understand the connection of lib here...

    The .lib is created automatically when you make the dll. It contains the functions address (location) in the dll (same as GetProcAddress() does). It must be built into the exe (with the header).

    Look at 'implicit' (static) v 'explicit' (dynamic) linking.

    Sometimes you only need a function occasionally so you link to the dll while the app is running, use the function, free the dll (FreeLibrary() ). As per the code snippet.

    Why?

    The dll is someone elses work / not included in all versions of the app.

    You may not know the name of the dll you want until run time, ie OS or hardware specific.

    No changes to the header or .lib are needed if you explicitly link.

    Some performance and error issues.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working with binary files
    By spank in forum C Programming
    Replies: 21
    Last Post: 01-04-2006, 08:55 AM
  2. Working with files in C
    By fmchrist in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2005, 02:28 PM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM