Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to call a form that is in a DLL  from an application?

Posted on 2016-08-11
13
Medium Priority
?
136 Views
Last Modified: 2016-08-15
Hello guys,

Today my delphi application is 120 mb of exe, I think it is very big exe file.
I would like to split many of forms to DLLs, so that I can reduce the exe file.

I have some doubts:

how to call a form from my exe application?
is the same way to call a form from a DLL too?

Do you have any example to show me how to?

regards
Alexandre
0
Comment
Question by:hidrau
  • 7
  • 4
  • 2
13 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41752849
a big exe

or lots of smaller dll's and a smaller exe, probably totalling to more than 120mb
and a bit of dll-hell

in essence, deploying 1 exe is way simpler than deploying multiple files

have you tried looking what's causing the large size ?
allowing multiple themes or skins is usually 1 such item, which doesn't add to the functionality
0
 
LVL 1

Author Comment

by:hidrau
ID: 41752879
I have 259 forms in my project :(
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 400 total points
ID: 41752947
any items in common amongst those forms or a group of forms ?

usually it's possible to use visual form inheritance to bundle common items
descending forms can have very little code to add / alter functionality

another possibity is to use frames
for same pieces of functionality spread across different forms
1
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:hidrau
ID: 41753008
I see. Thanks a lot for some ideas,

But the solution to me at this moment is DLL, split my application into some DLL files.

I know how to create a DLL, but my problem is how to call a form that is in a DLL from my application.

If you can do a simple example so that I can study it and go ahead, it would be very interesting to me.
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 41753069
Look at my old answer here on EE to similar question...
1
 
LVL 1

Author Comment

by:hidrau
ID: 41753265
Hello Sinisa,

It is really very simple, I made the example and worked.

I followed this : http://www.delphi-central.com/formdll.aspx

But I need to load my form Explicit and I didn't have success :(

I also followed this link:

http://www.drbob42.com/delphi/headconv.htm

this code in my application where I call the DLL that has the form:

procedure Tform1.Button5Click(sender: Tobject);
Var
  TcadCliente : procedure; 
  Handle : THandle;
Begin 
  Handle := LoadLibrary('DllFuncoes.dll');
  If handle <> 0 Then 
  Begin 
    Tcadcliente := GetProcAddress(Handle, 'ChamaCadCliente');
    FreeLibrary(Handle)
  End 
End

Open in new window


What is wrong? The form doesn't appear on the screen.

If I call the form Implicit, it works fine :(  - The first example is an implicit method.

Thanks
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41753406
call FreeLibrary when you no longer need to call the dll
Your Tcadcliente address will only be valid until FreeLibrary
The address  TcadCliente will call the dll ...

load the dll's at the beginning of the program or when you need it for the first time
the first can lead to longer start time, the latter to some coding overhead
unload the used library before closing the application, usually in mainform destroy or finalization

it might be best to start with ModalDialogs
like this:

procedure ShowUserInfo(List: TStrings);
var frm: TfrmUserinfo;
begin
  frm := TfrmUserInfo.Create(nil);
  try
    frm.LabelLastName.Caption := List.Values['LAST_NAME'];
    frm.LabelFirstName.Caption := List.Values['LAST_NAME'];
    // etc ... 
    frm.ShowModal;
  finally
    frm.Free;
  end;
end;

Open in new window


btw above sample also implements expicitly creating and freeing a form
1
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 1600 total points
ID: 41753431
you forget to call your form before FreeLibrary:
procedure Tform1.Button5Click(sender: Tobject);
Var
  TcadCliente : procedure; 
  Handle : THandle;
Begin 
  Handle := LoadLibrary('DllFuncoes.dll');
  If handle <> 0 Then 
  Begin 
    Tcadcliente := GetProcAddress(Handle, 'ChamaCadCliente');
    if Assigned(Tcadcliente) then
       Tcadcliente();
    FreeLibrary(Handle)
  End 
End

Open in new window

1
 
LVL 1

Author Closing Comment

by:hidrau
ID: 41753761
Thanks a lot
0
 
LVL 1

Author Comment

by:hidrau
ID: 41754012
Sinisa Vuk,

I am getting an error :(

It is strange because in Delphi 7 I don't receive the error, only with Delpi XE7.

The application compiled perfect, when I call the application and click on the button to call the form that is in my DLL workes fine so far, the error happens when I close the form.

Something that also forgot?

please, download my application study

http://www.teachmenow.com.br/instalador/font.rar

thanks
0
 
LVL 1

Author Comment

by:hidrau
ID: 41754052
After breathing a little bit :) I think I found the problem.

In my dll I wasn't distroying the form with free, I was using release

it was:
form := Tform1.create(nil)
Try
   form.showmodal
Finally
  form.release
  form := nil
End

it worked fine when I made the changes

form := Tform1.create(nil)
Try
   form.showmodal
Finally
  form.free
  form := nil
End

If you can take a look at my example, you gonna see
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41754426
Release is to be called in an event handler
like inside an onclick

it add's a message on the queue of the form : WM_RELEASE
and waits to processs it

in a finally, there is no point in using release

setting form := nil depends if you use local variables or not
my sample in ShowUserInfo doesn't require a frm:= nil as it is a local procedure

if you have global or variables in the implementation section, that's when you might have to use nil
that's usually when you only want a single instance of a form
1
 
LVL 1

Author Comment

by:hidrau
ID: 41756679
thanks for all the comments
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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