![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 3
| Loading DLL with specific path 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 |
| doublenach is offline | |
| | #2 |
| Super Moderator Join Date: Aug 2001
Posts: 7,813
| 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
__________________ If you aim at everything you will hit something but you won't know what it is. Last edited by Bubba; 02-08-2010 at 11:54 PM. |
| Bubba is offline | |
| | #3 | ||||
| Registered User Join Date: Jan 2010
Posts: 230
| Quote:
Quote:
Quote:
Quote:
Isn't the use of regsvr32 to store a registry mapping between COM/ActiveX GUIDs and their respective dll location? | ||||
| _Mike is offline | |
| | #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 |
| doublenach is offline | |
| | #5 |
| the hat of redundancy hat Join Date: Aug 2001 Location: Hannover, Germany
Posts: 2,769
| 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. |
| nvoigt is offline | |
| | #6 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,763
| Quote:
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot | |
| brewbuck is offline | |
| | #7 | |
| Registered User Join Date: Jan 2010
Posts: 230
| Quote:
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; | |
| _Mike is offline | |
| | #8 | ||
| Super Moderator Join Date: Aug 2001
Posts: 7,813
| Quote:
Quote:
__________________ If you aim at everything you will hit something but you won't know what it is. | ||
| Bubba is offline | |
| | #9 |
| Registered User Join Date: Jan 2010
Posts: 230
| Ah okey, then I misunderstood your post. I thought you meant that you cannot use absolute paths. Sorry. |
| _Mike is offline | |
| | #10 |
| I have a hat!? Join Date: Apr 2008
Posts: 178
| If you need more debugging info than just GetLastError() for failed dll loads, here's how you get some internal debug spew from Windows. |
| adeyblue is offline | |
| | #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 |
| doublenach is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Loading Struct from dll | peyman_k | C++ Programming | 2 | 08-15-2008 03:12 PM |
| Dll Injection Question | zenox | C Programming | 13 | 03-15-2008 10:54 AM |
| Loading bitmap in dll | Mithoric | Windows Programming | 2 | 12-22-2003 01:53 PM |
| Using class with DLL | greg2 | C++ Programming | 2 | 09-12-2003 05:24 AM |
| Exporting Object Hierarchies from a DLL | andy668 | C++ Programming | 0 | 10-20-2001 01:26 PM |