Reputation: 4752
If I call LoadLibrary("foo.dll")
, it will look in various locations on the system, choose the best match, and load that library. I would like to find the full path to this file before actually loading it. Is there a simple way to do this? My google-fu has thus far failed me, but it seems intuitive there is some function to do this. My current solution is to actually call LoadLibraryEx
with the LOAD_LIBRARY_AS_DATAFILE
flag, then GetModuleFileName
on the result; after this I assume I would have to release the library and reload it without this flag, but this seems like a roundabout method. Is there a better way? ResolvePathFromDllName
perhaps?
Upvotes: 2
Views: 1590
Reputation: 125688
You can use SearchPath
, but if you're planning on actually calling LoadLibrary
later it's not recommended (see the Remarks
section on the linked page (and quoted below in this answer) regarding possibly returning the wrong results).
Note also that the search order is slightly different than that of LoadLibrary
, because it doesn't make the same presumptions at the start of the search that are documented in LoadLibrary
. It strictly searches the PATH
as its documentation says it does; it doesn't automatically look first in the same folders. See the paragraph in Remarks
:
The SearchPath function is not recommended as a method of locating a .dll file if the intended use of the output is in a call to the LoadLibrary function. This can result in locating the wrong .dll file because the search order of the SearchPath function differs from the search order used by the LoadLibrary function. If you need to locate and load a .dll file, use the LoadLibrary function.
So the proper answer to this question would be to use LoadLibrary
in the first place (as you are) to make sure you're finding the same DLL you'll be loading later.
Upvotes: 2