Thread: How to link dll

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    How to link dll

    Hi, guys,
    I got a problem when I am trying to link other project in MS VC++. I made a project A and include and link the header and library in B without problem. But I can not find a good way to link dll of B, there for I have to copy all dll in A. Ugly! I think there must be a way to do it in MS VC++. Please help me. Thank you.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Linking to a library is possible because that is the function of compiled library files. You cannot link (at compile time [which is what it sounds like you're meaning]) to a DLL because of the very nature of a DLL ("Dynamic Link" Library). You must link dynamically (at run time) to a DLL file by loading it into your program's memory space. Look up LoadLibrary().

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    4
    Thanks for help. I have just looked at it and it uses SetDllDirectory( LPCTSTR lpPathName). Since I have a couple of derectories of dll that has to be used at run time, I tried to set path in Env. variables of the system but it does not work. I have less exp. in using MS VC++, is there any where that allowing use dlls from other place like linking library?

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Since DLLs are loaded at run time, you cannot specify anything within the MSVC IDE that has anything to do with it. If you set your PATH variable to include your DLL directories and they can't be loaded, try rebooting your system then giving it another shot. One thing to note, however, is that if you are distributing these DLLs, you should just use SetDllDirectory() because your end users likely won't be thrilled about having to modify their system path just so your app can find it's DLLs.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    4
    Thanks, I will try to use SetDllDirectory().

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Since DLLs are loaded at run time, you cannot specify anything within the MSVC IDE that has anything to do with it.

    Not 100% true.....

    DLLs can be linked IMPLICITLY or EXPLICITLY.

    Implicitly is at load time. It requires that you link the exe to the DLLs header and LIB file using the IDE (as you would to a .LIB). If you created the DLL these will have been produced by the DLL. The DLL can be written in any language as long as the correct calling conventions are defined in the DLLs functions.
    If the DLL is not found the app will not run.
    MSDN
    "The import library only contains code to load the DLL and to implement calls to functions in the DLL. Finding an external function in an import library informs the linker that the code for that function is in a DLL. To resolve external references to DLLs, the linker simply adds information to the executable file that tells the system where to find the DLL code when the process starts up."

    Explicitly is at run time. You LoadLibrary(), lock the functions with GetProcAddress() and use the DLL code. When finished you 'drop' the DLL with FreeLibrary(). This means the DLL can be used as required and freed when not in use.
    You may also need a .DEF file as in some cases the function names can get 'mangled'.
    This form of linking is usually reserved for special uses
    ie don't know which DLL you will use until run time.
    not all versions of app will use DLL so some don't include it ie demo versions.
    The DLL changes frequently and so don't want to link to its .LIB
    "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. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Link DLL with Visual C++ not working
    By status() in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2006, 02:47 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. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM