Recent

Author Topic: Create cross-platform library for C++ and Pascal host apps  (Read 1664 times)

Mr. George

  • New Member
  • *
  • Posts: 21
Create cross-platform library for C++ and Pascal host apps
« on: January 21, 2022, 03:13:51 pm »
Hi!
I'm going to create cross-platform library (for Windows and Linux).
Exported library methods should accept and return complicated data structures that will include numbers and UTF-16 strings.

1. I'm not sure, which types i should use for exported library methods in order to provide compatibility
for C++ (or C#) host applications running under windows and Linux. Especially for UTF-16 strings.
I can create some packed record that will use ctypes and Pwidechar for strings.
C++ host application will pass pointer to memory that contain structure, i should be able to use it from pascal library, right?
But how to return complicated result like record with UTF-16 strings from exported pascal library method to C++ host application?

2. What if host application will be another C++ library rather than exe?
In that case, can pascal still return compilated record with utf-16 strings?

Any advices would be highly appreciated.
« Last Edit: January 21, 2022, 03:18:46 pm by Mr. George »

PascalDragon

  • Hero Member
  • *****
  • Posts: 4003
  • Compiler Developer
Re: Create cross-platform library for C++ and Pascal host apps
« Reply #1 on: January 21, 2022, 03:25:34 pm »
1. I'm not sure, which types i should use for exported library methods in order to provide compatibility
for C++ (or C#) host applications running under windows and Linux. Especially for UTF-16 strings.
I can create some packed record that will use ctypes and Pwidechar for strings.

In principle, yes, though you need to be aware that on non-Windows wchar_t is generally 4 Byte and thus UTF-32 instead of 2 Byte and UTF-16. Thus you can't simply return a PWideChar (which is always for UTF-16) and handle that as a wchar_t if you want to support *nix.

C++ host application will pass pointer to memory that contain structure, i should be able to use it from pascal library, right?
But how to return complicated result like record with UTF-16 strings from exported pascal library method to C++ host application?

You need to make sure that the same module that allocated something is the one that frees it. So either the application provides your library with a buffer and size and you complain if the buffer size is too small (preferably returning the new size) or you allocate the buffer yourself and also provide a function for the application to free your buffer again.

2. What if host application will be another C++ library rather than exe?
In that case, can pascal still return compilated record with utf-16 strings?

It doesn't matter if the caller is an executable or a library from the point-of-view of your own library it's the same: it's external code.

Mr. George

  • New Member
  • *
  • Posts: 21
Re: Create cross-platform library for C++ and Pascal host apps
« Reply #2 on: January 21, 2022, 04:34:16 pm »
Thanks!

Quote
You need to make sure that the same module that allocated something is the one that frees it. So either the application provides your library with a buffer and size and you complain if the buffer size is too small (preferably returning the new size) or you allocate the buffer yourself and also provide a function for the application to free your buffer again.
Let me put some example, for simplicity, targeted only for windows (so i use PWideChar).

Code: Pascal  [Select][+][-]
  1. TInputOutputData = packed record
  2.   SomeInt:    cint;
  3.   SomeString: PWideChar; // pointer to null terminated UTF-16 string
  4. end;
  5.  
  6. PTInputOutputData = ^TInputOutputData;
  7.  

For input parameter, C++ app will call my method, pass pointer (without structure size, i already know it from sizeof(TInputOutputData)).
When library method finish execution, C++ app will clean own buffer.

Code: Pascal  [Select][+][-]
  1. procedure ProcCallMeFromC(InputParameter: PTInputOutputData); cdecl;

Now, if i need to return data:

Code: Pascal  [Select][+][-]
  1. function FuncCallMeFromC(OutputParam: PTInputOutputData): cint; cdecl;
  2. begin
  3.   Result := 0; // no errors
  4.   // C++ app already allocated memory for TInputOutputData
  5.   with TInputOutputData(OutputParam^) do
  6.    begin
  7.     SomeInt := 2;
  8.     SomeString := PWideChar('test'); // is it safe?
  9.    end;
  10.   // C++ app will clear own buffer, but will it be able to clear memory for SomeString as well?
  11. end;
« Last Edit: January 21, 2022, 04:41:42 pm by Mr. George »

datiscum

  • New member
  • *
  • Posts: 5
Re: Create cross-platform library for C++ and Pascal host apps
« Reply #3 on: January 21, 2022, 06:01:25 pm »
I would like to give a warning because I have just had a bad experience with CrossPlatform Library and Lazarus. Under Windows it will probably work but under Linux there are some things to consider and others will not work without errors.  I wanted to create a Firebird plugin and basically it worked, but when I unloaded the module the database server crashed with a SIGSEGV.
According to the developers, this problem will not be solved quickly in the future. If you are interested, you can read more here.

https://gitlab.com/freepascal.org/fpc/source/-/issues/39531


Mr. George

  • New Member
  • *
  • Posts: 21
Re: Create cross-platform library for C++ and Pascal host apps
« Reply #4 on: January 21, 2022, 06:32:49 pm »
Thanks. Good to know.
« Last Edit: January 29, 2022, 10:25:03 pm by Mr. George »

 

TinyPortal © 2005-2018