Thread: How do I use a dll file??

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    19

    How do I use a dll file??

    Hi,

    I have a general idea of what dll file does. From my understnading, we can write a set of useful functions and data and can compile it to a dll format. But if we want to have other programs access this dll's functions, we need to "export" the functions using .def file or by inserting some Microsoft specific directives in the code.

    But I was a bit confused when I saw some legacy automated test scripts at my work. The scripts had some lines like
    Code:
     nunit-console.exe unittest.dll
    Here nunit-console.exe is a test framework that does unit testing by utilitizing functions defined in unittest.dll. But when I did dumpbin.exe /EXPORTS unittests.dll, I couldn't see any function that was exported. I tried with /all option but ended in vain.

    nunit-console.exe is a largely available 3rd party testing harness, and unittest.dll is something a developer at my work wrote. This may seem a stupid question, but then how can nunit-console.exe access information (whether it's function or data) in the dll file if no functions were exported?

    Thanks in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Maybe it just runs it? ie loads it so DllMain() or whatever it's called is run.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    You don't always need .def files, that's a Visual Studio thing.
    You just need LoadLibrary, FreeLibrary, and GetProcAddress to load functions from a dll. I guess that script gets the dll from command line and loads it.

    If you're interested in using DLLs, try a Simple Message box loading from a DLL tutorial.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by execute View Post
    You don't always need .def files, that's a Visual Studio thing.
    You just need LoadLibrary, FreeLibrary, and GetProcAddress to load functions from a dll. I guess that script gets the dll from command line and loads it.

    If you're interested in using DLLs, try a Simple Message box loading from a DLL tutorial.
    However, GetProcAddress requires the function to be exported, either as a ordinal (numeric) or as a named function. If there are no exports, I don't know how it works - probably like zacs7 suggests: It calls dllmain, and it all unravels from there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by execute View Post
    You don't always need .def files, that's a Visual Studio thing.
    You just need LoadLibrary, FreeLibrary, and GetProcAddress to load functions from a dll.
    No, it's not a "Visual Studio thing". It is another way of linking and using functions from a DLL. The way you describe is the way I would call dynamic. .Def files is another way I like to call static.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    IIRC .DEF files are only used to prevent name mangling (exported name can be corrupted, preventing calling).

    All they contain is a list of function names in order, not full declarations.

    A WIN32 C app I worked on required the use of .def files (as well as the .lib) to dynamically call a WIN32 C dll.
    "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

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Dynamic linking allows you to 'drop-in' a new DLL and run without recompiling. Static linking forces you to link with the static export lib of the DLL and thus any changes in the DLL must be recompiled into the static export lib.

    Dynamic linking is more complex but more flexible. Static linking is a snap but not flexible at all.

    Dynamic linking will require you to use LoadLibrary() and GetProcAddress() and static linking will only require you to use a header to define a constant as dllexport or dllimport.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Bubba View Post
    Dynamic linking allows you to 'drop-in' a new DLL and run without recompiling. Static linking forces you to link with the static export lib of the DLL and thus any changes in the DLL must be recompiled into the static export lib.

    Dynamic linking is more complex but more flexible. Static linking is a snap but not flexible at all.

    Dynamic linking will require you to use LoadLibrary() and GetProcAddress() and static linking will only require you to use a header to define a constant as dllexport or dllimport.
    There is also delay-load linkage. It works like static linkage, but loads the dll only on first call to its function. So application could check the presense of the dll, using LoadLibrary function, and if the dll is not found - just disable the part of the code that uses the functions of the dll (and so the dll will never be loaded and user will not see the "Missing file" message), but there is no need to use this GetProcAddress crap
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Well, sometimes (not often) it is beneficial to manually load and link the DLL. I have had to do it before a couple times, but only rarely and for specific reasons.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    Well, sometimes (not often) it is beneficial to manually load and link the DLL. I have had to do it before a couple times, but only rarely and for specific reasons.
    The most common scenario of this is when you have several choices for an interface type DLL - e.g. a DLL that knows different models of cameras or scanners to import pictures to the photo editor. By choosing the right DLL, the photo editor in itself doesn't need to know how to import an image from a Canon EOS 10D or a Fuji FinePix 1234 or a HP ScanJet 4567. Instead, a DLL for each of those models can be produced to provide this functionality.

    Another example would be in the photo editor that wants to be able to load/save many different file formats. Instead of having the application in itself know how to load the content of a JPEG, TIFF, BMP, PNG, etc, etc, we write a DLL that knows how to deal with each format, and when the application is asked to open a JPEG, it loads "LoadSaveJpeg.DLL", and calls it's Load() function with the name of the image.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This is a nice little class for dynamic DLL use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM