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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.