Forum > Packages and Libraries
C# Wrapper for Free Pascal / Lazarus library classes & controls
brian71us:
Hello,
I am attempting to create C# / .NET wrappers for classes and controls developed with Free Pascal / Lazarus. The target platform is Windows CE, .NET Compact Framework 3.5, and the current version of Lazarus.
I've reviewed the documentation at the link shown below.
http://wiki.freepascal.org/Using_Pascal_Libraries_with_.NET_and_Mono
My first attempt was to simply create a form in Lazarus to be shown in the .NET application. What I am finding is that if I use the ShowMessage then everything is ok. But when I attempt to show my custom form the .NET application shuts down. No error message... it just shots down as if you clicked the close button.
Here's the unit from the Lazarus project:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit ExportedFunctions; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Dialogs, fmKeypad, fmSample; procedure ShowForm; stdcall;procedure SimpleMessage; stdcall; implementation procedure SimpleMessage; stdcall;begin ShowMessage('Yay!');end; procedure ShowForm; stdcall;var Form: TForm1;begin Form := TForm1.Create(nil); try Form.Show; finally Form.Free; end;end; exports ShowForm, SimpleMessage; end.
Here's the C# wrapper.
--- Code: C [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---using System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Linq;using System.Text; namespace Tester{ public class BTEK_FPL { [DllImport("BTEK_FPL.dll", CallingConvention = CallingConvention.StdCall)] public static extern void ShowForm(); [DllImport("BTEK_FPL.dll", CallingConvention = CallingConvention.StdCall)] public static extern void SimpleMessage(); }}
Any thoughts or ideas as to what I might be doing wrong?
Also, if anyone has experience creating C# wrappers for visual controls I would greatly appreciate some assistance!
jc99:
Have you tried to import the DLL into a Lazarus-Project ?
[edit]
... Next time send a whole project ... (as a zip)
the lazarus source doesn't compile (there is no "forms"-unit)
[edit2]
The form only flashes, because you make it visible and the next moment you free (destroy it)
Library code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---library BTEK_FPL; {$mode objfpc}{$H+} uses Classes, { you can add units after this } interfaces, forms, ExportedFunctions; begin Application.Initialize;end.
Unit-Code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit ExportedFunctions; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Dialogs; procedure ShowForm; stdcall;procedure SimpleMessage; stdcall; implementation uses Forms,Buttons,Controls; procedure SimpleMessage; stdcall;begin ShowMessage('Yay!');end; procedure ShowForm; stdcall;var Form: TForm; lButton: TBitBtn;begin Form := TForm.Create(nil); lButton := TBitBtn.Create(Form); with lButton do begin Parent := Form; height := 56; Align:=alBottom; Kind:=bkClose; BorderSpacing.Around:=6; end; try Form.ShowModal; finally Form.Free; end;end; var Form: TForm; procedure ShowForm2(const App:TApplication); stdcall;var lButton: TBitBtn;begin if not assigned(form) or (Form.Owner <> App) then begin app.CreateForm(TForm,Form); lButton := TBitBtn.Create(Form); with lButton do begin Parent := Form; height := 56; Align:=alBottom; Kind:=bkClose; BorderSpacing.Around:=6; end; end; form.show;end; exports ShowForm, ShowForm2, SimpleMessage; end.Works quite well ...
brian71us:
I hadn't noticed that you could attach a file. Thanks!
The form itself is just a blank form with two buttons and not really much going on just to see if this will work. Likewise, for testing, this project targets Win32 and not WinCE which is the eventual goal. The VS 2015 project is also included.
Trying to utilize this library in a Lazarus project did not occur to me but that is a great idea. :)
Phil:
--- Quote from: brian71us on July 04, 2017, 08:05:12 pm ---Any thoughts or ideas as to what I might be doing wrong?
Also, if anyone has experience creating C# wrappers for visual controls I would greatly appreciate some assistance!
--- End quote ---
The wiki article you cite also suggests that if you need a UI, put it in the .NET code, not in the Pascal dynamic library.
In general, I would not put any LCL code in a dynamic library. People have been doing that for years and often have problems. Put non-UI code into the dynamic library and the UI in the calling code, as in the example apps in Part 3 here:
https://macpgmr.github.io/MacXPlatform/PascalDynLibs.html
jc99:
--- Quote from: brian71us on July 04, 2017, 10:06:32 pm ---
I hadn't noticed that you could attach a file. Thanks!
The form itself is just a blank form with two buttons and not really much going on just to see if this will work. Likewise, for testing, this project targets Win32 and not WinCE which is the eventual goal. The VS 2015 project is also included.
Trying to utilize this library in a Lazarus project did not occur to me but that is a great idea. :)
--- End quote ---
try that :
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---library BTEK_FPL; {$mode objfpc}{$H+} uses Classes, { you can add units after this } interfaces, forms, fmKeypad, fmSample, ExportedFunctions; begin Application.Initialize; {<<--- that one !!}end.
and showmodal instead of show !
Navigation
[0] Message Index
[#] Next page