Forum > General
[SOLVED] Calling the path of a DLL when it is called by a 3rd party
(1/1)
Gizmo:
I'm aware that for GUI Applications you can use ExtractFilePath(Application.ExeName) to get the full path of where the application is running from.
I'm aware that you can use GetCurrentDir for any application, DLL or exe, to get the full path of where the application\DLL is running from.
But, if you've created a DLL with Freepascal that is called from another propitiatory, commercial, Application that you do not write, what is the best way of calling the full path of where the DLL is being called from in that context? Because, I can't use Application.ExeName in my DLL, because it is not an Application. And if I use GetCurrentDir in my DLL, it gives me the full path of where the commercial application is running from and not the DLL itself, e.g. c:\Program Files\SomeProgram\ProgramName.exe
So for example, lets assume the DLL and the commercial exe are in the following locations :
C:\Users\Gizmo\Documents\UsefulDLLs\MyDLL.dll
C:\Program Files\SomeProgram\TheProgram.exe
I need to call the path of where MyDLL.dll is running from, i.e. I need the return string to be C:\Users\Gizmo\Documents\UsefulDLLs\
I seem to be a bit stuck.
440bx:
Use GetModuleFilename passing it the dll's HMODULE.
Gizmo:
Perfect! You're a star. Thank you.
For the benefit of others :
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function GetModuleName : ansistring; // https://stackoverflow.com/questions/2043/can-a-windows-dll-retrieve-its-own-filename var szDLLName: array[0..MAX_PATH] of Char; begin FillChar(szDLLName, SizeOf(szDLLName), #0); GetModuleFileName(hInstance, szDLLName, MAX_PATH); Result := szDLLName; end; // DLL calls the path of itself, which gives you the path where your other libraries are stored alongsidePATH_OF_DLL := GetModuleName;// Now strip out the DLL name from the resulting value, leaving the path, and append the library filename that you need to itFULL_LIB_PATH := ExtractFilePath(PATH_OF_DLL) + 'LibraryName.dll';// Now check the library existsif FileExists(FULL_LIB_PATH) then ...
Remy Lebeau:
--- Quote from: Gizmo on June 11, 2021, 12:46:54 pm ---I'm aware that you can use GetCurrentDir for any application, DLL or exe, to get the full path of where the application\DLL is running from.
--- End quote ---
GetCurrentDir() returns the calling process's current working directory, which can change dynamically during the process's lifetime, and as such at any given time it may or may not be (usually not) the directory where the EXE/DLL is located.
--- Quote from: Gizmo on June 11, 2021, 12:46:54 pm ---And if I use GetCurrentDir in my DLL, it gives me the full path of where the commercial application is running from and not the DLL itself, e.g. c:\Program Files\SomeProgram\ProgramName.exe
--- End quote ---
It is not not guaranteed to return the EXE's path.
Navigation
[0] Message Index