Lazarus

Programming => General => Topic started by: osda on February 11, 2019, 02:29:00 pm

Title: Using c++ dll in lazarus
Post by: osda 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.



Title: Re: Using c++ dll in lazarus
Post by: simone 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
Title: Re: Using c++ dll in lazarus
Post by: avra 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
Title: Re: Using c++ dll in lazarus
Post by: mig-31 on February 11, 2019, 03:14:55 pm
OS, hardware?
Mostly GPIO library have C headers not C++.

Upload C/C++ header here.

Title: Re: Using c++ dll in lazarus
Post by: osda 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.






Title: Re: Using c++ dll in lazarus
Post by: osda on February 12, 2019, 09:34:22 am
I have found a demo of api provides by the manufacturer.


Title: Re: Using c++ dll in lazarus
Post by: Thaddy 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)
Title: Re: Using c++ dll in lazarus
Post by: giahung1997 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
Title: Re: Using c++ dll in lazarus
Post by: marcov 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.
Title: Re: Using c++ dll in lazarus
Post by: Thaddy 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.
Title: Re: Using c++ dll in lazarus
Post by: marcov 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.
Title: Re: Using c++ dll in lazarus
Post by: giahung1997 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?  ::)
Title: Re: Using c++ dll in lazarus
Post by: marcov 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:

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 :-)
Title: Re: Using c++ dll in lazarus
Post by: PascalDragon on February 13, 2019, 11:39:22 am
There already exists an automatic wrapper generator: SWIG (http://www.swig.org/). Maybe it should finally be correctly extended (http://wiki.freepascal.org/SWIG) for Free Pascal/Delphi?
Title: Re: Using c++ dll in lazarus
Post by: marcov 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.
Title: Re: Using c++ dll in lazarus
Post by: Thaddy on February 13, 2019, 01:03:17 pm
That still leaves that he has enough information (the def and the API) but not enough determination to take on the task...
API's are strange things: the larger they are the less functionality you use in my experience, so pick and choose is a real option most of the time. (e.g. sqlite is not fully translated, but fully functional in normal life)
Title: Re: Using c++ dll in lazarus
Post by: mig-31 on February 13, 2019, 05:07:11 pm
As I see your motheboard have 8 DIO(configurable digital input/output). Because it is I/O port, you can control it thought I/O port memory address, like motherboard COM or LPT port. In this case you need OS, which provided direct memory access, for example Linux (needed
root permition) and you don't need any C++ SDK.

Use these functions to do it https://www.freepascal.org/docs-html/current/rtl/x86/index-5.html (https://www.freepascal.org/docs-html/current/rtl/x86/index-5.html)
Code: Pascal  [Select][+][-]
  1. fpIOperm
  2. WritePort
  3. ReadPort
  4.  
Title: Re: Using c++ dll in lazarus
Post by: giahung1997 on February 15, 2019, 08:05:54 am
That still leaves that he has enough information (the def and the API) but not enough determination to take on the task...
API's are strange things: the larger they are the less functionality you use in my experience, so pick and choose is a real option most of the time. (e.g. sqlite is not fully translated, but fully functional in normal life)

I think I should remind you I'm not the same as the OP and we attempt to solve a different problem  :)
Title: Re: Using c++ dll in lazarus
Post by: kirill on April 02, 2022, 03:27:40 pm
Anobody can help me create pas file for driver?
Driver Shtrih-M for cash-device. In delphi I can import ActiveX component and save as pas file. But this file I can't use in Lazarus witch don't support OLE. I found project in github https://github.com/shtrih-m/fr_drv_ng with c++ headers for driver. Anybody can create pas file from this headers?
I'm interesting release for windows. 
Title: Re: Using c++ dll in lazarus
Post by: marcov on April 02, 2022, 03:31:06 pm
Anobody can help me create pas file for driver?
Driver Shtrih-M for cash-device. In delphi I can import ActiveX component and save as pas file. But this file I can't use in Lazarus witch don't support OLE.

Pascal should roughly work with the Delphi import file, and has its own (commandline) importer called importtl.
Title: Re: Using c++ dll in lazarus
Post by: BSaidus on April 02, 2022, 08:39:18 pm
You can find usefull informations about how to use C++ classes in Delphi.
http://rvelthuis.de/articles/articles-cppobjs.html (http://rvelthuis.de/articles/articles-cppobjs.html)
Title: Re: Using c++ dll in lazarus
Post by: kirill on April 02, 2022, 08:51:18 pm
Pascal should roughly work with the Delphi import file, and has its own (commandline) importer called importtl.
I right understand that I must run importtl some_file.h?
I try run for each file (not only .h) but have error

Reading typelib from classic_interface.h ...                                                                            An unhandled exception occurred at $000000010003E4EF:                                                                   EOleSysError: Ошибка при загрузке библиотеки.                                                                             $000000010003E4EF                                                                                                       $000000010002D44D                                                                                                       $000000010002E2A8                                                                                                       $0000000100022E9B                                                                                                       $0000000100001F34                                                                                                       $0000000100002356                                                                                                       $0000000100011F10                                                                                                       $0000000100001820                                                                                                       $00007FFCAA647034                                                                                                       $00007FFCAA782651                                                                                                     


PS. Sorry for my badly english.
Title: Re: Using c++ dll in lazarus
Post by: PascalDragon on April 02, 2022, 08:59:38 pm
Pascal should roughly work with the Delphi import file, and has its own (commandline) importer called importtl.
I right understand that I must run importtl some_file.h?

You shouldn't use this on the header files. This is for importing the type library. You can also install the LazActiveX package that will add a menu item to import an ActiveX component as well (yes, FPC does support OLE/ActiveX without any (currently) known problems).
Title: Re: Using c++ dll in lazarus
Post by: kirill on April 03, 2022, 02:01:16 am
Sorry, my english pour and I can't tell what's problems. Thank you, PascalDragon, I understand you message and I install and import DrvFR.dll from system. But in Delphi create module with class TDrvFR witch I use in Delphi and have small knowledge, but Lazarus create IDrvFR and I don't know hove it's use. And you can't help me with my language. Thank you all for home help me, but I go russian forum with my problem. Thank you.

PS. @marcov, I understand what you mean about importttl, and I'm import dll with this tool too, but it's don't have (simple for me TDrvFR) . Delphi module without number, Lazarus with 1_3
Title: Re: Using c++ dll in lazarus
Post by: PascalDragon on April 04, 2022, 01:41:31 pm
PS. @marcov, I understand what you mean about importttl, and I'm import dll with this tool too, but it's don't have (simple for me TDrvFR) . Delphi module without number, Lazarus with 1_3

You need to instantiate TAxcDrvFR instead and then use the OleServer property to access the functionality of the library.
Title: Re: Using c++ dll in lazarus
Post by: AlexTP on April 04, 2022, 02:03:11 pm
@kirill
>Sorry, my english pour and I can't tell what's problems.

Consider to use google-translate, it gives OK results!
TinyPortal © 2005-2018