Unable to Register Class TFMXApplicationDelegate - macos

I've read the Embarcadero and other docs, searched the net, and obviously something isn't sinking in. I have a significantly more complex Application and DLL/Dylib, but am now just using this simple example to try and get it to work. When I build the App and DLL for Win32 it works fine and it works fine if I do not make a function call to the DLL. As soon as I make a call to the DLL the error Unable to register Class TFMXApplicationDelegate is raised and the application terminates. This behaviour is the same whether the guest OS is on a VM (parallels) or a physical device (MacBook Pro 15 mid-2015).
QUESTION: How can I ensure that TFMXApplicationDelegate gets Registered, is there a setting or permission I need to set. It seems fairly basic since, according to the Apple documentation:
The app delegate is effectively the root object of your app.
The DPR:
Library pTestDLL;
uses
uTestDLL in 'uTestDLL.pas';
{$R *.res}
end.
And here's the simple PAS file (for the Dylib):
unit uTestDLL;
interface
uses
FMX.Dialogs;
// External functions and procedures
{$IFDEF MSWINDOWS}
function say_Hello(Hello: string): boolean; stdcall; forward;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string): boolean; cdecl; forward;
{$ENDIF MACOS}
exports
{$IFDEF MSWINDOWS}
say_Hello;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
_say_Hello;
{$ENDIF MACOS}
Implementation
{$IFDEF MSWINDOWS}
function say_Hello(Hello: string): boolean; stdcall;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string): boolean; cdecl;
{$ENDIF MACOS}
begin
Result := True;
showmessage('In DLL: ' + Hello);
end;
end.
And lastly the simple test application:
unit uDylibTest1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation;
const
// Windows DLL Names
{$IFDEF MSWINDOWS}
TestDLL = 'pTestDLL.dll';
{$ENDIF MSWINDOWS}
// macOS DYLIB Names
{$IFDEF MACOS}
TestDLL = 'libpTestDLL.dylib';
{$ENDIF MACOS}
type
TfDylibTest = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TuDylibTest = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
{$IFDEF MSWINDOWS}
function say_Hello(Hello: string): boolean; stdcall; external TestDLL Delayed;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
function _say_Hello(Hello: string): boolean; cdecl; external TestDLL;
{$ENDIF MACOS}
var
fDylibTest: TfDylibTest;
implementation
{$R *.fmx}
procedure TfDylibTest.Button1Click(Sender: TObject);
var
b:boolean;
begin
showmessage('B4 function call);
b := False;
// Call the DLL Function
{$IFDEF MSWINDOWS}
b := say_Hello('The string passed to the DLL') then
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
b := _say_Hello('The string passed to the DLL');
{$ENDIF MACOS}
if b then
showmessage('Say Hello OK')
else
showmessage('Say Hello Failed');
end;
procedure TfDylibTest.FormCreate(Sender: TObject);
begin
showmessage('onCreate');
end;
procedure TfDylibTest.FormShow(Sender: TObject);
begin
showmessage('onShow');
end;
end.

I'm answering tis on behalf of Alexander Brazda López who posted this on the G+ Delphi forum (so I'm not taking credit for someone else's answer):
ShowMessage() use FMX.Dialogs inside a dll. I remember there were some
limitations about this and use bpl instead dll was recomended. I would
try to find some information about this. Just for check, try to use a
reversestring function for test open and calling dll functions its
working.
I cannot comment WRT the veracity of the next part of the response, but to be fair will include it. However the bottom line was that with FMX.Dialogs or FMX.Forms in the uses clause, I got the error. Remove those and the TFMXApplicationDelegate error did not occur.
In the non-trivial 'real' example, I was using the unit for showmessage and for application.processmessages which was in a cross platform function called WaitNoFreeze() which, as the name suggests, puts a wait in the main thread, but does not stop all other activity like sleep().
The problem is not that the dylib is not found, this can be checked by
calling a basic function in the dylib as function sum (a, b: integer):
integer
The problem is when we invoke the UI through some function or use
embedded resources such as forms. This is because if we do not use
bpl, local instances are created instead of shared global instances,
this prevents functions such as RegisterClass or GetMem from sharing
the data. In the case of FMX there are differences due to the api for
the example that Kevin has exposed, in Windows the Handle of the
window is not necessary, so 0 is a valid value, in MacOSX it is
necessary but the value in the dynlib is NULL since it has not been
started, although in the application, but because the FMX instances
are not shared, it is not possible to access this information. It is
the same thing that happens when we try to free a block of memory in a
dll in windows that has been allocated by the application, that is why
we must use a bpl so that the memory manager of the application and
the dll is the same and maintain the same instances regardless of
whether their methods are invoked from the dll or from the exe

Related

how to eliminate extraneous console output after ctrl-c in Windows?

When pressing Ctrl-c there is almost always additional output. I'd like to ensure that after receiving the Ctrl-c the program doesn't show anything other than possibly "^C".
I found what is mostly the same question but, it was for Linux and my attempts to "port" the solution from Linux to Windows have not succeeded.
At this point, I'm out of things to try and can use some help, which I will definitely appreciate. Thank you.
The short example program below suffers from that problem.
{$APPTYPE CONSOLE}
program _SetConsoleCtrlHandler;
uses
Windows,
SysUtils
;
function CtrlHandler(CtrlType : DWORD) : BOOL; stdcall;
begin
result := FALSE;
case CtrlType of
CTRL_C_EVENT,
CTRL_BREAK_EVENT:
begin
result := TRUE;
ExitProcess(7);
end;
end;
end;
var
s : shortstring;
begin
SetConsoleCtrlHandler(#CtrlHandler, TRUE);
while TRUE do
begin
write('press <ctrl-c> to end this program : ');
readln(s);
end;
end.
The way I usually do this is to have a separate unit that is signaled and a simple wait, like the following. In the main console project you call WaitForCtrlC instead of Readln(). You could also use a TEvent and wait on the event instead of looping, like I show in this example:
uses
{$IFDEF LINUX}
Posix.Signal,
{$ENDIF}
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
SysUtils;
procedure WaitForCtrlC;
implementation
var
Control_C: Boolean = False;
{$IFDEF MSWINDOWS}
function ConsoleCtrlHandler(dwCtrlType: DWORD): BOOL; stdcall;
begin
if (dwCtrlType = CTRL_C_EVENT) then
Control_C := True;
Result := True;
end;
{$ENDIF}
{$IFDEF LINUX}
var
sigIntHandler: sigaction_t;
procedure SigHandler(SigNum: Integer); cdecl;
begin
Control_C := True;
end;
{$ENDIF}
procedure WaitForCtrlC;
begin
while not Control_C do
Sleep(25);
end;
initialization
{$IFDEF MSWINDOWS}
Windows.SetConsoleCtrlHandler(#ConsoleCtrlHandler, True);
{$ENDIF}
{$IFDEF LINUX}
sigIntHandler._u.sa_handler := #SigHandler;
sigemptyset(sigIntHandler.sa_mask);
sigIntHandler.sa_flags := 0;
sigaction(SIGINT, #sigIntHandler, nil);
{$ENDIF}

Trouble with displaying RTF-Text between Delphi XE8 and Delphi 7

I have trouble displaying RTF-Formatted text between two delphi-Versions.
The TDBRichEdit-Control and TRichEdit-Control seem to have trouble parsing the RTF-text supplied.
Sometimes it seems as if to get it to work in XE8 you simply have to connect a TDBRichEdit to the appropriate Field in the Dataset, but the same Method would mess up the Delphi 7 Version.
So we figured we handle this via Code.
Basically we replaced the TDBRichEdits with normal TRichEdits and
supply the RTF through a stream.
textstream:=TStringStream.Create(CDS.FieldByName('TEXT').AsString);
try
RichEditFAPLAN.Lines.Clear();
RichEditFAPLAN.Lines.LoadFromStream(textStream);
finally
textstream.Free();
end;
this worked fine until about 1h ago when I decided to move the code above into it's own globally available function.
procedure StreamRichTextTo(ARichEdit:TCustomRichEdit; ADataSet:TDataSet; AFieldName:String);
var
ws:WideString;
Stream:TStringStream;
begin
ARichEdit.Lines.Clear();
if (ADataSet=nil) or (ADataSet.FindField(AFieldName)=nil) or (ADataSet.FieldByName(AFieldName).IsNull) then exit;
ws:=UTF8Decode(ADataSet.FieldByName(AFieldName).AsString);
Stream:=TStringStream.Create(ws);
try
Stream.Position:=0;
ARichEdit.Lines.LoadFromStream(Stream);
finally
Stream.Free();
end;
end;
I tried different Variations of this code. With MemoryStream, Widestring, UTF8 Encoding, AnsiString and what not.
procedure StreamRichTextTo(ARichEdit:TCustomRichEdit; ADataSet:TDataSet; AFieldName:String);
var
{$IFDEF VER150}
ws:WideString;
Stream:TStringStream;
{$ELSE}
s:AnsiString;
//Stream:TMemoryStream;
Stream:TStringStream;
{$ENDIF}
begin
ARichEdit.Lines.Clear();
if (ADataSet=nil) or (ADataSet.FindField(AFieldName)=nil) or (ADataSet.FieldByName(AFieldName).IsNull) then exit;
{$IFDEF VER150}
ws:=UTF8Decode(ADataSet.FieldByName(AFieldName).AsString);
Stream:=TStringStream.Create(ws);
{$ELSE}
{
s:=ADataSet.FieldByName(AFieldName).AsAnsiString;
Stream:=TMemoryStream.Create();
Stream.Clear();
Stream.Write(PAnsiChar(s)^,length(s));
}
s:=ADataSet.FieldByName(AFieldName).AsAnsiString;
Stream:=TStringStream.Create(s,TEncoding.UTF8);
//Stream.WriteString(s);
Stream.Position:=0;
{$ENDIF}
try
ARichEdit.Lines.LoadFromStream(Stream);
finally
Stream.Free();
end;
end;
All to no avail, the XE-RTF-Text always comes out looking either like this or as a simple '':
{\rtf1\fbidis\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 arial;}{\f1\fswiss\fprq2\fcharset0 Arial;}{\f2\fnil arial;}}
\viewkind4\uc1\pard\ltrpar\lang1031\f0\fs20 Gie\'dftemp.: \tab 1350 - 1370 \'b0C
\par \ul\f1 Form fest verklammern und belasten
\par \ulnone\f0
\par \tab\tab\f2
\par }
What's more, the original Code with the "textstream", that I commented back in, is also not working anymore.
What I'm looking for is a code-solution that can properly handle RTF-Text between different IDE-Versions.
EDIT:
Here is a sample Project-Code.
The thing is, everything works fine in this project.
I have no idea why the same code does no longer work in our software and I can not reproduce it.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Data.DB,
Datasnap.DBClient;
type
TForm1 = class(TForm)
REGoal: TRichEdit;
Button1: TButton;
CDS: TClientDataSet;
RESource: TRichEdit;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure StreamRichTextTo(ARichEdit:TRichEdit; ADataSet:TDataSet; AFieldName:String);
var
Stream:TStringStream;
//{$IFDEF VER150}
ws:WideString;
//{$ELSE}
//{$ENDIF}
begin
ARichEdit.Lines.Clear();
if (ADataSet=nil) or (ADataSet.FindField(AFieldName)=nil) or (ADataSet.FieldByName(AFieldName).IsNull) then exit;
try
//{$IFDEF VER150}
ws:=UTF8Decode(ADataSet.FieldByName(AFieldName).AsString);
Stream:=TStringStream.Create(ws);
//{$ELSE}
// Stream:=TStringStream.Create(ADataSet.FieldByName(AFieldName).AsString);
//{$ENDIF}
ARichEdit.Lines.LoadFromStream(Stream);
finally
Stream.Free();
end;
end;
(* Copy this to RESource
{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 arial;}{\f1\fnil arial;}}
\viewkind4\uc1\pard\lang1031\f0\fs20 Erste Form ist eine Zulegekontrolle durchzuf\fchren.
\par
\par
\par \f1
\par }
*)
procedure TForm1.Button1Click(Sender: TObject);
var
Stream:TStringStream;
begin
if CDS.FindField('TEXT')=nil then
begin
CDS.FieldDefs.Add('TEXT',ftWideString,4096);
CDS.CreateDataSet();
end;
CDS.Edit();
CDS.FieldByName('TEXT').AsString:=RESource.text;
CDS.Post();
StreamRichTextTo(REGoal,CDS,'TEXT');
end;
Just Create a new Form1
Add 2 TRichEdits, one is called RESource and the other REGoal
Add a TClientDataSet and call it CDS
Add A Button
Assign the OnClick-procedure to the button
I'll try creating the project in D7 and port it to XE8 next, maybe this will reproduce the effect.
EDIT 2:
Creating the Project in Delphi 7 and then Opening it in XE8 produces the same result.
My guess is, that there is something happening when the Database-Value is assigned to a String-Variable (or passed directly into the stream) which would be why I'm unable to reproduce the error.
Also maybe the Database is at fault.
it's a Firebird 3.0 Database with a VarChar-Field
This IS the working Version of the code.
Apparently there was something wrong with the RTF-Code itself due to a bug in our Editor
procedure StreamRichTextTo(ARichEdit:TRichEdit; ADataSet:TDataSet; AFieldName:String);
var
{$IFDEF VER150}
ws:WideString;
{$ELSE}
ws:String;
{$ENDIF}
Stream:TStringStream;
begin
ARichEdit.Lines.Clear();
if (ADataSet=nil) or (ADataSet.FindField(AFieldName)=nil) or (ADataSet.FieldByName(AFieldName).IsNull) then exit;
{$IFDEF VER150}
ws:=UTF8Decode(ADataSet.FieldByName(AFieldName).AsString);
{$ELSE}
ws:=ADataSet.FieldByName(AFieldName).AsString;
{$ENDIF}
Stream:=TStringStream.Create(ws);
try
ARichEdit.Lines.LoadFromStream(Stream);
finally
Stream.Free();
end;
end;

True free heap not what it should be after ShellExecute

I have a simple Application that reads data from an .ini file into a record, and then does a ShellExecute. However, when I run it in debug mode,it tells me that the True free heap isn't what it should be:
"True heap size: 688128, True free heap: 688016, Should be: 688128"
This only happens if I actually run the ShellExecute (however even if I don't run the ShellExecute, I get a True free heap of 688016, but no complaint that it should be 688128), so I wonder if I either need to free the PChar() conversion (which shouldn't be the case from all that I've read), or the handle returned by ShellExecute (though CloseHandle(ShellHandle); doesn't change the message), or if it's intended behavior?
The IniSettings.SetupCommand in question is an .msi file that triggers a UAC prompt, but I really just want to make this a Fire & Forget thing since my Lazarus app doesn't care what it launches.
For reference, here's my whole Unit. I'm using Lazarus 1.6.2, FPC 3.0.0, i386-win32, and I'm targeting Windows only with this. My Debug Settings are at the very bottom, after the code.
unit MainUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls,
Graphics, Dialogs, StdCtrls, IniFiles, Windows;
type
{ IniSettings }
TAutorunIniSettings = record
AutorunTitle: AnsiString;
SetupCommand: AnsiString;
end;
{ TMainForm }
TMainForm = class(TForm)
btnSetup: TButton;
lblTitle: TLabel;
procedure btnSetupClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ private declarations }
IniSettings: TAutorunIniSettings;
public
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
var
AutorunIni: TIniFile;
begin
try
AutorunIni := TIniFile.Create('Autorun.ini');
IniSettings.AutorunTitle := AutorunIni.ReadString('Autorun', 'Title', 'Autorun');
IniSettings.SetupCommand := AutorunIni.ReadString('Autorun', 'Setup', '');
self.Caption := IniSettings.AutorunTitle;
finally
if(AutorunIni <> nil) then AutorunIni.Free;
end;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
end;
procedure TMainForm.btnSetupClick(Sender: TObject);
begin
ShellExecute(0, 'open', PChar(IniSettings.SetupCommand), nil, nil, SW_SHOWNORMAL);
end;
end.

Delphi DLL Issues - wrong integer value recevied and access violation issue

I'm having trouble getting to grips with DLLs in Delphi 7. I have two problems:
1) The procedure takes an integer parameter - but the dll receives a different value to the one I pass.
2) The application that called the dll crashes with an access violation after the function completes.
Here's my dll code:
library apmDLL;
uses
Classes, Messages, Windows, Dialogs, sysutils ;
const
WM_MY_MESSAGE = WM_USER + 1;
procedure sendtoACRPM (functionKey : integer); stdcall;
begin
showmessage('You sent - '+inttostr(functionKey));
showmessage('Finished Now');
end;
exports sendtoACRPM;
end.
So when I call this with the code below I get:
'Sending - 1'
'You Sent - 1636532'
'Finished Now'
Then the calling application crashes with an access violation.
The calling application looks like this:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, shlobj, shellapi;
const
WM_MY_MESSAGE = WM_USER + 1;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure sendtoACRPM (functionKey : integer) ; external 'apmDLL.dll';
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var
myInt: integer;
begin
myInt := strtoint(edit1.text);
showmessage('Sending - ' + inttostr(myInt));
sendtoACRPM(myInt);
end;
end.
Any ideas what I'm doing wrong here?
You need stdcall both in the DLL and in the calling code declaration. You only have it in the DLL.
Calling conventions need to match on both sides. :-)
procedure sendtoACRPM (functionKey : integer); stdcall; external 'apmDLL.dll';
You should use the standard Windows MessageBox instead of ShowMessage, so that the DLL can be used from non-Delphi applications as well.

Invoke methods in Pascal

I am in front of following problem:
My Main programming Language is C++ with the Qt4 Library, but now I have to write a Pascal Wrapper, which should give the possibility to use the functions of a C DLL in Pascal.
Now I want to make it possible to invoke a method from any Pointer. But I can't find a Pascal method to invoke a method. I want something like the QMetaObject::invokeMethod method in QT. I got following code:
unit CgPConnect;
//{$mode objfpc}{$H+}
{$mode delphi}
interface
uses
Classes, SysUtils, dynlibs;
type
Callback = Record
var callbackObject: Pointer;
var objectFunction: string;
end;
CallbackObject = Record
var objectName: string;
var callback: Callback;
end;
MutableObject = Object
var name: string;
var state: string;
var properties: array of VariantMap;
var annotations: array of VariantMap;
end;
PConnect = Class
constructor create(connectorPath: string);
destructor destroy;
private
var hostactionCallbacks: array of CallbackObject;
var mConnectorPath: string;
var mConnectorLibrary: TLibHandle;
function loadConnectorLibrary: Boolean;
public
procedure registerCallbackForHostaction(objectName, objectFunction: string; callbackObject: pointer);
procedure callHostactionCallback(receivedObject :MutableObject);
var mLibraryLoaded: Boolean;
end;
implementation
constructor PConnect.create(connectorPath: string);
begin
mConnectorPath:= connectorPath;
mLibraryLoaded:= false;
//Eventuell noch slash hinzufügen
mLibraryLoaded:= loadConnectorLibrary;
end;
destructor PConnect.destroy;
begin
UnloadLibrary(mConnectorLibrary);
end;
procedure PConnect.registerCallbackForHostaction(objectName, objectFunction: string; callbackObject: pointer);
var c: Callback;
var callbackCount: integer;
begin
if mLibraryLoaded = true then
begin
c.callbackObject:= callbackObject;
c.objectFunction:= objectFunction;
callbackCount:= Length(hostactionCallbacks)+1;
SetLength(hostactionCallbacks, callbackCount);
hostactionCallbacks[callbackCount].objectName:= objectName;
hostactionCallbacks[callbackCount].callback:= c;
end;
end;
procedure PConnect.callHostactionCallback(receivedObject :MutableObject);
var receivedObjectName, objectFunction: string;
var i, count: integer;
var callbackObject: pointer;
begin
if mLibraryLoaded = true then
begin
receivedObjectName:= receivedObject.name;
count:= Length(hostactionCallbacks);
for i:=0 to count do
begin
if hostactionCallbacks[i].objectName = receivedObjectName
begin
objectFunction:= hostactionCallbacks[i].callback.objectFunction;
callbackObject:= hostactionCallbacks[i].callback.callbackObject;
if callbackObject <> 0 then
//INVOKE METHOD (objectFunction) OF OBJECT (callbackObject)
end;
end;
end;
end;
end.
I would be happy about a fast answer :)
You cannot directly and portably call a C++ method from Pascal. If your callbackfunction is a C++ object, forget it.
Otherwise fill a TMethod object and cast that to the proper "procedure of object" declaration. Don't forget the calling convention.
For more bizarre solutions you might want to have a look at (RemObjects') Pascalscript.
P.s. this is the same that you can't even call a C++ method reliably from another C++ compiler. It is not Pascal vs C++ per se.

Resources