Load DLL from other folder than the current

This is a discussion on Load DLL from other folder than the current within the C++ Programming forums, part of the General Programming Boards category; Hi! so i didn't want to spam my src directory (VS2010) with DLLs and decided to gently move them into ...

  1. #1
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244

    Load DLL from other folder than the current

    Hi!

    so i didn't want to spam my src directory (VS2010) with DLLs and decided to gently move them into an other directory. of course, the application wouldn't find them there, so i tried this:

    Code:
    LoadLibrary("dlls\\SDL.dll");
    but then again, the library i'm using (SDL) won't find the dll anyway.

    does anyone have any idea how to load dlls?
    help would be greatly appreciated.

  2. #2

  3. #3
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    seems like a nice and simple solution, but

    Code:
    SetDllDirectory("C:\\bin");
    	LoadLibrary("c:\\bin\\SDL.dll");
    using this, it's still not working, even if used at the start of main().
    for testing purposes, i've choosen an absolute path "c:\bin"

    could this be because the dlls get loaded even before i get the chance to do this call?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does the LoadLibrary call succeed or fail? If it fails, what does GetLastError say? If it succeeds, what is going wrong?

  5. #5
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i can't debug it. i just found out, that the error occurs, before the program is started. before main() is entered, the program fails.
    are there any pragmas for DLL directories? because it seems quite preprocessoric...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    DLL is not a compiler thing, it's a linker thing, and linker never sees source code.

    I don't really know what error you're getting, or why we're fiddling with LoadLibrary, but I would suggest poking around your project settings, especially in the linker section.

  7. #7
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i'm using this
    Code:
    #pragma comment(lib, "SDL.lib")
    in order to link the .lib file with my project. that seems to be why the executable dependencies are set to eg. SDL.dll. that must be why it tries to load them in MainCRTStartup (i guess).
    so how do i link the .lib files without creating the dependencies to the dlls?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you want to link with SDL.lib or with SDL.dll? I would be surprised if the answer was "both". (And did you specify the correct directory in the default lib path?)

  9. #9
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i guess you can only link .lib files, right?
    i put the .lib files in either the executable directory or the dll directory, none works. the linker still sets the DLL dependency so loading the dlls has no effect as they have to be there, before the executable code loads...

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Unless they've changed the conventions when I wasn't looking, .lib = static linking and .dll = dynamic linking. Generally you don't link a library both static and dynamic at the same time.

    It wouldn't surprise me if neither of those directories are in the default path the compiler looks for when including libraries. Either fix your path or provide the full path in your #pragma (or, if you've got LoadLibrary, just take the #pragma out, which is what I'd try first if it were me).

  11. #11
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    i can't remove the pragma, because it then sais "unresolved external symbol". so the .lib file is somehow essential...

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    If you are using a lib file (probably makes sense for a dll for SDL) you dont need LoadLibrary.

    Have a look at the second link I gave earlier paying attention to the dll search orders. if you dont want the dll in the exe folder or the system folders, then try set the PATH variable to the folder you want

  13. #13
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    the PATH variable is worh an idea, but has to be set up before the application starts. and that also makes it less dynamic... any chance on using LoadLibrary without the .lib files?

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Yes, but you will then have to get function pointers to every function you need.

    Best bet - Use a system directory, put it in the exe folder or manually set the PATH

  15. #15
    Registered User Devils Child's Avatar
    Join Date
    Jan 2008
    Posts
    244
    ok. i guess i'll choose the PATH variant.

    thankx guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Clean Windows Temp folder and the User Temp folder?
    By patrick22 in forum Windows Programming
    Replies: 11
    Last Post: 01-27-2008, 03:29 PM
  3. deleting a folder AND copying a folder
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2004, 08:48 AM
  4. retrieving current CPU load
    By kristy in forum C Programming
    Replies: 2
    Last Post: 11-30-2003, 08:30 AM
  5. Please help with current time in c++
    By Agnesa in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2002, 07:10 AM

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