Board index » cppbuilder » Cannot attach a DLL's component as a child of an EXE's component

Cannot attach a DLL's component as a child of an EXE's component

I run an EXE which has a TPanel.
The EXE loads a plug-in DLL at run time.
The DLL creates a TButton.

I want to attach the TButton to the EXE's TPanel at run time.

In my DLL, I have a method like :

extern "C" bool __declspec(dllexport) Initialize(TPanel* panel)
{
TButton* button = new TButton(panel);
button->Parent = panel;
...

Quote
}

At run time, the application crashes with the error "Cannot assign a TFont
to a TFont".
This happens when the button's Parent property is set with the panel.

Help!! :-)

 

Re:Cannot attach a DLL's component as a child of an EXE's component


you can't do that if your not using RTL and Run time packages.
because the DLL and EXE is using 2 different RTL's
 that means 2 different memory handlers.
 the best you may be able to do is set the parentWindow..
which is window Handle.
Quote
Voxen wrote:
> I run an EXE which has a TPanel.
> The EXE loads a plug-in DLL at run time.
> The DLL creates a TButton.

> I want to attach the TButton to the EXE's TPanel at run time.

> In my DLL, I have a method like :

> extern "C" bool __declspec(dllexport) Initialize(TPanel* panel)
> {
> TButton* button = new TButton(panel);
> button->Parent = panel;
> ...
> }

> At run time, the application crashes with the error "Cannot assign a TFont
> to a TFont".
> This happens when the button's Parent property is set with the panel.

> Help!! :-)

Re:Cannot attach a DLL's component as a child of an EXE's component


The VCL is not designed to pass over the DLL boundary like you are trying to
do.  One of the related problems in doing so is what is causing the problem
you are seeing - the RTTI (runtime type information) inside a DLL is not the
same as the RTTI inside an application.  So in this particular case, a TFont
inside a DLL is not recognizable as a valid TFont by the exectuable, and
vice versa.  Thus they can't be assigned to each other directly, thus the
error you are seeing.

If you need to pass VCL parameters to DLL functions then you should use a
package instead of a DLL.  A package is essentially a special VCL-aware DLL.

The alternative is to not pass VCL parameters directly to begin with.  In
this particular case, you could change your function to simply pass the
panel's HWND handle instead.  Also note that TWinControl derived components
have a second constructor that takes a HWND instead of a TComponent for
situations when the parent window is a non-VCL window, ie:

    extern "C" bool __declspec(dllexport) Initialize(HWND hwnd)
    {
        TButton* button = new TButton(hwnd);
        ...
    }

Gambit

Quote
"Voxen" <vo...@free.fr> wrote in message

news:3e23f09a$1@newsgroups.borland.com...
Quote
> At run time, the application crashes with the error "Cannot
> assign a TFont to a TFont". This happens when the button's
> Parent property is set with the panel.

Re:Cannot attach a DLL's component as a child of an EXE's component


Quote
> If you need to pass VCL parameters to DLL functions then you should use a
> package instead of a DLL.  A package is essentially a special VCL-aware

DLL.

The DLL or package must not be linked to the EXE through an import lib. It's
more like a a plugin.

In that case, can I threat the package as a regular DLL (LoadLibrary(),
GetProcAddress(), etc) and still have VCL parameters work as intended, or do
I need to use LoadPackage() and any other VCL methods to call the package's
methods?

I can't find detailled help on using dynamically loaded packages.

Re:Cannot attach a DLL's component as a child of an EXE's component


When using the HWND contructor, you do not need to set the ParentWindow
yourself.  It is set in the constructor for you.

However, I notice that TFrame does not expose the HWND constructor like
other TWinControl decendants do.  It may not be possible to use TFrame like
you are trying to then, I'm not sure.

It is issues like this that are exactly why I hate using the VCL in DLLs,
and as such never actually do.

I would seriously suggest that you look into changing your DLL into a
Package instead in order to gain the VCL support.  If that still doesn't
work, then I'm out of any more ideas right now short ofr re-thinking your
design.

Gambit

Quote
"Voxen" <vo...@free.fr> wrote in message

news:3e2537a0@newsgroups.borland.com...
Quote
>     TFrame* frame = new TFrame(hwnd);
>     frame->ParentWindow = hwnd;

Re:Cannot attach a DLL's component as a child of an EXE's component


Packages must be loaded with LoadPackage() instead of LoadLibrary(), and
freed with UnloadPackage() instead of FreeLibrary().  GetProcAddress()
should still work fine, though.

Gambit

Quote
"Voxen" <vo...@free.fr> wrote in message

news:3e253fa5@newsgroups.borland.com...
Quote
> In that case, can I threat the package as a regular DLL (LoadLibrary(),
> GetProcAddress(), etc) and still have VCL parameters work as intended,
> or do I need to use LoadPackage() and any other VCL methods to call
> the package's methods?

Other Threads