Solved

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

Posted on 2016-08-11
13
58 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 36

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 36

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 100 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
 
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 25

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 36

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 25

Accepted Solution

by:
Sinisa Vuk earned 400 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 36

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

15 Experts available now in Live!

Get 1:1 Help Now