Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2017-09

How to call C++ function with Vector

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

leledumbo
to Dennis
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++.

Marco van de Voort
to Dennis: I have a windows 32-bit dll written in C++ (probably C++ Builder). It has 1 function…

In our previous episode, Dennis said:

Show quoted text 6 lines
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)

Mark Morgan Lloyd
to leledumbo: It's hard, you must create additional wrapper that exposes simpler type (pointer or array…
leledumbo 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]
Mark Morgan Lloyd
to leledumbo
leledumbo 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]
Mark Morgan Lloyd
to leledumbo
leledumbo 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]