Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   llamada a una funcion dentro de una dll (https://www.clubdelphi.com/foros/showthread.php?t=49217)

mauqu 16-10-2007 14:48:44

llamada a una funcion dentro de una dll
 
Estimado.

Desarrolle una dll con delphi con una función exportada, y la uso desde una aplicación desarrollada también en delphi y todo anda perfecto, pero cuando quiero usar esa misma función con otro lenguaje de programación, por ejemplo c++ me sale el siguiente error luego de que se llama a la función de mi dll

"argumentos incorrectos, o en conflicto con otros.", cuando le doy aceptar a este mensaje la función de la dll se ejecuta perfectamente.

por que puede ser esto ???, ahí les paso el código que desarrolle.

Código Delphi [-]
 
library Interfaz;
 
uses
  SysUtils,
  Classes,
  Dialogs,
  ActiveX,
  dNegocios in 'dNegocios.pas' {dmNegocios: TDataModule},
  fTester in 'fTester.pas' {frmTester};
{$R *.res}
var
  MProc: procedure(Respuesta:integer);
 
procedure Inicializar;
begin 
  CoInitialize(nil);
  if not assigned(dmNegocios) then
    dmNegocios:=TdmNegocios.Create(nil); 
  if not assigned(frmTester) then
    frmTester:=TfrmTester.Create(nil);
end;
 
procedure Terminar;
begin
  freeandnil(dmNegocios);
  freeandnil(frmTester);
end;
 
function Testear(Value:integer):integer; StdCall;
begin
  Result:=0;
  Inicializar;
  frmTester.ShowModal;
  Terminar;
end;
 
exports
  Testear name 'Testear';
 
begin

end.

mensana 16-10-2007 16:24:01

Cita:

Empezado por mauqu (Mensaje 238824)
... pero cuando quiero usar esa misma función con otro lenguaje de programación, por ejemplo c++

Quizá el problema está en la definición de la interface en C++ (el fichero .h)
, como la has definido ?

mauqu 16-10-2007 16:34:47

El tema es este, quiero crear una dll que despues la pueda usar con cualquier lenguaje o programa que trabaje con dll.

Probe con c++ de la siguiente manera

Código Delphi [-]
 
#include  
#include  
 
typedef int (__pascal *MYPROC)(int); 
 
int main(VOID) 
{ 
    int i;
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
 
    // handle de la DLL.
 
    hinstLib = LoadLibrary(TEXT("InterfazVisualLab.dll")); 
 
    
    if (hinstLib != NULL) 
    { 
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "Testear"); 
 
        
        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            i=(ProcAdd) (0); 
        }
 
        
        fFreeResult = FreeLibrary(hinstLib); 
    } 
 
    
    if (! fRunTimeLinkSuccess) 
        printf("Message printed from executable\n"); 
    return 0;
}

probe con visual basic de la siguiente manera:

Código Delphi [-]
 
Private Declare Function Testear Lib "InterfazVisualLab.dll" (ByVal a As Integer) As Integer
 
Private Sub Command1_Click()
    Testear (0)
End Sub

En los dos casos me da error :S

mensana 16-10-2007 16:43:25

Cita:

Empezado por mauqu (Mensaje 238865)
typedef int (__pascal *MYPROC)(int);

En Delphi le indicaste que era stdcall, en C++ también.

Prueba con la convención de llamada stdcall, o sus variantes según compilador, _stdcall o __stdcall (doble subrayado!)

aeff 16-10-2007 17:47:57

Saludos, mira mi versión:

Código de la DLL
Código Delphi [-]
library MyDll;

uses
  SysUtils,
  Windows,
  Classes,
  ActiveX,
  fTestear in 'fTestear.pas' {frmTestear};

{$R *.RES}

procedure Inicializar();
begin
  CoInitialize(nil);
end;

procedure Testear(Value: Integer); stdcall;
begin
  Inicializar();
  if not Assigned(frmTestear) then
    frmTestear := TfrmTestear.Create(nil);
  frmTestear.Caption := IntToStr(Value);
  frmTestear.ShowModal;
  frmTestear.Release;
end;

  exports Testear;
end.



Ahora, en el codigo C++ esta es mi version, no esta muy arregalda para contrarrestar excepciones pero creo que eso debes saberlo hacer:

Código:

//---------------------------------------------------------------------------

#pragma hdrstop

#include "Windows.hpp"
#include "Classes.hpp"
//---------------------------------------------------------------------------

#pragma argsused

  typedef int (__stdcall *TTestear)(int Value);

void main()
{
  int DllHinst;
  DllHinst = (int)LoadLibrary("E:/TitOSoft/Inventando/Dll/MyDll.dll");
  TTestear Testear;
  Testear = (TTestear)GetProcAddress((int*)DllHinst, "Testear");
  Testear(55);
}
//---------------------------------------------------------------------------



Espero que entiendas lo que hice,


Saludos
Aeff!


La franja horaria es GMT +2. Ahora son las 09:38:23.

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