Recent

Author Topic: Using c++ dll in lazarus  (Read 8625 times)

osda

  • Newbie
  • Posts: 6
Using c++ dll in lazarus
« on: February 11, 2019, 02:29:00 pm »
I have a c++ dll for control GPIO, i want to use this on lazarus.But i not found a way to make this.




simone

  • Hero Member
  • *****
  • Posts: 573
Re: Using c++ dll in lazarus
« Reply #1 on: February 11, 2019, 03:11:06 pm »
You can find useful information in the following paper:

ftp://ftp.freepascal.org/pub/fpc/docs-pdf/CinFreePascal.pdf
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Using c++ dll in lazarus
« Reply #2 on: February 11, 2019, 03:11:32 pm »
If you want to do it on Raspberry Pi, there are many alternatives that you can look here:
http://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#Native_hardware_access
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: Using c++ dll in lazarus
« Reply #3 on: February 11, 2019, 03:14:55 pm »
OS, hardware?
Mostly GPIO library have C headers not C++.

Upload C/C++ header here.

Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

osda

  • Newbie
  • Posts: 6
Re: Using c++ dll in lazarus
« Reply #4 on: February 11, 2019, 03:27:26 pm »
I m using a industrial pc with windows 7 : https://www.ieiworld.com/en/product/model.php?II=297

I have add the dll in attachement provides by the manufacturer.






« Last Edit: February 11, 2019, 11:04:37 pm by osda »

osda

  • Newbie
  • Posts: 6
Re: Using c++ dll in lazarus
« Reply #5 on: February 12, 2019, 09:34:22 am »
I have found a demo of api provides by the manufacturer.



Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Using c++ dll in lazarus
« Reply #6 on: February 12, 2019, 10:35:28 am »
I checked the SDK and it can not be used with Freepascal at this time.
The classes need to be flattened into a pure C proxie API which has not been done yet.
There are two issues here:
- FPC does not support C++ classes on interfacing level (yet, it is on the wish list), but it does support any pure C API.
- The manufacturer should really provide a flattened C interface, not Freepascal (or any other language that does not support C++ class interfaces)

As it stands, this API is strictly tied to C++.
So you can do two things:
- Flatten the API into pure C yourself, which requires quite some work on your side (and knowledge of C++, C and - e.g. - Freepascal)
- Ask the manufacturer to open things up for other programming languages by providing a pure C interface on top of the C++ classes..

There are some tricks/hacks to make this work in FPC otherwise but these are:
- C++ compiler dependend
- Platform dependend
- Not guaranteed to work and tied to compiler and platform versions.
- depend on internal knowledge of how a particular C++ compiler lays out its classes and VMT's.

So as it stands you need an API as a C interface, flattened, from the C++ interface.
( The hack is usually like this: C++ VMT's always starts at relative zero. If you know the alignment and calling convention you can declare a pure abstract Pascal class/object/record and take a pointer to the C++ class and cast it to the pure abstract class. Since all methods are external, you can subsequently call the C++ class methods through the cast. Although this works sometimes, it is not recommended)
« Last Edit: February 12, 2019, 10:58:44 am by Thaddy »
Specialize a type, not a var.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Using c++ dll in lazarus
« Reply #7 on: February 12, 2019, 03:01:19 pm »
FPC currently not support C++ based DLL and as I know no competition support this too. FreeBasic said they support that partially but the word "partial" I think means nearly just a prototype  :P

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Using c++ dll in lazarus
« Reply #8 on: February 12, 2019, 03:11:11 pm »
FPC currently not support C++ based DLL and as I know no competition support this too. FreeBasic said they support that partially but the word "partial" I think means nearly just a prototype  :P

Afaik that partial is the same as FPC cppdecl stuff. Very, very limited, and only for people that know what they are doing.

But now that VS community edition is very easy, wrapping dlls is about an hour work.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Using c++ dll in lazarus
« Reply #9 on: February 12, 2019, 04:24:40 pm »
The FreeBasic stuff uses something similar to my hack. Indeed it is limited, as I explained.
cppdecl is very limited if even correct at all. AFAIK It is certainly not C++ to all compilers because there is no such thing as a calling convention for C++ in the language specification.
« Last Edit: February 12, 2019, 04:58:52 pm by Thaddy »
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Using c++ dll in lazarus
« Reply #10 on: February 12, 2019, 05:10:22 pm »
FreeBasic has two codegenerators, an native/assembler one, and a plain C one.

My guess it is the plain C (and in practice: gcc) one, and the they simply the code from src to dest, and hope the C compiler does also C++ mangling etc.

Anyway, instead of wrapping it with FB, is to go to the source and do it with the free community visual studio, which is more compatible to VS codebases.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Using c++ dll in lazarus
« Reply #11 on: February 12, 2019, 10:19:36 pm »
I have an idea: it's almost impossible to use C++ DLL without flattened C wrapper so what about a Lazarus package that generate C.dll automatically from C++.dll for us? With the source available despite time consuming we can great the wrapper ourselves. What about closed source ones? I think dlltool with this hack can help, at least on Windows or as an inspiration: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs

Lazarus distribution already included gcc.exe (a very old one) why a dlltool.exe would matter?  ::)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Using c++ dll in lazarus
« Reply #12 on: February 12, 2019, 10:27:35 pm »
I have an idea: it's almost impossible to use C++ DLL without flattened C wrapper so what about a Lazarus package that generate C.dll automatically from C++.dll for us?

Well, there are two problems with that:
  • There is no such thing as an universal "C++" dll. Name mangling and calling conventions are compiler and compiler version dependent. This is both the "read" C++ definition parts AND the generate C that calls C++ part.
  • Even if it wasn't, it would require quite a bit of a C++ compiler to pull it off.

So that makes it manual. If there was an automated manner to just read the C++ definitions you could generate a (part of) a C/C++ project, but even that is difficult, and requires human interpretation.

But if that was so easy, such projects would already exist, and we would be talking here about integrating it in Lazarus, rather than writing it from scratch.

Quote
With the source available despite time consuming we can great the wrapper ourselves. What about closed source ones? I think dlltool with this hack can help, at least on Windows or as an inspiration: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs

Lazarus distribution already included gcc.exe (a very old one) why a dlltool.exe would matter?  ::)

The lazarus gcc.exe is a dummy for windres to process windres .h include files.

Moreover, most dlls are MSVC and not gcc/mingw in the first place.

But by all means, demonstrate something that works, and then we'll talk about inclusion :-)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Using c++ dll in lazarus
« Reply #13 on: February 13, 2019, 11:39:22 am »
There already exists an automatic wrapper generator: SWIG. Maybe it should finally be correctly extended for Free Pascal/Delphi?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Using c++ dll in lazarus
« Reply #14 on: February 13, 2019, 11:43:56 am »
I only know it as straight C converter, I don't know how good the C++ stuff is.

But a flattener will typically generate plain C->C++ sourcecode, and the C header of that at can be processed with h2pas, so why do we need pascal support ?

Moreover, it doesn't sort the fundamental problem, the many differing GCC builds (SJ vs SEH and native vs posix threads + versioning) vs VS (SDK's often use an older one, while the free one is very new).

And as Swig states:

Quote
In general, using SWIG with a different C++ compiler than the one that was used to compile the target scripting language may not work (for example, trying to create a Tcl/Tk module using the Borland compiler when Tcl/Tk has been compiled with Visual C++).

IOW problem 1 above persists, also with swig.
« Last Edit: February 13, 2019, 01:29:09 pm by marcov »

 

TinyPortal © 2005-2018