views:

205

answers:

4

I'm was trying to write a dll library in Delphi wih a function that creates an instance of a TFrame descendant and returns it. But when I imported this function in an application, every time I called it I would get an exception like "the 'xxx' control has no parent window". I'm not 100% sure, but the exception appeared in the constructor of that class when any of GUI controls was accessed.

Could you please tell me what the reason of that behaviour is? Should I just use TForm descendants instead or is there a better solution?

Thank you!

A: 

Sounds like you simply need to assign the component (a form or part of a form, like a panel) that holds the frame to theframe.parent.

You cannot do GUI work before it is assigned. Frames are parts of forms for reuse, and normally need to assign some parent to them.

Move the GUI code to onshow or a procedure you call explicitely, so that the calling code can assign parent.

Or make the parent a parameter in the function.

Marco van de Voort
Upon second-reading your answer (I'm a bit word blind), it seems your answer is missing a bit after the "normally need to"...
Jeroen Pluimers
fixed. Writing in a hurry is always a problem for me. Working on all sentences at once :)
Marco van de Voort
+1  A: 

There are a few important things to remember:

  1. When using DLLs, both your DLL and your EXE each have an Application instance that are struggling for control. The Controls in your DLL will see the Application instance that belongs to the DLL; the Controls in your EXE will see the Application instance that belongs to the EXE. That struggle is not there when using packages, as then there will only be one Application instance.
  2. Frames are Controls, but they are not Forms.
  3. When using Controls in an application, they cannot visually exist without a parent Control (usually a Form or a container that has a parent hierarchy towards a Form).
  4. Some Controls cannot expose their full functionality unless they exist visually and have a valid parent.

Try to reproduce your problem inside the EXE; if you cannot reproduce, it is probably the first thing in the above list.

--jeroen

Jeroen Pluimers
+2  A: 

About the error

That error message is raised from the Controls.pas unit, from the TWinControl.CreateWnd method. Essentially that code is used to create the Window handle for your TWinControl descendant (TFrame, TButton, TEdit... if it can have keyboard focus it's an TWinControl descendant), and it's actually an very sensible error message: You can't have a Window without an WindowParent, and since we're talking about the VCL here, it makes a lot of sense to try and get the parent window handle from TWinControl.Parent; And that's not assigned.

That's not WHY the error message is popping up. You get to see that error message because some of the code you're using to set up the frame requires an Window handle for some operation. It could be anything, like setting the Caption of some component (that internally requires an window handle do to some calculation). I personally really hate it when that happens. When I create GUI's from code I try to delay the assignment of Parent as much as possible, in an attempt to delay the creation of the window, so I got bitten by this many times.

Specific to your DLL usage, possible fix

I'm going to put my psycho mind reader hat on. Since you need to return a FRAME from your DLL, and you can't return the actual Frame because that's an Delphi-specific object and you're not allowed to return Delphi-specific objects over DLL boundaries, my guess is you're returning an Window Handle, as all the nice API's do, using a function definition like this:

function GiveMeTheNiceFrame:HWND;

The trouble is, that routine requires the creation of the actual Window Handle, by a call to TWinControl.CreateWnd, and in turn that call requires an parent window handle to set up the call to Windows.CreateWindowEx, and the routine can't get an parent window handle, so it errors out.

Try replacing your function with something allong the lines of:

function GiveMeTheNiceFrame(OwnerWindow:HWND):HWND;
begin
  Result := TMyNiceFrame.CreateParanted(OwnerWindow).Handle;
end;

... ie: use the CreateParented(AParentWindow:HWND) constructor, not the usual Create(AOwner:TComponent) and pass an owner HWND to your DLL.

Cosmin Prund
Thanks for the answer. I think it should help; I'll try that later. BTW, let me ask you a question. Why is it not allowed to return a Delphi specific object over DLLs? I'm going to use this library with Delphi applications only. The function returns the TFrame type but the actual object created is an instance of a descendant class. Isn't it OK? Thanks!
Mariusz
It's not OK for versioning reasons: Using the returned object requires knowledge of it's memory layout, and that's subject to change (it can change with Delphi's version). It will work when you test it, and it will work if the Exe and Dll are built with the same compiler and same compiler settings, but if you're going to impose such restrictions you might as well use packages.
Cosmin Prund
Thanks, it's goot to know that. I did what you suggested and I'm still getting the exception :-(. The exported function does this: Result := TFrame1.CreateParented(ParentWindowHandle); And ParentWindowHandle is a handle to the main window of an app that calls the method (Self.Handle, when called from the main window class).
Mariusz
Debug further. With your DLL in the editor, go to Run -> Load Process and load the EXE that uses your DLL. Do whatever you need with the EXE to make it load your DLL and call your function. The IDE will stop on the error, look at the call stack, find the offending code and make sure that Parent is assigned.
Cosmin Prund
The correct way to debug the DLL is to specify the Host Application (the exe) in Project Options and then do RUN. The Host Application will be started and when the error happens you'd be able to debug as you normally do.
Cosmin Prund
This is the line that raises an exception (a call stack item): ":00511884 TWinControl.CreateWnd + $AC". The parent of the frame is nil, even though the ParentWindow is non-zero... I think I'm giving up. I'll just use TForm :-). Thanks for your commitment anyway!
Mariusz
I started my answer with the ` About the error ` section, telling you the error pops up in exactly the place you're saying it pops up: TWinControl.CreateWnd; You need to look into your call stack, call by call, until you find **your own code**: that's what needs fixing.
Cosmin Prund
Thank you for devoting your time!
Mariusz
A: 

Here you have some solutions and description, why .dll should be replaced with a .bpl :)

Delphi About - seek Delphi form dll

Shreq