How would you link a program to a DLL?

This is a discussion on How would you link a program to a DLL? within the C++ Programming forums, part of the General Programming Boards category; What is this process called? Linking a program to a DLL for more source code? Where would I learn this? ...

  1. #1
    Pro
    Pro is offline
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    How would you link a program to a DLL?

    What is this process called? Linking a program to a DLL for more source code? Where would I learn this? Can anyone explain a bit? Thanks.
    Last edited by Pro; 02-08-2008 at 09:11 PM. Reason: Misspelled Title

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Nice name.

    A DLL is a Windows-specific dynamically-loaded library. Basically, you compile your program with some statically-linked library (say, libSDL.a or SDL.lib), and then when you run your program, it looks for say SDL.dll and loads that into memory. This means several things:
    • If the library is already loaded into memory, it does not have to be loaded again. This is great for really common libraries.
    • If the DLL version is not what your program is expecting, you might have trouble.
    • And of course, anyone who runs your program must have the DLL.


    Anyway, with that out of the way . . . when you create a DLL, you basically combine a bunch of .o (or .obj) files. When you compile a .c or .cpp file, that is what you get. A DLL is just ordinary C or C++ code written in such a way that the code does not have a main(); rather, it has functions and classes that can be used in another program.

    [edit] At least, that's what I think . . . I've never really used DLLs, being a Linux person. For more information, you might try google: http://www.google.ca/search?hl=en&q=...e+Search&meta= [/edit]
    Last edited by dwks; 02-08-2008 at 09:43 PM.
    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.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Linking a DLL usually requires 3 things:
    1) The DLL file itself.
    2) A .lib file to link with, specific to the DLL. Generated when creating the DLL file.
    3) A header with the prototypes of the functions exported by the DLL.

    What you do is include the header file, the add the .lib file under "additional libraries" or such under your linker so it searches the lib file.
    Then you can call all those functions in the dll as if they were in your own program. This process is called static linking.

    There is another way called dynamic linking.
    In this one, you don't need a .lib file, nor a header really, but you still need to know what functions are exported and how to call them correctly, so a header is a good thing nevertheless.
    Anyway, you first load the dll using LoadLibrary.
    You then find the function you want to call using GetProcAddress, whose return you should cast to a function pointer. Then you can call that function pointer to call the function of choice. Then you close the library using FreeLibrary.

    Static linking requires the DLL to be present with your application, otherwise Windows will complain "X.dll couldn't be found. Try reinstalling the application." Your program will never even begin to run.

    Dynamic linking has the advantage that you can check if the dll exists and print errors or whatever, but your program can run without the dll.
    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.

  4. #4
    Emulator
    Join Date
    Feb 2008
    Posts
    43

    Talking

    Quote Originally Posted by Elysia View Post
    Linking a DLL usually requires 3 things:
    1) The DLL file itself.
    2) A .lib file to link with, specific to the DLL. Generated when creating the DLL file.
    3) A header with the prototypes of the functions exported by the DLL.

    What you do is include the header file, the add the .lib file under "additional libraries" or such under your linker so it searches the lib file.
    Then you can call all those functions in the dll as if they were in your own program. This process is called static linking.

    There is another way called dynamic linking.
    In this one, you don't need a .lib file, nor a header really, but you still need to know what functions are exported and how to call them correctly, so a header is a good thing nevertheless.
    Anyway, you first load the dll using LoadLibrary.
    You then find the function you want to call using GetProcAddress, whose return you should cast to a function pointer. Then you can call that function pointer to call the function of choice. Then you close the library using FreeLibrary.

    Static linking requires the DLL to be present with your application, otherwise Windows will complain "X.dll couldn't be found. Try reinstalling the application." Your program will never even begin to run.

    Dynamic linking has the advantage that you can check if the dll exists and print errors or whatever, but your program can run without the dll.
    Thank you guys. Would you guys mind giving me an example code of linking to a DLL? Seems like im going to use the dynamic linking process instead of static.

    *Also, I changed my name to this, hehe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Code:
    // Let's find the function "foo", whose prototype is "void foo(int n)".
    typedef void (FooFncPtr)(int);
    HMODULE hDll = LoadLibrary("my dll.dll");
    FooFncPtr* pFoo = (FooFncPtr*)GetProcAddress(hDll, "foo");
    pFoo();
    pFoo = NULL;
    FreeLibrary(hDll);
    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
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by Elysia View Post
    Code:
    // Let's find the function "foo", whose prototype is "void foo(int n)".
    typedef void (FooFncPtr)(int);
    HMODULE hDll = LoadLibrary("my dll.dll");
    FooFncPtr* pFoo = (FooFncPtr*)GetProcAddress(hDll, "foo");
    pFoo();
    pFoo = NULL;
    FreeLibrary(hDll);
    Thanks once again! What header file would you use to do this? Is this static or dynamic linking? Thanks.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Static linking requires the DLL to be present with your application, otherwise Windows will complain "X.dll couldn't be found. Try reinstalling the application." Your program will never even begin to run.
    There is no such thing as statically linking to a DLL. DLL stands for Dynamic Link Library, therefore all DLL linking is dynamic. You are getting static linking mixed up with implicit linking.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    This is dynamic.
    All of these functions are located somewhere in the nest of Windows.h. And you would like to have a header to the dll that shows the exported functions from the dll and the prototypes of them.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Thanks once again! What header file would you use to do this? Is this static or dynamic linking? Thanks.
    Thats called explicit linking to a DLL file. The only header file you need to do that is windows.h.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by bithub View Post
    There is no such thing as statically linking to a DLL. DLL stands for Dynamic Link Library, therefore all DLL linking is dynamic. You are getting static linking mixed up with implicit linking.
    Yeah, it's called implicit linking. My bad.
    Thanks for the correction.
    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.

  11. #11
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    What are the big difference between implicit, dynamic, and static linking? Thanks lol.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    No such thing as static linking as bithub mentioned. It's called implicit linking. It was my bad >_<
    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.

  13. #13
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    It's no problem. One of you spelled it explicit linking, and the other implicit linking. Which one is the right one?, lol.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Oh I see bithub just called it explicit. However, according to MSDN, it's called implicit linking.
    But even so, the name doesn't really mean that much. It's the first method I mentioned, using a .lib file.
    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.

  15. #15
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    So one uses a .lib file and the other doesn't? That is the main difference right? Just checkin'..

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. How to link dll
    By liang in forum Windows Programming
    Replies: 5
    Last Post: 07-14-2005, 11:55 PM
  3. dynamically link my DLL to an application
    By Maranello in forum C++ Programming
    Replies: 0
    Last Post: 04-20-2005, 07:52 AM
  4. DLL Error with VB .NET Program
    By willc0de4food in forum Windows Programming
    Replies: 6
    Last Post: 04-09-2005, 02:38 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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