How to call C++ function with Vector

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to call C++ function with Vector

Dennis
I have a windows 32-bit dll written in C++ (probably C++ Builder).
It has 1 function which I need to call:

int SPAPI_GetProduct(vector<SPApiProduct>& apiProdList)

I am stuck at the keyword vector.

How is it translated into free pascal?

Is the following correct?

type
    TSPApiProduct_Array = array of SPApiProduct;

function SPAPI_GetProduct(var apiProdList : TSPApiProduct);stdcall;

Thanks a lot in advance.

Dennis
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Free Pascal - General mailing list
> I am stuck at the keyword vector.
>
> How is it translated into free pascal?

It's hard, you must create additional wrapper that exposes simpler type
(pointer or array of pchar) OR make wrapper for the vector methods and
simply declare it as an opaque pointer. std::vector is template/generic C++
class that implements a dynamic array functionality, but it can't be used
outside C++.



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Marco van de Voort
In reply to this post by Dennis
In our previous episode, Dennis said:
> I have a windows 32-bit dll written in C++ (probably C++ Builder).
> It has 1 function which I need to call:
>
> int SPAPI_GetProduct(vector<SPApiProduct>& apiProdList)
>
> I am stuck at the keyword vector.

It is not a keyword it is a generic type. So that is not easy, specially
since C++ and Pascal classes differ.

And even if the call succeeds, getting results from apiprodlist is major
problem number two.

You can try to match the C++ code with carefully crafted pascal code, but
that requires knowledge about implementation details of the C++
implementaton.

I avoided that way (as too complicated and too fragile), but Rudy Velthuis
has an article on it (C++ <-> Delphi)
http://www.rvelthuis.de/articles/articles-cppobjs.html

I myself generally solve this with a wrapper DLL, by crafting a DLL in C++
that exports a C (not C++) interface, and then use that. The same method as
QT is used from Lazarus (via wrapper dll/.so qtpas)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Mark Morgan Lloyd-5
In reply to this post by Free Pascal - General mailing list
On 26/09/17 11:00, leledumbo via fpc-pascal wrote:
>> I am stuck at the keyword vector. >> How is it translated into free pascal?
> It's hard, you must create additional wrapper that exposes simpler type(pointer or array of pchar) OR make wrapper for the vector methods andsimply declare it as an opaque pointer. std::vector is template/generic C++class that implements a dynamic array functionality, but it can't be usedoutside C++.

Would it be possible to do this via a Lua shim?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Dennis Poon
In reply to this post by Marco van de Voort


Marco van de Voort wrote:
>
> I myself generally solve this with a wrapper DLL, by crafting a DLL in C++
> that exports a C (not C++) interface, and then use that. The same method as
> QT is used from Lazarus (via wrapper dll/.so qtpas)
> _______________________________________________
Thanks.  I think I will follow your advice.

Dennis

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Free Pascal - General mailing list
In reply to this post by Mark Morgan Lloyd-5
> Would it be possible to do this via a Lua shim?

How is it even related with that?



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Mark Morgan Lloyd-5
On 26/09/17 19:00, leledumbo via fpc-pascal wrote:
>> Would it be possible to do this via a Lua shim?
> How is it even related with that?

I thought FPC had the ability to execute a fragment of Lua, and Lua I
believe can call C++ directly.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Free Pascal - General mailing list
> I thought FPC had the ability to execute a fragment of Lua, and Lua I
believe can call C++ directly.

No, Lua can only call C, just like FPC. You do know the problem of C++
interfacing, don't you?



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to call C++ function with Vector

Mark Morgan Lloyd-5
On 27/09/17 05:15, leledumbo via fpc-pascal wrote:
>> I thought FPC had the ability to execute a fragment of Lua, and Lua Ibelieve can call C++ directly.
> No, Lua can only call C, just like FPC. You do know the problem of C++interfacing, don't you?

Yes, very much so. My apologies for my speculation generating noise.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal