Android JNI bridge Toast C++ not working - How to fix it? - c++

I'm trying to solve a little problem with my hpp unit.
I found some examples on the internet showing how to Toast an information on screen with an Android JNI bridge, but it was just in pascal (delphi), so I decided to use the same pas file but with it an hpp file for conversion.
Till now I got it:
#ifndef Android_Jni_ToastHPP
#define Android_Jni_ToastHPP
#pragma delphiheader begin
#pragma option push
#pragma option -w- // All warnings off
#pragma option -Vx // Zero-length empty class member
#pragma pack(push,8)
#include <FMX.Helpers.Android.hpp> // Pascal unit
#include <Androidapi.JNIBridge.hpp> // Pascal unit
#include <Androidapi.JNI.JavaTypes.hpp> // Pascal unit
#include <Androidapi.JNI.GraphicsContentViewText.hpp> // Pascal unit
#include <Androidapi.JNI.Util.hpp> // Pascal unit
#include <Androidapi.JNI.App.hpp> // Pascal unit
#include <FMX.Surfaces.hpp> // Pascal unit
//-- user supplied -----------------------------------------------------------
namespace Android
{
namespace Jni
{
namespace Toast
{
//-- type declarations -------------------------------------------------------
extern DELPHI_PACKAGE Androidapi::Jni::Toast __fastcall Toast(const System::UnicodeString Msg, TToastLength duration);
#pragma pack(pop)
//-- type declarations -------------------------------------------------------
//-- var, const, procedure ---------------------------------------------------
} /* namespace Android */
} /* namespace JNI */
} /* namespace Toast */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROID_JNI_TOAST)
using namespace Android::Jni::Toast;
#endif
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROID_JNI)
using namespace Android::Jni;
#endif
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROID)
using namespace Android;
#endif
#pragma pack(pop)
#pragma option pop
#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif // Androidapi_Jni_ToastHPP
If you would like to have the pascal unit, here is it:
unit Android.JNI.Toast;
interface
{$IFDEF ANDROID}
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}
{$IFDEF ANDROID}
type
TToastLength = (LongToast, ShortToast);
JToast = interface;
JToastClass = interface(JObjectClass)
['{69E2D233-B9D3-4F3E-B882-474C8E1D50E9}']
{ Property methods }
function _GetLENGTH_LONG: Integer; cdecl;
function _GetLENGTH_SHORT: Integer; cdecl;
{ Methods }
function init(context: JContext): JToast; cdecl; overload;
function makeText(context: JContext; text: JCharSequence; duration: Integer)
: JToast; cdecl;
{ Properties }
property LENGTH_LONG: Integer read _GetLENGTH_LONG;
property LENGTH_SHORT: Integer read _GetLENGTH_SHORT;
end;
[JavaSignature('android/widget/Toast')]
JToast = interface(JObject)
['{FD81CC32-BFBC-4838-8893-9DD01DE47B00}']
{ Methods }
procedure cancel; cdecl;
function getDuration: Integer; cdecl;
function getGravity: Integer; cdecl;
function getHorizontalMargin: Single; cdecl;
function getVerticalMargin: Single; cdecl;
function getView: JView; cdecl;
function getXOffset: Integer; cdecl;
function getYOffset: Integer; cdecl;
procedure setDuration(value: Integer); cdecl;
procedure setGravity(gravity, xOffset, yOffset: Integer); cdecl;
procedure setMargin(horizontalMargin, verticalMargin: Single); cdecl;
procedure setText(s: JCharSequence); cdecl;
procedure setView(view: JView); cdecl;
procedure show; cdecl;
end;
TJToast = class(TJavaGenericImport<JToastClass, JToast>)
end;
procedure Toast(const Msg: string; duration: TToastLength = ShortToast);
{$ENDIF}
implementation
{$IFDEF ANDROID}
uses
FMX.Helpers.Android, Androidapi.Helpers;
procedure Toast(const Msg: string; duration: TToastLength);
var
ToastLength: Integer;
begin
if duration = ShortToast then
ToastLength := TJToast.JavaClass.LENGTH_SHORT
else
ToastLength := TJToast.JavaClass.LENGTH_LONG;
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(Msg),
ToastLength).show
end);
end;
{$ENDIF}
end.
However I get error when compilling using this source...
What con I do?
PS.: I arrived on that state just comparing AppMetthod sources with what I was making...
Log:
Checking project dependencies...
Compiling WebBrowser.cbproj (Debug, Android)
bccaarm command line for "uMain.cpp"
c:\program files (x86)\embarcadero\studio\15.0\bin\bccaarm.exe -cc1 -D _DEBUG -isysroot
C:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\platforms\android-14\arch-arm -idirafter =\usr\include -idirafter
C:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\sources\cxx-stl\gnu-libstdc++\4.8\include -idirafter
C:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\sources\cxx-stl\gnu-libstdc++\4.8\libs\armeabi-v7a\include -idirafter
C:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\sources\android\native_app_glue -I
"C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\CPP\Mobile Snippets\WebBrowser" -isystem "c:\program files
(x86)\embarcadero\studio\15.0\include" -isystem "c:\program files (x86)\embarcadero\studio\15.0\include\android\rtl" -isystem "c:\program files
(x86)\embarcadero\studio\15.0\include\android\fmx" -isystem "c:\program files (x86)\embarcadero\studio\15.0\include\android\crtl" -g
-fno-limit-debug-info -fborland-extensions -fborland-auto-refcount -nobuiltininc -nostdsysteminc -triple thumbv7-none-linux-androideabi -emit-obj
-mconstructor-aliases -pic-level 2 -target-abi aapcs-linux -nostdinc++ -fdeprecated-macro -fexceptions -fcxx-exceptions -munwind-tables
-mstackrealign -fno-spell-checking -fno-use-cxa-atexit -main-file-name uMain.cpp -x c++ -std=c++11 -O0 -tU -o .\Android\Debug\uMain.o
-dependency-file .\Android\Debug\uMain.d -MT .\Android\Debug\uMain.o uMain.cpp
[bccaarm Error] Android.JNI.Toast.hpp(36): expected a class or namespace
[bccaarm Error] Android.JNI.Toast.hpp(36): reference to 'Android' is ambiguous
Android.JNI.Toast.hpp(28): candidate found by name lookup is 'Android'
FMX.Helpers.Android.hpp(28): candidate found by name lookup is 'Fmx::Helpers::Android'
[bccaarm Error] Android.JNI.Toast.hpp(36): expected unqualified-id
[bccaarm Error] Android.JNI.Toast.hpp(47): expected a class or namespace
[bccaarm Error] Android.JNI.Toast.hpp(47): reference to 'Android' is ambiguous
Android.JNI.Toast.hpp(28): candidate found by name lookup is 'Android'
FMX.Helpers.Android.hpp(28): candidate found by name lookup is 'Fmx::Helpers::Android'
[bccaarm Error] Android.JNI.Toast.hpp(47): expected namespace name
[bccaarm Error] Android.JNI.Toast.hpp(50): expected a class or namespace
[bccaarm Error] Android.JNI.Toast.hpp(50): reference to 'Android' is ambiguous
Android.JNI.Toast.hpp(28): candidate found by name lookup is 'Android'
FMX.Helpers.Android.hpp(28): candidate found by name lookup is 'Fmx::Helpers::Android'
[bccaarm Error] Android.JNI.Toast.hpp(50): expected namespace name
[bccaarm Error] Android.JNI.Toast.hpp(53): reference to 'Android' is ambiguous
Android.JNI.Toast.hpp(28): candidate found by name lookup is 'Android'
FMX.Helpers.Android.hpp(28): candidate found by name lookup is 'Fmx::Helpers::Android'
[bccaarm Error] uMain.cpp(32): use of undeclared identifier 'Toast'
Failed
Elapsed time: 00:00:05.8
EDIT:
New log file:
[bccaarm Error] Androidapi.JNI.Toast.hpp(36): no type named 'Toast' in namespace 'Androidapi::Jni'
[bccaarm Error] Androidapi.JNI.Toast.hpp(36): expected unqualified-id
[bccaarm Error] uMain.cpp(32): unexpected namespace name 'Toast': expected expression
Failed
Since now, thanks a lot.

The .hpp file is malformed.
None of the public types defined in the interface section of the .pas file are defined in the .hpp file. In particular, the TToastLength enum is missing. And Toast() is declared as a procedure in the .pas file and thus has no return value, but it has an (erroneous) return value in the .hpp file.
Did you (or someone else) create the .hpp file manually? I suspect this is the case, for two noticable reasons:
the Android_JNI_ToastHPP define in the initial #ifndef/#define does not match the Fmx_Helpers_AndroidHPP in the corresponding #endif.
the Delphi compiler always names C++ namespaces for Pascal unit names by uppercasing the first letter and lowercasing the remaining letters, but this .hpp file has an all-uppercase JNI namespace.
The Delphi compiler would not makes those mistakes.
With that said, Android.JNI.Toast.pas should be renamed to Androidapi.JNI.Toast.pas (and the .hpp file renamed and its namespaces updated accordingly) for consistency with Embarcadero's other JNI units (and to match the example in this article). That should also help alleviate the reference to 'Android' is ambiguous errors as well.
I strongly suggest you correct the .pas file, run it through the Delphi compiler once to produce the correct .hpp file, and then use it as-is in your C++ code.
Androidapi.JNI.Toast.pas:
unit Androidapi.JNI.Toast;
interface
{$IFDEF ANDROID}
uses
Androidapi.JNIBridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;
type
TToastLength = (LongToast, ShortToast);
JToast = interface;
JToastClass = interface(JObjectClass)
['{69E2D233-B9D3-4F3E-B882-474C8E1D50E9}']
{ Property methods }
function _GetLENGTH_LONG: Integer; cdecl;
function _GetLENGTH_SHORT: Integer; cdecl;
{ Methods }
function init(context: JContext): JToast; cdecl; overload;
function makeText(context: JContext; text: JCharSequence; duration: Integer)
: JToast; cdecl;
{ Properties }
property LENGTH_LONG: Integer read _GetLENGTH_LONG;
property LENGTH_SHORT: Integer read _GetLENGTH_SHORT;
end;
[JavaSignature('android/widget/Toast')]
JToast = interface(JObject)
['{FD81CC32-BFBC-4838-8893-9DD01DE47B00}']
{ Methods }
procedure cancel; cdecl;
function getDuration: Integer; cdecl;
function getGravity: Integer; cdecl;
function getHorizontalMargin: Single; cdecl;
function getVerticalMargin: Single; cdecl;
function getView: JView; cdecl;
function getXOffset: Integer; cdecl;
function getYOffset: Integer; cdecl;
procedure setDuration(value: Integer); cdecl;
procedure setGravity(gravity, xOffset, yOffset: Integer); cdecl;
procedure setMargin(horizontalMargin, verticalMargin: Single); cdecl;
procedure setText(s: JCharSequence); cdecl;
procedure setView(view: JView); cdecl;
procedure show; cdecl;
end;
TJToast = class(TJavaGenericImport<JToastClass, JToast>)
end;
procedure Toast(const Msg: string; duration: TToastLength = ShortToast);
{$ENDIF}
implementation
{$IFDEF ANDROID}
uses
FMX.Helpers.Android, Androidapi.Helpers;
procedure Toast(const Msg: string; duration: TToastLength);
var
ToastLength: Integer;
begin
if duration = ShortToast then
ToastLength := TJToast.JavaClass.LENGTH_SHORT
else
ToastLength := TJToast.JavaClass.LENGTH_LONG;
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(SharedActivityContext, StrToJCharSequence(Msg),
ToastLength).show
end);
end;
{$ENDIF}
end.
Androidapi.JNI.Toast.hpp:
// CodeGear C++Builder
// Copyright (c) 1995, 2014 by Embarcadero Technologies, Inc.
// All rights reserved
// (DO NOT EDIT: machine generated header) 'Androidapi.JNI.Toast.pas' rev: 28.00 (Android)
#ifndef Androidapi_Jni_ToastHPP
#define Androidapi_Jni_ToastHPP
#pragma delphiheader begin
#pragma option push
#pragma option -w- // All warnings off
#pragma option -Vx // Zero-length empty class member
#pragma pack(push,8)
#include <System.hpp> // Pascal unit
#include <SysInit.hpp> // Pascal unit
#include <Androidapi.JNIBridge.hpp> // Pascal unit
#include <Androidapi.JNI.JavaTypes.hpp> // Pascal unit
#include <Androidapi.JNI.GraphicsContentViewText.hpp> // Pascal unit
#include <System.Rtti.hpp> // Pascal unit
//-- user supplied -----------------------------------------------------------
namespace Androidapi
{
namespace Jni
{
namespace Toast
{
//-- type declarations -------------------------------------------------------
enum DECLSPEC_DENUM TToastLength : unsigned char { LongToast, ShortToast };
__interface JToastClass;
typedef System::DelphiInterface<JToastClass> _di_JToastClass;
__interface JToast;
typedef System::DelphiInterface<JToast> _di_JToast;
__interface INTERFACE_UUID("{69E2D233-B9D3-4F3E-B882-474C8E1D50E9}") JToastClass : public Androidapi::Jni::Javatypes::JObjectClass
{
public:
virtual int __cdecl _GetLENGTH_LONG(void) = 0 ;
virtual int __cdecl _GetLENGTH_SHORT(void) = 0 ;
HIDESBASE virtual _di_JToast __cdecl init(Androidapi::Jni::Graphicscontentviewtext::_di_JContext context) = 0 /* overload */;
virtual _di_JToast __cdecl makeText(Androidapi::Jni::Graphicscontentviewtext::_di_JContext context, Androidapi::Jni::Javatypes::_di_JCharSequence text, int duration) = 0 ;
__property int LENGTH_LONG = {read=_GetLENGTH_LONG};
__property int LENGTH_SHORT = {read=_GetLENGTH_SHORT};
};
__interface INTERFACE_UUID("{FD81CC32-BFBC-4838-8893-9DD01DE47B00}") JToast : public Androidapi::Jni::Javatypes::JObject
{
public:
virtual void __cdecl cancel(void) = 0 ;
virtual int __cdecl getDuration(void) = 0 ;
virtual int __cdecl getGravity(void) = 0 ;
virtual float __cdecl getHorizontalMargin(void) = 0 ;
virtual float __cdecl getVerticalMargin(void) = 0 ;
virtual Androidapi::Jni::Graphicscontentviewtext::_di_JView __cdecl getView(void) = 0 ;
virtual int __cdecl getXOffset(void) = 0 ;
virtual int __cdecl getYOffset(void) = 0 ;
virtual void __cdecl setDuration(int value) = 0 ;
virtual void __cdecl setGravity(int gravity, int xOffset, int yOffset) = 0 ;
virtual void __cdecl setMargin(float horizontalMargin, float verticalMargin) = 0 ;
virtual void __cdecl setText(Androidapi::Jni::Javatypes::_di_JCharSequence s) = 0 ;
virtual void __cdecl setView(Androidapi::Jni::Graphicscontentviewtext::_di_JView view) = 0 ;
virtual void __cdecl show(void) = 0 ;
};
class DELPHICLASS TJToast;
#pragma pack(push,4)
class PASCALIMPLEMENTATION TJToast : public Androidapi::Jnibridge::TJavaGenericImport__2<_di_JToastClass,_di_JToast>
{
typedef Androidapi::Jnibridge::TJavaGenericImport__2<_di_JToastClass,_di_JToast> inherited;
public:
/* TJavaImport.Create */ inline __fastcall TJToast(void * ID, void * ClsID, Androidapi::Jnibridge::TJavaVTable* VTable) : Androidapi::Jnibridge::TJavaGenericImport__2<_di_JToastClass,_di_JToast> (ID, ClsID, VTable) { }
/* TJavaImport.Destroy */ inline __fastcall virtual ~TJToast(void) { }
};
#pragma pack(pop)
//-- var, const, procedure ---------------------------------------------------
extern DELPHI_PACKAGE void __fastcall Toast(const System::UnicodeString Msg, TToastLength duration = (TToastLength)(0x1));
} /* namespace Toast */
} /* namespace Jni */
} /* namespace Androidapi */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROIDAPI_JNI_TOAST)
using namespace Androidapi::Jni::Toast;
#endif
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROIDAPI_JNI)
using namespace Androidapi::Jni;
#endif
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_ANDROIDAPI)
using namespace Androidapi;
#endif
#pragma pack(pop)
#pragma option pop
#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif // Androidapi_Jni_ToastHPP

Related

64 bits DLL c/c++ interface to Delphi

I have a .h file that I need to translate into Delphi, to call a DLL interface that is written in C/C++.
In 32bit, everything goes well, I can use the DLL with a Delphi app with no issue.
In 64bit, it does not work very well.
I did not write this DLL interface, it comes from a third party that does hardware.
namespace gXusb {
typedef int INTEGER;
typedef short INT16;
typedef unsigned CARDINAL;
typedef unsigned char CARD8;
typedef float REAL;
typedef double LONGREAL;
typedef char CHAR;
typedef unsigned char BOOLEAN;
typedef void * ADDRESS;
struct CCamera;
extern "C" EXPORT_ void __cdecl Enumerate( void (__cdecl *CallbackProc)(CARDINAL) );
extern "C" EXPORT_ CCamera *__cdecl Initialize( CARDINAL Id );
Is Cardinal still 32bit unsigned under 64bit?
I'm not very sure about this way of declaring this function type and what it does with 64bit compilation:
void (__cdecl *CallbackProc)(CARDINAL)
It looks a bit cumbersome.
What puzzles me is this:
typedef unsigned CARDINAL;
I have figured out this is 32bit for a 32bit DLL, but did it stay 32bit under a 64bit DLL?
Something like the following should work fine in both 32bit and 64bit:
unit gXusb;
interface
type
// prefixing types that Delphi already declares...
_INTEGER = Int32;
_INT16 = Int16;
_CARDINAL = UInt32;
CARD8 = UInt8;
_REAL = Single;
LONGREAL = Double;
_CHAR = AnsiChar;
_BOOLEAN = ByteBool;
ADDRESS = Pointer;
CCamera = record end;
PCCamera = ^CCamera;
UsbEnumCallback = procedure(Param: _CARDINAL); cdecl;
procedure Enumerate(Cb: UsbEnumCallback); cdecl;
function Initialize(Id: _CARDINAL): PCCamera; cdecl;
implementation
const
TheDLLName = 'the.dll';
procedure Enumerate; external TheDLLName;
function Initialize; external TheDLLName;
end.
Personally, I would just get rid of any type aliases that are not strictly necessary, use native Delphi types were appropriate, eg:
unit gXusb;
interface
type
CARD8 = UInt8;
CCamera = record end;
PCCamera = ^CCamera;
UsbEnumCallback = procedure(Param: UInt32); cdecl;
procedure Enumerate(Cb: UsbEnumCallback); cdecl;
function Initialize(Id: UInt32): PCCamera; cdecl;
implementation
const
TheDLLName = 'the.dll';
procedure Enumerate; external TheDLLName;
function Initialize; external TheDLLName;
end.

Delphi calling C++Builder function in obj file, errors occured when some functions used from math.h

I want to use Delphi 10 Seattle to call a C obj file complied with C++ Builder 10 Seattle.
It succeeds when the C function is simple, eg. c=a+b, but it fails when using sin() or cos() operations.
The following are the codes:
C++Builder code:
#include "using_math.h"
#include <math.h>
double test2(double a)
{
return sin(a);
}
Header file:
double test2(double a);
Delphi code:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,math;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$L 'using_math.obj'}
{$R *.dfm}
end.
Errors:
[dcc32 Error] Unit1.pas(27): E2065 Unsatisfied forward or external declaration: '_sin'
[dcc32 Error] Unit1.pas(27): E2065 Unsatisfied forward or external declaration: '__turboFloat'
[dcc32 Fatal Error] Project12.dpr(5): F2063 Could not compile used unit 'Unit1.pas'

Delphi Library Memory Manager Strange

I got an error from DLL that compiled from Delphi using in c++ run in multithread
Delphi (Architect 10.3 Version 26.0.32429.4364) Library Code
library Project1;
uses
System.SysUtils,
System.Classes;
{$R *.res}
procedure __test(size: integer); cdecl;
var
data : AnsiString;
begin
SetLength(data, size);
end;
exports
__test;
begin
end.
C++ (Viausl Studio 2019) Load Library And Using Multithread
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <Windows.h>
#include <thread>
typedef void(__cdecl *functest)(int);
HINSTANCE hInst = nullptr;
functest test;
void thread_loop() {
while (1) {
test(10);
}
}
int main()
{
hInst = LoadLibraryA("Project1.dll");
if (!hInst) {
return 0;
}
test = (functest)GetProcAddress(hInst, "__test");
if (!test) {
return 0;
}
std::thread t1(thread_loop);
std::thread t2(thread_loop);
return 1;
I got an exception but it should not get any exception because that is procedure variable which was not shared
Set IsMultiThread to True in your Delphi DLL's main block. Delphi's default memory manager assumes single-threaded mode by default.
begin
IsMultiThread := True;
end.

Delphi Access Violation calling a C++ DLL that implements an interface

To import a DLL in Delphi I've written a unit translating a .h file
This DLL allow me to define a procedure to monitor two values and use them in my app.
the original .h file is:
class MyINTERFACE
{
public:
virtual void MonitorProc(int value1, int value2) = 0;
};
extern MY_API MyHANDLE* Init();
extern MY_API UINT InitMonitorProc(MyHANDLE * handle, MyINTERFACE * pMyProc);
and this is my translation:
unit myCdllUnit;
interface
const
TMyHANDLE = DWORD;
MyINTERFACE = class
public
procedure MonitorProc(value1: longint; value2: longint); virtual; cdecl; abstract;
end;
function Init(): TMyHANDLE; cdecl; external 'myCdll.dll';
function InitMonitorProc(handle: TMyHANDLE; pMyProc: MyINTERFACE):DWORD; cdecl; external 'myCdll.dll';
implementation
end.
in my main form I override the MonitorProc (initially only with "inherited" to test the calling)
TMyMonitor = class(MyINTERFACE)
public
procedure MonitorProc(value1: longint; value2: longint); override;
end;
procedure TMyMonitor.MonitorProc(value1: longint; value2: longint);
begin
inherited;
end; <--------------------------- here get an Access Violation
and in my FormCreate I pass an istance to InitMonitorProc
MyMonitor := TMyMonitor.Create;
InitMonitorProc(Handle, MyMonitor);
the DLL is calling correctly but exiting MonitorProc I get an access violation error.
Where am I wrong?

Return string array from dll function [duplicate]

This question already has an answer here:
Receive an array of string from a c++ DLL in Delphi 7
(1 answer)
Closed 7 years ago.
How can I return a string array from C++ dll and then the function will be call by a Delphi application.
I tried:
C++ dll
#include <windows.h>
#include <vector>
#include <string>
using namespace std;
extern "C"
{
__declspec( dllexport ) void arrayStr(vector<string> s)
{
s.push_back("111");
s.push_back("222");
s.push_back("333");
}
}
Delphi
procedure arrayStr(StrMem : TStringList); cdecl; external 'arrayStr.dll';
...
var
StrMem : TStringList;
i : integer;
begin
StrMem := TStringList.Create;
arrayStr(StrMem);
for i := 0 to StrMem.Count-1 do
begin
ShowMessage(StrMem[i]);
end;
StrMem.Free;
end;
The TStringList (Delphi) is incompatible with C++ STL containers.
You should do the following:
C/C++ side:
void __stdcall Func(char **strings, int count);
Delphi side:
type PPAnsiChar = ^PAnsiChar;
procedure Func(ArrayOfStrings: PPAnsiChar; CountOfArray: Integer); stdcall;