HInstance or Filename of DLL

my app dynamically loads a dll. Inside the dll I need to know the HInstance of the dll module. But: the HInstance variable there is the one of the main app which loaded the dll.

There is one way I know of that works (stepping through the LibModuleList and checking the filename of the instances found there), but then I need to hard-code the dll's name. I want to avoid this.

So alternatively my question would be answered if I knew a generic way for a DLL to find out it's own filename.

Things I already tried but failed:
- put code in the initialization section of a unit of the dll to get the filename of the first module in the LibModuleList (which is the last one loaded), but it appears the dll has not yet been registered then.
- get the HInstance of the module that 'owns' a class or address defined or associated with a module in my dll. This also returns the HInstance of the main app.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

MadshiConnect With a Mentor Commented:
You're wrong. "HInstance" is in reality the handle of the module!!!!!

Compile this little dll and load it in your application with LoadLibrary. You will get a message box with this text "c:\project1Path\project.dll".

library Project1;

uses Windows;

{$R *.RES}

var arrCh : array [0..MAX_PATH] of char;
  GetModuleFileName(hInstance, arrCh, MAX_PATH);
  MessageBox(0, arrCh, 'info', 0);

Regards, Madshi.
cadenzaAuthor Commented:
Hi Madshi,

you're absolutely right. I did try exactly this myself before and got the name of the main app!! I have no idea what went wrong then, but more important now is that it works.


All Courses

From novice to tech pro — start learning today.