Difference between revisions of "Creating LCL Control From Libraries"

From Lazarus wiki
Jump to navigationJump to search
(code highlighting)
Line 7: Line 7:
 
== Examples ==
 
== Examples ==
 
=== Usage example 1 (library) ===
 
=== Usage example 1 (library) ===
<pre>procedure init; stdcall;
+
<delphi>procedure init; stdcall;
 
var myBtn:TRHandle;
 
var myBtn:TRHandle;
 
begin
 
begin
Line 14: Line 14:
 
end;
 
end;
  
exports init;</pre>
+
exports init;</delphi>
  
 
=== Usage example 2 (library) ===
 
=== Usage example 2 (library) ===
<pre>procedure init(RApplication,RParent:TRHandle); stdcall;
+
<delphi>procedure init(RApplication,RParent:TRHandle); stdcall;
 
var winCtrl:TRWinControl;
 
var winCtrl:TRWinControl;
 
begin
 
begin
Line 28: Line 28:
 
end;
 
end;
  
exports init;</pre>
+
exports init;</delphi>
  
 
=== Usage example 3 (application) ===
 
=== Usage example 3 (application) ===
<pre>type
+
<delphi>type
 
   TDllInit1=procedure(intr:TRWinControlInterface); stdcall;
 
   TDllInit1=procedure(intr:TRWinControlInterface); stdcall;
 
   TDllInit2=procedure(intr:TRWinControlInterface; Appl,Parnt:TRHandle); stdcall;
 
   TDllInit2=procedure(intr:TRWinControlInterface; Appl,Parnt:TRHandle); stdcall;
Line 54: Line 54:
 
   init2(FillRWinControlInterface,TRHandle(Application),TRHandle(Panel1));
 
   init2(FillRWinControlInterface,TRHandle(Application),TRHandle(Panel1));
 
   FreeLibrary(lib);
 
   FreeLibrary(lib);
end.
+
end.</delphi>
</pre>
 
  
  
Line 66: Line 65:
 
** This probably is slower then just using PChars but it allows passing any type of data up to 2Gb.
 
** This probably is slower then just using PChars but it allows passing any type of data up to 2Gb.
 
To make it easy to switch, just type this switch  
 
To make it easy to switch, just type this switch  
  {$DEFINE USE_BIN_STR}
+
<delphi>{$DEFINE USE_BIN_STR}</delphi>
 
on the very top of your "rwincontrol" unit.
 
on the very top of your "rwincontrol" unit.
 
If this switch is set, binary strings support is enabled.
 
If this switch is set, binary strings support is enabled.
Line 78: Line 77:
 
Actually, it's not just LCL but any other class you want. You just have to register your class application-side and provide a suitable wrapper library-side (though it is not required).
 
Actually, it's not just LCL but any other class you want. You just have to register your class application-side and provide a suitable wrapper library-side (though it is not required).
 
To register a class, put something like the following code in the initialization section of your program:
 
To register a class, put something like the following code in the initialization section of your program:
<pre>RegisterClass(TMyClass);</pre>
+
<delphi>RegisterClass(TMyClass);</delphi>
  
  

Revision as of 18:25, 13 August 2011

About

Due to several problems, LCL components and ansistrings cannot be passed between a library (dll/so/dynlib) and an application (exe/app).

This interface provides a simple API between the library and the application to create LCL UI controls application-side, however controlled by dummies library-side.


Examples

Usage example 1 (library)

<delphi>procedure init; stdcall; var myBtn:TRHandle; begin

 myBtn:=RCreate(StrToRstr('TButton'));
 RSetStrProp(myBtn,StrToRstr('Caption'),StrToRstr('Hello world!'));

end;

exports init;</delphi>

Usage example 2 (library)

<delphi>procedure init(RApplication,RParent:TRHandle); stdcall; var winCtrl:TRWinControl; begin

 winCtrl:=TRWinControl.Create(RApplication);
 winCtrl.Left:=20;
 winCtrl.Top:=10;
 winCtrl.Width:=100;
 winCtrl.Height:=24;
 winCtrl.Parent:=RParent;

end;

exports init;</delphi>

Usage example 3 (application)

<delphi>type

 TDllInit1=procedure(intr:TRWinControlInterface); stdcall;
 TDllInit2=procedure(intr:TRWinControlInterface; Appl,Parnt:TRHandle); stdcall;

var

 lib:TLibHandle;
 init1:TDllInit1;
 init2:TDllInit2;

begin

 RegisterClass(TButton);
 RegisterClass(TWinControl);
 // library example 1
 lib:=LoadLibrary('library1.'+SharedSuffix);
 init1:=TDllInit(GetProcAddress(lib,'init'));
 init1(FillRWinControlInterface);
 FreeLibrary(lib);
 // library example 2
 lib:=LoadLibrary('library2.'+SharedSuffix);
 init2:=TDllInit(GetProcAddress(lib,'init'));
 init2(FillRWinControlInterface,TRHandle(Application),TRHandle(Panel1));
 FreeLibrary(lib);

end.</delphi>


Technical details

Passing strings

Strings cannot be usually passed between a library and an application. Since this system requires passing strings, a solution has been included:

  • You can either use simple PChars
    • Fast but not suitable for binary data (especially containing null characters)
  • Or use the binary string system
    • This probably is slower then just using PChars but it allows passing any type of data up to 2Gb.

To make it easy to switch, just type this switch <delphi>{$DEFINE USE_BIN_STR}</delphi> on the very top of your "rwincontrol" unit. If this switch is set, binary strings support is enabled. If not, the more conventional PChars are used.

How does it work?

A set of functions in the application are passed to the library as pointers in a record (TRWinControlInterface). Each time a library needs to change or create a control these functions are called. They basically make use of RTTI to provide this functionality.

What type of LCL controls?

Actually, it's not just LCL but any other class you want. You just have to register your class application-side and provide a suitable wrapper library-side (though it is not required). To register a class, put something like the following code in the initialization section of your program: <delphi>RegisterClass(TMyClass);</delphi>


FAQ/Troubleshoot

I get "class not found" errors

You have to register that class application-side. See #What type of LCL controls?

Where do I download the sources?

Test Demo (incomplete)

View Source Code

Copyright

Devised and created by Christian Sciberras, with the help of the following people from #lazarus-ide IRC (alphabetical):

  • Andreas '\pub\bash0r' Schneider
  • Dmitry 'skalogryz' Boyarintsev
  • Marc 'giantm' Weustink
  • Vincent 'fpcfan' Snijders


License

© 2009 Covac Software. You may copy, store, modify and sell this software/source as long as you keep copyright comments in the source code intact.