Gu.
Gu.

Reputation: 1957

Application.handle from DLL

Delphi. How from DLL to learn Handle the appendix which has caused this DLL?

That is necessary: Knowing Handle appendices, I wish in Dll to use this Handle at creation of dialogues, because dialogue created as TOpendialog.Create(nil) sometimes it appears under the main window of the basic form. And so, in DLL I would make:

application.handle:=GetExeHandle; // GetExeHandle - How to learn?
Opendialog1:=TOpendialog.Create(application);
...

So it is correct?

Upvotes: 1

Views: 4657

Answers (2)

Viktor Svub
Viktor Svub

Reputation: 1469

Unless you use runtime packages (and you don't, or not the right ones), you are in for a world of pain.

Your library will have its' own copy not only of (T)Application but also of thread sycnhronization queue and event (and everything else).

What you are trying to do can seem to work, but it may (and it will) break anytime cause any complex dialog, regardless if VCL or WinAPI, does its' own message pumping, which will bypass the applications' idle and synchronization handling, resulting in reentrancy issues and random stalls or deadlocks.

You may try to handle a lot of the cases by copying the applications' handles, events etc. to the DLL's globals upon its' initialization (is I tried to do), but (not only) if you use anything like TApplication or TThread in the DLL, it will break sometimes.

You can avoid these problems if you use the right BPL runtime packages in your app and the library, as they will share the same namespace and globals as the application using them.

Upvotes: 4

Rob Kennedy
Rob Kennedy

Reputation: 163277

The only time your DLL shows a dialog box is when the host application calls a function from your DLL. Include the parent window handle as one of the function's input parameters so that the EXE can tell you which handle to use. Do not attempt to discover the handle yourself. As a library developer, you cannot guess what the host application is doing.

If you don't want to include the handle on every function call, then add an initialization function that users of the DLL need to call before any other functions. Pass the handle in the initialization and then store it in a variable in your DLL so that other functions can use the value when they need it.

Upvotes: 10

Related Questions