Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   importar una dll de c++ (https://www.clubdelphi.com/foros/showthread.php?t=87046)

mayusod 08-11-2014 20:11:59

importar una dll de c++
 
Hola chicos a ver si alguin me puede decir por que esto no funciona

Código:

fichero de cabecera .h
#ifndef _CGLOGICSWINAPI_H_
#define _CGLOGICSWINAPI_H_
#if _MSC_VER > 1000
#pragma once
#endif
#ifdef __cplusplus
extern "C" { // only need to export C interface if used by C++ source code
#endif
#ifdef _COMPILING_THE_DLL_
#define CGLOGICS_API __declspec(dllexport)
#else
#define CGLOGICS_API __declspec(dllimport)
#endif
// API functions recommended for new integrations
CGLOGICS_API short __stdcall registerEvents3CG( void (__cdecl *cgStatusCall)( int amount, short status, short mode ),
void (__cdecl *cgErrorCall)( int errorCode, char* errorText, char* extInfo ),
void (__cdecl *cgLevelWarningCall)( short warningType, long denomination, short numberOf, char* typeString, #ifdef __cplusplus
}
#endif
#endif

y ahora la conversion a Delphi

Código Delphi [-]
 
unit CGLOGICSWINAPI;
interface
uses
{$IFDEF WIN32}
Windows,Dialogs,SysUtils;
{$ELSE}
Wintypes, WinProcs;
{$ENDIF}
{$IFNDEF _CGLOGICSWINAPI_H_}
{$DEFINE _CGLOGICSWINAPI_H_}
{$IFDEF _MSC_VER > 1000}
{ #pragma once }
{$ENDIF}
{$IFDEF __cplusplus}
{$ENDIF}
type
TProcStatus = procedure ( amount: Integer; status , mode:SmallInt); cdecl;
TProcError = procedure ( errorCode: Integer; errorText,extInfo : PChar); cdecl;
TProcLevelWarning = procedure (warningType:SmallInt; denomination: LongInt; numberOf :SmallInt;
typeString,denominationString,warningMessage,extInfo:PChar); cdecl;
TCashSession = Procedure(infoType: SmallInt;amount: LongInt; extInfo: PChar); cdecl;
 
function registerEvents3CG(ProcStatus: TProcStatus; ProcError:TProcError;
ProcLevelWarning:TProcLevelWarning; ProcCashSession:TCashSession;
message: PChar): SmallInt cdecl {$IFDEF WIN32} stdcall {$ENDIF};
 
{$IFDEF __cplusplus}
{$ENDIF}
{$ENDIF}
procedure ProcStatus( amount: Integer; status , mode:SmallInt); cdecl;
Procedure ProcError( errorCode: Integer; errorText,extInfo : PChar); cdecl;
procedure ProcLevelWarning(warningType:SmallInt; denomination: LongInt; numberOf :SmallInt;
typeString,denominationString,warningMessage,extInfo:PChar); cdecl;
Procedure CashSession(infoType: SmallInt;amount: LongInt; extInfo: PChar); cdecl;
 
implementation
procedure ProcStatus( amount: Integer; status , mode:SmallInt); cdecl;
begin
ShowMessage('amount:'+IntToStr(amount)+' statut:'+IntTostr(Status)+' mode:'+IntTostr(Mode));
end;
 
Procedure ProcError( errorCode: Integer; errorText,extInfo : PChar); cdecl;
begin
ShowMessage('errorCode:'+IntToStr(errorCode)+' errorText:'+errorText+' extInfo:'+extInfo);
end;
 
procedure ProcLevelWarning(warningType:SmallInt; denomination: LongInt; numberOf :SmallInt;
typeString,denominationString,warningMessage,extInfo:PChar); cdecl;
begin
ShowMessage('warningType:'+IntToStr(warningType)+' denomination:'+IntToStr(denomination)
+' numberOf:'+IntToStr(numberOf)+' typeString:'+typeString+' denominationString:'+denominationString
+' warningMessage:'+warningMessage+' extInfo:'+extInfo);
end;
 
Procedure CashSession(infoType: SmallInt;amount: LongInt; extInfo: PChar); cdecl;
begin
ShowMessage('infoType:'+IntToStr(infoType)+' amount:'+IntToStr(amount)+' extInfo:'+extInfo);
end;
 
function registerEvents3CG; external 'CGLOGICS.DLL';
 
end.

y asi es como lo llamo desde mi programa

Código Delphi [-]
  
unit UcashGuard;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls, Mask, rxToolEdit, rxCurrEdit,CGLOGICSWINAPI;
type
TForm1 = class(TForm)
btnIniApi: TButton;
BitBtn1: TBitBtn;
Label1: TLabel;
EditPuerto: TCurrencyEdit;
procedure btnIniApiClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
hDll: THandle;
MensajeCG : String;
end;
var
Form1: TForm1;
implementation
 
{$R *.dfm}
 
procedure TForm1.btnIniApiClick(Sender: TObject);
begin
MensajeCG:='';
registerEvents3CG(ProcStatus,ProcError,ProcLevelWarning,CashSession,PAnsichar(MensajecG));
end;
end.

alguna idea de porque cuando ejecuto el boton "Ini apli" me da el siguiente error
---------------------------
Cashguard
---------------------------
Access violation at address 100332F9 in module 'CGLOGICS.DLL'. Write of address 00404DBD.
---------------------------
Aceptar
---------------------------

gracias espero sus respuestas

Ñuño Martínez 11-11-2014 11:04:58

Creo que has copiado mal el archivo de cabecera de C. Me falta un parámetro (o más) en la función de registro.

De todas formas, visto por encima, las declaraciones están bien. ¿Has depurado paso a paso a ver cómo hace la llamada, y si los parámetros son los correctos?

escafandra 11-11-2014 19:54:24

Coincido con Ñuño Martínez en que no has copiado bien el archivo cabecera C:
Código:

void (__cdecl *cgLevelWarningCall)( short warningType, long denomination, short numberOf, char* typeString, #ifdef __cplusplus
No tiene sentido, te faltan cosas detrás de typeString...

Por otro lado, la función registerEvents3CG recibe como parámetros punteros a funciones void C. En delphi debes pasar los punteros a los procedimientos adecuados.

Es importante saber como están exportadas las funciones C en esa dll, en algunas ocasiones aparecen "nombres decorados", con la dll en la mano, lo podemos saber.

Sin las declaraciones correctas en C y sin la dll en cuestión, no se te puede ofrecer más ayuda.


Saludos.

Ñuño Martínez 13-11-2014 10:47:12

Cita:

Empezado por escafandra (Mensaje 484484)
Es importante saber como están exportadas las funciones C en esa dll, en algunas ocasiones aparecen "nombres decorados", con la dll en la mano, lo podemos saber.

Cierto. Y pensando en ello, y si no recuerdo mal, hay un standard de facto en C en el que se añade el carácter subrayado a los nombres de función dentro de las DLL, y en C++ se añaden caracteres para indicar el número y tipo de los parámetros, por aquello de la sobrecarga de funciones.


La franja horaria es GMT +2. Ahora son las 22:18:25.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi