Thread: Loading DLL with specific path

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    Loading DLL with specific path

    Hi all,

    I have a C project in which I need to load a dll, using LoadLibrary() I am able to load it if the dll is on the executable folder or in the system32 folder, but I cannot load it from a specific path. In my case, my app will use a dll located in Program Files\Common Files and LoadLibrary doesn't load when I write the full path.

    How do I do this?


    Regards,
    Rafael

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    LoadLibrary() will fail if the path is invalid. If it is not loading the DLL then it cannot find it. The DLL link process starts in the same folder as your EXE. If the DLL cannot be found there then Windows looks in the System32 folder. If it cannot be found there a message box will popup stating it could not find a component necessary to run your EXE.

    Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded. If LoadLibrary() returns a NULL HMODULE then you must call GetLastError() to get the error information. The Platform SDK explains this in great detail.

    http://msdn.microsoft.com/en-us/libr...75(VS.85).aspx
    Last edited by VirtualAce; 02-08-2010 at 11:54 PM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by doublenach View Post
    In my case, my app will use a dll located in Program Files\Common Files and LoadLibrary doesn't load when I write the full path.
    Sounds like the path is incorrect or your application doesn't have read access to that directory. What does GetLastError() say?

    Quote Originally Posted by Bubba View Post
    The DLL link process starts in the same folder as your EXE. If the DLL cannot be found there then Windows looks in the System32 folder. If it cannot be found there a message box will popup stating it could not find a component necessary to run your EXE.
    Unless you specify an absolute path. Even says so in the link you posted:
    If the string specifies a full path, the function searches only that path for the module.

    Quote Originally Posted by Bubba View Post
    Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded.
    Not true.
    Isn't the use of regsvr32 to store a registry mapping between COM/ActiveX GUIDs and their respective dll location?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Hi,

    thanks for the reply, as suggested I tried GetLastError and I received 'The specified module could not be found.'

    My code looks like this:
    LoadLibrary("C:\Program Files\Common Files\example.dll");

    The dll is there, if I copy/paste into the file browser there it is. I am thinking it is the syntax, how should it be? I really searched this but I can't find any example on microsoft website and also couldn't find any other code example on the internet that loads a dll from a specific path.

    Regards,
    Rafael

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to use double backslashes, as always:

    LoadLibrary("C:\\Program Files\\Common Files\\example.dll");

    Please note that your code will only run on machines that are installed in English. "Program Files" is localized in XP
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bubba View Post
    Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded. If LoadLibrary() returns a NULL HMODULE then you must call GetLastError() to get the error information. The Platform SDK explains this in great detail.
    That's untrue -- DLLs do not need to be registered. regsvr32 is normally used to register and unregister DLLs which provide system-level services, such as COM or ActiveX components. If you don't believe me, try it -- create a simple DLL project which exports a function, build the DLL, and call LoadLibrary() on it. It works fine.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by nvoigt View Post
    You need to use double backslashes, as always:

    LoadLibrary("C:\\Program Files\\Common Files\\example.dll");

    Please note that your code will only run on machines that are installed in English. "Program Files" is localized in XP
    Indeed, and so is "Common Files". And it also depends on if you're on a 32 or 64-bit OS.

    If you want version-independant code, use the %CommonProgramFiles% environment variable together with ExpandEnvironmentStrings Function (Windows)
    Example:
    Code:
    TCHAR *path = L"%CommonProgramFiles%\\microsoft shared\\Windows Live\\WindowsLiveLogin.dll";
    TCHAR *expandedPath = NULL;
    // Set length to 0 the first pass to find out required buffer length
    DWORD numChars = ExpandEnvironmentStrings(path, expandedPath , 0);
    expandedPath = new TCHAR[numChars];
    ExpandEnvironmentStrings(path, expandedPath, numChars);
    HMODULE hm = LoadLibrary(expandedPath);
    delete [] expandedPath;
    (proof of concept code, add your own error handling)

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That's untrue -- DLLs do not need to be registered.
    Interesting. I actually ran into this problem recently and regsvr32 fixed it. But again I am probably making a bad assumption that the DLL is a COM or ActiveX object since that is what I am used to. But yeah it would not make sense to force registration of every DLL. My apologies.

    Unless you specify an absolute path. Even says so in the link you posted:
    Which is why I posted the link. There is a lot of good information there about the issue. Personally I never specify absolute paths so have never ran into this particular problem while using LoadLibrary().

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Bubba View Post
    Which is why I posted the link. There is a lot of good information there about the issue. Personally I never specify absolute paths so have never ran into this particular problem while using LoadLibrary().
    Ah okey, then I misunderstood your post. I thought you meant that you cannot use absolute paths. Sorry.

  10. #10
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    If you need more debugging info than just GetLastError() for failed dll loads, here's how you get some internal debug spew from Windows.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Thank you all very much, I am now able to load the DLL using double backslashes.


    Rafael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading Struct from dll
    By peyman_k in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2008, 03:12 PM
  2. Dll Injection Question
    By zenox in forum C Programming
    Replies: 13
    Last Post: 03-15-2008, 10:54 AM
  3. Loading bitmap in dll
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 12-22-2003, 01:53 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM