Solved

Forms in DLL's

Posted on 2000-05-05
11
254 Views
Last Modified: 2010-04-04
Hi. I want to create a form using a different version of Delphi from which I'm using to develope the main application. Imagine, for example, I have an application developed in Delphi 4 and I want to develope a single form in Delphi5 in order to use some features that D4 doesn't have.

I have heard that you can turn a form into a DLL and then call that DLL from your program, and it will behave as if the form was really in your program. But I also need my 'DLL form' to use forms in the main program.

Can this be done? How can I do it?

Please explain it to me detailed.

Thanks in advance,

QUIQUE.
0
Comment
Question by:quique
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 1

Author Comment

by:quique
Comment Utility
I will explain better how I want the forms to comunicate:

Imagine in the main program there is a DataModule. Then I need to use tables defined in that DataModule in the 'DLL form' also!

QUIQUE.
0
 
LVL 1

Expert Comment

by:sxfo
Comment Utility
This may be more work than it's worth. YOu will need to carry around both the core sets of VCL code. I would imagine that any functionality that is new in D5 would need the base classes of D5 and will not function with the base classes of D4.

Have fun
0
 
LVL 1

Expert Comment

by:msedi
Comment Utility
Hello quiqui,

yes, there's a possibility to put the form in a dll, but you can't have access to the properties in a normal way. I will explain three methods that could be helpful:

1. Put the form in all DLL and then export a function the shows and creates the form. There you have no access to the forms' properties and function unless you export them explicitly as seperate functions. That obviously not what you want.

2. Another way is to create an ActiveForm. There you have full access to all properties and methods. The problem here is, that the dll has to be registered and you cannot load the dll with the LoadLibrary mechanism. In this case you have to use the COM functinality which is (in your case) rather easy to handle. Keep in mind, that you always have to register you ActiveForm DLL's!!!!

3. The third possibility would be: You create a DLL with an exported functions like the first point. But there the function won't show and create the form, but will create the form and export the pointer to the form. Then in your main application you can cast the pointer to the form and you can use the form like always.

Maybe this helps,
Good luck,
Martin
0
 
LVL 1

Author Comment

by:quique
Comment Utility
Hi, msedi.

I would like you to explain me the third option further.

Please, show me, above all, how I can export the pointer to the form, because I have no idea on how to do it!

Thanks in advance,

QUIQUE.
0
 
LVL 1

Author Comment

by:quique
Comment Utility
Please, give me a more detailed answer as I ask you in my previous comment.

Thanks,

QUIQUE.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
There are two easier ways:

1. Pass the application object to the DLL, like this:

var
  OldApp : TApplication;

Procedure SetApplication(Appl : TApplication);
begin
  OldApp := Application;
  Application := Appl;
end;

Then just create and show forms as normal.

2. (Easier still)
   Turn on packages by going to Project Options|Packages and checking "Build with runtime packages.

  Do this for both your app and the dll. Build both and away you go!

Cheers,

Raymond.
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Oh, if you pass the application object to the DLL you must set the oldapp back before you destroy the DLL or you will get exceptions.

Cheers,

Raymond.
0
 
LVL 1

Author Comment

by:quique
Comment Utility
I prefer msedi's solution. I am waiting to his answer.

QUIQUE.
0
 
LVL 1

Expert Comment

by:msedi
Comment Utility
Hello,

sorry that it took so long. But I was at university and my email forward didn't work. What I mean is, that you can create an abstract class which you use in your main form. For example:

type
  TTest = class(TObject);
 ...
   procedure Beep(); abstract;
  end;


Then you create your DLL and fill this abstract inherited class with your code an export one function that creates the object and returns the pointer to this object

in the dll:

write the class code here

interface
type
  TTest2 = class(TTest)
    procedure Beep(); override;
  end;

implementation
  TTest2.Beep();
  begin
    MessageBeep(MB_OK);
  end;


and create a function that creates and gives the object as result:

function CreateObject () : TTest;
begin
  Result := TTest.Create(nil);
end;


Then use this function in the main form:

....
var
  form : TTest2;

begin
  form := CreateObject();

  form.Beep();
  form.Free();
end;

I don't know exactly whether this works or not, but is almost the same functinality that COM does except that the DLL can be use with almost every programming language for Windows,

PS: If you have any questions, please mail to msedi@freenet.de until my forward works or I will look in this newsgroup. Unfortunately it is not possible to enter two emails in this group.


Good luck, Martin





0
 
LVL 1

Accepted Solution

by:
msedi earned 155 total points
Comment Utility
Hello,

sorry that it took so long. But I was at university and my email forward didn't work. What I mean is, that you can create an abstract class which you use in your main form. For example:

type
  TTest = class(TObject);
 ...
   procedure Beep(); abstract;
  end;


Then you create your DLL and fill this abstract inherited class with your code an export one function that creates the object and returns the pointer to this object

in the dll:

write the class code here

interface
type
  TTest2 = class(TTest)
    procedure Beep(); override;
  end;

implementation
  TTest2.Beep();
  begin
    MessageBeep(MB_OK);
  end;


and create a function that creates and gives the object as result:

function CreateObject () : TTest;
begin
  Result := TTest.Create(nil);
end;


Then use this function in the main form:

....
var
  form : TTest2;

begin
  form := CreateObject();

  form.Beep();
  form.Free();
end;

I don't know exactly whether this works or not, but is almost the same functinality that COM does except that the DLL can be use with almost every programming language for Windows,

PS: If you have any questions, please mail to msedi@freenet.de until my forward works or I will look in this newsgroup. Unfortunately it is not possible to enter two emails in this group.


Good luck, Martin





0
 
LVL 1

Author Comment

by:quique
Comment Utility
I have solved it myself. I have created a new type with a procedure of object in order to pass a procedure as argument to a function and it works.

Also, I had to include "Sharemem" in uses list because I was using String as parameters between the DLL and the main program.

Thanks for your support,

QUIQUE.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now