Recent

Author Topic: [SOLVED] Calling the path of a DLL when it is called by a 3rd party  (Read 2644 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
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.
« Last Edit: June 11, 2021, 03:28:23 pm by Gizmo »

440bx

  • Hero Member
  • *****
  • Posts: 4014
Re: Calling the path of a DLL when it is called by a 3rd party
« Reply #1 on: June 11, 2021, 12:54:30 pm »
Use GetModuleFilename passing it the dll's HMODULE.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: [SOLVED] Calling the path of a DLL when it is called by a 3rd party
« Reply #2 on: June 11, 2021, 03:30:50 pm »
Perfect! You're a star. Thank you.

For the benefit of others :

Code: Pascal  [Select][+][-]
  1. function GetModuleName : ansistring;   // https://stackoverflow.com/questions/2043/can-a-windows-dll-retrieve-its-own-filename
  2.   var
  3.     szDLLName: array[0..MAX_PATH] of Char;
  4.   begin
  5.     FillChar(szDLLName, SizeOf(szDLLName), #0);
  6.     GetModuleFileName(hInstance, szDLLName, MAX_PATH);
  7.     Result := szDLLName;
  8.   end;  
  9.  
  10. // DLL calls the path of itself, which gives you the path where your other libraries are stored alongside
  11. PATH_OF_DLL := GetModuleName;
  12. // Now strip out the DLL name from the resulting value, leaving the path, and append the library filename that you need to it
  13. FULL_LIB_PATH := ExtractFilePath(PATH_OF_DLL) + 'LibraryName.dll';
  14. // Now check the library exists
  15. if FileExists(FULL_LIB_PATH) then ...                                
  16.  
« Last Edit: June 11, 2021, 03:33:14 pm by Gizmo »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: [SOLVED] Calling the path of a DLL when it is called by a 3rd party
« Reply #3 on: June 12, 2021, 12:32:22 am »
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.

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.

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

It is not not guaranteed to return the EXE's path.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018