* * *

Author Topic: C# Wrapper for Free Pascal / Lazarus library classes & controls  (Read 999 times)

brian71us

  • Newbie
  • Posts: 4
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  [Select]
  1. unit ExportedFunctions;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Dialogs, fmKeypad, fmSample;
  9.  
  10. procedure ShowForm; stdcall;
  11. procedure SimpleMessage; stdcall;
  12.  
  13. implementation
  14.  
  15. procedure SimpleMessage; stdcall;
  16. begin
  17.   ShowMessage('Yay!');
  18. end;
  19.  
  20.  
  21. procedure ShowForm; stdcall;
  22. var
  23.   Form: TForm1;
  24. begin
  25.   Form := TForm1.Create(nil);
  26.  
  27.   try
  28.     Form.Show;
  29.   finally
  30.     Form.Free;
  31.   end;
  32. end;
  33.  
  34. exports
  35.   ShowForm,
  36.   SimpleMessage;
  37.  
  38. end.
  39.  

Here's the C# wrapper.

Code: C  [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Tester
  8. {
  9.     public class BTEK_FPL
  10.     {
  11.         [DllImport("BTEK_FPL.dll", CallingConvention = CallingConvention.StdCall)]
  12.         public static extern void ShowForm();
  13.         [DllImport("BTEK_FPL.dll", CallingConvention = CallingConvention.StdCall)]
  14.         public static extern void SimpleMessage();
  15.     }
  16. }
  17.  

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

  • Hero Member
  • *****
  • Posts: 516
    • My private Site
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #1 on: July 04, 2017, 09:40:42 pm »
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  [Select]
  1. library BTEK_FPL;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   { you can add units after this }
  8.   interfaces,
  9.   forms,
  10.   ExportedFunctions;
  11.  
  12. begin
  13.   Application.Initialize;
  14. end.

Unit-Code:
Code: Pascal  [Select]
  1. unit ExportedFunctions;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Dialogs;
  9.  
  10. procedure ShowForm; stdcall;
  11. procedure SimpleMessage; stdcall;
  12.  
  13. implementation
  14.  
  15. uses Forms,Buttons,Controls;
  16.  
  17. procedure SimpleMessage; stdcall;
  18. begin
  19.   ShowMessage('Yay!');
  20. end;
  21.  
  22.  
  23. procedure ShowForm; stdcall;
  24. var
  25.   Form: TForm;
  26.   lButton: TBitBtn;
  27. begin
  28.   Form := TForm.Create(nil);
  29.   lButton := TBitBtn.Create(Form);
  30.   with lButton do
  31.   begin
  32.     Parent := Form;
  33.     height := 56;
  34.     Align:=alBottom;
  35.     Kind:=bkClose;
  36.     BorderSpacing.Around:=6;
  37.   end;
  38.   try
  39.     Form.ShowModal;
  40.   finally
  41.     Form.Free;
  42.   end;
  43. end;
  44.  
  45. var
  46.   Form: TForm;
  47.  
  48. procedure ShowForm2(const App:TApplication); stdcall;
  49. var
  50.   lButton: TBitBtn;
  51. begin
  52.   if not assigned(form) or (Form.Owner <> App) then
  53.     begin
  54.     app.CreateForm(TForm,Form);
  55.   lButton := TBitBtn.Create(Form);
  56.   with lButton do
  57.   begin
  58.     Parent := Form;
  59.     height := 56;
  60.     Align:=alBottom;
  61.     Kind:=bkClose;
  62.     BorderSpacing.Around:=6;
  63.   end;
  64.    end;
  65.   form.show;
  66. end;
  67.  
  68. exports
  69.   ShowForm,
  70.   ShowForm2,
  71.   SimpleMessage;
  72.  
  73. end.
Works quite well ...
« Last Edit: July 05, 2017, 12:59:04 am by jc99 »
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.2 - 1.6.4, 1.8rc3
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are

brian71us

  • Newbie
  • Posts: 4
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #2 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. :)

Phil

  • Hero Member
  • *****
  • Posts: 2302
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #3 on: July 05, 2017, 12:37:29 am »
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!

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

  • Hero Member
  • *****
  • Posts: 516
    • My private Site
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #4 on: July 05, 2017, 01:11:32 am »

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. :)
try that :
Code: Pascal  [Select]
  1. library BTEK_FPL;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   { you can add units after this }
  8.   interfaces,
  9.   forms, fmKeypad, fmSample,
  10.   ExportedFunctions;
  11.  
  12. begin
  13.   Application.Initialize; {<<--- that one !!}
  14. end.

and showmodal instead of show !
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.2 - 1.6.4, 1.8rc3
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are

brian71us

  • Newbie
  • Posts: 4
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #5 on: July 05, 2017, 02:44:33 am »

Thanks, that worked perfectly
 ;D

brian71us

  • Newbie
  • Posts: 4
Re: C# Wrapper for Free Pascal / Lazarus library classes & controls
« Reply #6 on: July 05, 2017, 02:51:13 am »
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

There are several reasons that I'm looking to go down this path:
1. The .NET CF lacks a lot of the standard features and controls including a masked edit control.
2. The .NET CF / WinCE appear to leak memory... even opening / closing a blank form consumes memory that is never released. Having a screen full of controls is slowly causing my apps to crash. Hoping the LCL won't do the same.
3. We get a lot of complaints about the speed of the device and applications. I hope that the LCL will be faster.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus