Solved

Problems in integrating VCL & FireMonkey via a FM DLL

Posted on 2014-09-12
4
634 Views
Last Modified: 2014-09-14
I have written a Fire Monkey (mobile phone) application that also runs on Windows. So far so good.

Although it is not recommended by Embarcadero, I also have been able to compile the Fire Monkey into a VCL project. They don't interact, apart from the VCL calling the main frame of the Fire Monkey app, and then receiving back control when all is complete.

It is a little ugly at the user interface end, in that the Fire Monkey app also appears on the windows task bar, even when it is not running.

Also when I tested it on Vista I get an exception on closing the VCL.

Therefore, following Embarcadero's recommendation, I have written a DLL interface that embodies the Fire Monkey component. However I am doing something wrong, because as soon as I carry out a task in a sub-unit referencing the main form I get a data exception.

library LangDLL;

uses
  FMX.Forms,
  System.SysUtils,
  System.Classes,
  uMMain in 'uMMain.pas',
  uMAccess in 'uMAccess.pas',
  uUnique in 'uUnique.pas';

{$R *.res}

procedure ShowMobile; stdcall export;
var
  MM: TfMMain;
  test: string;
begin
  test := 'OK';
try
  MM := TfMMain.Create(application);
  MM.FormActivate(application);
  MM.Free;
except
  test := 'Failed';
end;
end;
exports
  ShowMobile;
begin
end.

Open in new window


It passes through the above code without a worry, and in the main part of the App it works OK.

I have a couple of units that are called by the program. When the logic flows to those units in the DLL style operation the form fMMain is now nil to them. (It is OK when it runs as a stand-alone program.)

The simple work-around would be eliminate those units, but why is fMMain = nil in this situation?
0
Comment
Question by:GrahamDLovell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40321261
fMMain  is defined as TfMMain in uMMain unit? Why do you mix fmx/vcl code?
0
 

Author Comment

by:GrahamDLovell
ID: 40321370
I just gave the form the name fMMain (form, mobile, main)

The default for a new form in fmx is Form1. The IDE adds the T prefix for a type. The same pattern is followed here.

How have I mixed fmx / vcl code?
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40322113
I build small example with some success:

Dll part  (delph->new library):
library test1;

uses
  FMX.Forms,
  System.SysUtils,
  System.Classes,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

procedure ShowMobile; stdcall;
var
  MM: TForm1;
  test: string;
begin
  test := 'OK';
try
  MM := TForm1.Create(nil);
  try
    MM.ShowModal;
  finally
    MM.Free;
  end;
except
  test := 'Failed';
end;
end;
exports
  ShowMobile;

begin

end.

Open in new window


...calling part:
uses ..., Unit3;

procedure TForm2.Button1Click(Sender: TObject);
begin
  StartForm;
end;

Open in new window


... unit3 where dll is loading dynamically ... and main thing here is to not call FreeLibrary by yourself:
unit Unit4;

interface

uses Winapi.Windows, System.SysUtils, Vcl.Forms;

type
  //user dll
  TFMXDllForm = procedure; stdcall;

function StartForm: Boolean;

var
  hDLLHandle: Cardinal = 0;  //global dll handle for reusing....

implementation

function StartForm: Boolean;
var
  sFileName: String;
  frm: TFMXDllForm;
begin
  Result := False;
  sFileName:=IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))+'test1.dll';

  if hDLLHandle = 0 then //reuse dll handle
    hDLLHandle:=LoadLibrary(PChar(sFileName));
  if hDLLHandle <> 0 then
  begin
    try
      frm:=GetProcAddress(hDLLHandle, 'ShowMobile');

      if Assigned(frm) then
      begin
        frm;  //show dll form
        Result := True;
      end;
    except
    end;
  end;
end;

initialization

finalization
  //if hDLLHandle <> 0 then //do not free by own
  //  FreeLibrary(hDLLHandle);

end.

Open in new window

0
 

Author Closing Comment

by:GrahamDLovell
ID: 40322152
Brilliant. Works a charm.

For anyone else who tries this, here are some other things I learnt about FireMonkey DLLs:

This process instantiates the FireMonkey form, and so you have to do everything inside the form (no calls to functions or procedures that are not part of that form). - that is the answer to my original question.

Make sure you don't double reference the form, e.g. in a function TfMMain.GetList, don't reference a value as "fMMain.NewList", just use "NewList". While the former works in a standalone FireMonkey app, the DLL doesn't like it.

I also had to abandon using TFileStream in the DLL, but that is a matter for another question (but not from me, since I just used TClientDataSet.LoadFromFile instead).
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Setting up Idhttp indy component to use a proxy server 2 1,710
Suppress UAC Prompt using shellapi 6 494
MAPI Error1: User cancelled request ? 7 1,234
detect keyboard input monitoring 3 401
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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