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?
Related
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'
I have a simple program that exports a DLL, this DLL exports functions from another DLL:
// SDK_DLL.cpp :
#include "functions.h"
#include "functions_advanced.h"
#include "stdafx.h"
#include <stdio.h>
using namespace std;
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf("Hello from DLL...");
}
__declspec(dllexport) function_config FuncInit = appd_config_init();
__declspec(dllexport) function_config * FuncInit2()
{
function_config* cfg = function_config_init();
return cfg;
}
}
The function_config_init() returns a pointer, I cannot seem to find a way of making this proper export declaration.
I am loading a simple function to Delphi this way:
procedure DisplayHelloFromDLL; external 'C:\Users\Administrator\Documents\Visual Studio 2017\Projects\SDK_DLL\Debug\SDK_DLL.dll';
Will I need to change the way I am loading this pointer returning function?
Thanks a lot for your help.
FuncInit is an exported variable. Delphi does not support importing of variables via external, only of functions. If you need to import FuncInit, you will have to use GetProcAddress() directly to get a pointer to the variable at runtime:
type
// you did not show the C/C++ declaration
// of function_config, so I can't provide
// a translation here, but it is likely to
// be a struct, which is a record in Delphi ...
function_config = ...;
pfunction_config = ^function_config;
function GetFuncInit: pfunction_config;
begin
Result := pfunction_config(GetProcAddress(GetModuleHandle('SDK_DLL.dll'), 'FuncInit'));
end;
var
FuncInit: pfunction_config;
FuncInit := GetFuncInit;
For purposes of interop across languages/compilers, the only portable calling conventions are cdecl and stdcall. When no calling convention is specified in code, the default used by most C and C++ compilers is __cdecl (but can usually be specified in compiler settings), while the default used by Delphi is register instead (__fastcall in C++Builder).
When no parameters or return value are used, like in DisplayHelloFromDLL(), then declaring the wrong calling convention does not really matter. But when parameters and/or a return value are used, like in FuncInit2(), then declaring the correct calling convention matters. See Pitfalls of converting for more details.
So, the two DLL functions in question would likely need to be declared like the following in Delphi:
type
function_config = ...;
pfunction_config = ^function_config;
procedure DisplayHelloFromDLL; cdecl; external 'SDK_DLL.dll' name '_DisplayHelloFromDLL';
function FuncInit2: pfunction_config; cdecl; external 'SDK_DLL.dll' name '_FuncInit2';
If the DLL uses a .def file to remove name mangling from the exported names, you can omit the name attribute:
type
function_config = ...;
pfunction_config = ^function_config;
procedure DisplayHelloFromDLL; cdecl; external 'SDK_DLL.dll';
function FuncInit2: pfunction_config; cdecl; external 'SDK_DLL.dll';
I'm writing a small application in VB.NET in Visual Studio 2013, and so far what I've written is the following code:
Public Class MainMenu
Private Declare Function Version_Get Lib "mypath/mydll.dll" () As String
' Before anyone asks, yes, the DLL is present in the mypath folder
Private Sub MainMenu_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Temp As String
Dim Dummy As Integer
On Error GoTo Handler
Temp = Version_Get
Dummy = 1 ' This line is never reached in debug mode
Handler:
If Not IsNothing(Err.GetException()) Then
MsgBox("Error " & Str(Err.Number) & " generated by the application " & Err.Source & ControlChars.CrLf & Err.Description, vbCritical, "Error")
End
End If
End Sub
End Class
Where the DLL is supposed to return a BSTR (this is of course extern "C" etc., but for the sake of comprehension I'm writing the function pure and simple):
File .h
#ifdef EVALFUNC_EXPORTS
#define EVALFUNC_API __declspec(dllexport)
#else
#define EVALFUNC_API __declspec(dllimport)
#endif
extern "C"
{
EVALFUNC_API BSTR __stdcall Version_Get();
}
File .cpp
BSTR __stdcall Version_Get()
{
CRegKey Key;
CString sValue;
BSTR Str;
LONG nA = Key.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\my Software"), KEY_READ);
// Before anyone asks, yes, the application is present in the system
ULONG nValueLength = 0;
LONG nB = Key.QueryStringValue(_T("Version"), NULL, &nValueLength);
if (nValueLength > 0) LONG nC = Key.QueryStringValue(_T("Version"), sValue.GetBufferSetLength(nValueLength - 1), &nValueLength);
Str = sValue.AllocSysString();
return Str;
}
The problem is that even if I wrote an error handler, the code encounters no error, and Version_Get makes my code crash without any error (the MainMenu form gets loaded anyway).
I tried the DLL on another VB environment (Excel) with the same declaration.
Private Declare Function Version_Get Lib "mypath/mydll.dll" () As String
In that case the string variable is filled with the correct text.
What am I doing wrong?
I had to create a class to import the Dll by marshalling the BSTR String:
Imports System.Runtime.InteropServices
Public Class ImportDll
<DllImport("myDll.dll", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function Version_Get() As <MarshalAs(UnmanagedType.BStr)> String
End Function
End Class
And could call the code as follows:
Version = ImportDll.Version_Get
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
i have some trouble accessing a dll written in vc++ that exports an interface. First i tried to use classes, but after some google-search i came to the solution, that this i not possible. I just want to make sure, that the plugin interface can accessed, by using other languages like c++.
Delphi Interface
IPlugIn = interface
function GetName: WideString; stdcall;
end;
Delphi Plugin call
procedure TForm1.Button5Click(Sender: TObject);
var
hLib: Cardinal;
MLoadPlugIn: TLoadPlugIn;
PlugIn: IPlugIn;
begin
hLib := LoadLibrary('PluginB.dll');
try
if not(hLib = 0) then
begin
#MLoadPlugIn := GetProcAddress(hLib, 'LoadPlugIn');
if not(#MLoadPlugIn = nil) then
begin
if MLoadPlugIn(PlugIn) then
try
ShowMessage(PlugIn.GetName); // here i get the access-violation using the vc++ plugin
finally // i get the return value but the instance is not created
PlugIn := nil;
end;
end
else
raise Exception.Create('');
end;
finally
FreeLibrary(hLib);
end;
end;
Delphi plugin dll
TMyPlugin = class(TInterfacedObject, IPlugIn)
public
function GetName: WideString; stdcall;
end;
function TMyPlugin.GetName;
begin
result := 'TMyPlugin';
end;
function LoadPlugIn(var PlugIn: IPlugIn): Boolean; stdcall;
begin
try
PlugIn := TMyPlugin.Create;
result := True;
except
result := False;
end;
end;
exports
LoadPlugIn;
vc++ plugin dll
// IPlugIn
__interface //__declspec(uuid("E44BB34F-D13F-42D7-9479-4C79AF5C0D1B"))
IPlugIn : public IUnknown
{
void _stdcall GetName(BSTR* result);
};
// TMyPlugIn header
class TMyPlugIn : public IPlugIn
{
public:
// Constructor
TMyPlugIn() : m_cRef(1) {}
// Destructor
~TMyPlugIn() {}
// Needed to implement IUnknown used by COM to acces your component
HRESULT _stdcall QueryInterface(const IID& iid, void** ppv);
ULONG _stdcall AddRef();
ULONG _stdcall Release();
void _stdcall GetName(BSTR* result);
private:
long m_cRef ;
};
// TMyPlugIn cpp
HRESULT _stdcall TMyPlugIn::QueryInterface(const IID& iid, void** ppv)
{
if (iid == IID_IUnknown)
{
*ppv = static_cast<IPlugIn*>(this) ;
}
else if (iid == IID_IPlugIn)
{
*ppv = static_cast<IPlugIn*>(this) ;
}
else
{
*ppv = NULL ;
return E_NOINTERFACE ;
}
reinterpret_cast<IUnknown*>(*ppv)->AddRef() ;
return S_OK ;
}
ULONG _stdcall TMyPlugIn::AddRef()
{
return InterlockedIncrement(&m_cRef) ;
}
ULONG _stdcall TMyPlugIn::Release()
{
if (InterlockedDecrement(&m_cRef) == 0)
{
delete this ;
return 0 ;
}
return m_cRef ;
}
void _stdcall TMyPlugIn::GetName(BSTR* result)
{
string s1 = "PluginName";
*result = A2WBSTR(s1.c_str());
}
// the export function from the cpp plugin
extern "C" bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn* PlugIn);
bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn* PlugIn)
{
PlugIn = new TMyPlugIn;
return TRUE;
}
You get the access violation because this code
extern "C" bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn* PlugIn);
bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn* PlugIn)
{
PlugIn = new TMyPlugIn;
return TRUE;
}
creates an instance of your plugin class and writes the address to the stack, where it quickly will be forgotten. Back in the Delphi program the original plugin interface variable is still nil, so calling a method on it crashes. You need to mimic what QueryInterface() does, like so:
extern "C" bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn** PlugIn);
bool __declspec(dllexport) __stdcall LoadPlugIn(IPlugIn** PlugIn)
{
*PlugIn = new TMyPlugIn;
return TRUE;
}
This passes the address of the interface variable, and the address of the plugin instance will be written to the variable.
In addition to what mghie has said, you also have a problem with mismatched definitions between Delphi and C++
Your C++ signature for GetName is:
void _stdcall GetName(BSTR* result);
Your Delphi signature is:
function GetName: WideString; stdcall;
There are (at least) 2 possible ways to fix this.
1) If you want the Delphi code to work as a function, then make it safecall and adjust the C++ to match:
Delphi:
function GetName: WideString; safecall;
C++:
HRESULT _stdcall GetName(BSTR* result);
or
2) fix the Delphi to match the existing C++ defn:
procedure GetName( var name: WideString );
I (personally) would probably go the safecall route, as I think it is much cleaner on the Delphi side...
In general, you should not export interfaces (and for that matter: objects should really not be exported) across DLL boundaries because you do not know which memory manager, run-time library and object model will be on either side.
See also this thread about exceptions in DLL's (exceptions are objects).
Since the Delphi interface model is binary compatible with the COM interface model, and Visual C++ can export COM objects, you should go the COM way (was Adelf also suggested).
--jeroen
All Delphi classes parent - TObject class from VCL.
If you use Borland C++ Builder(VCL library) - you can write plugin to Delphi with this way.
For another cases.. you should read about COM.