Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Generar evento en libreria (https://www.clubdelphi.com/foros/showthread.php?t=86966)

ander 29-10-2014 15:24:52

Generar evento en libreria
 
Hola a todos.
Lo que quiero hacer es generar un evento de un componente existente (ej. OnTimer) en una libreria.
Como podria hacerlo ?? He generado esta libreria y no doy pico en bola.
Si alguien pudiese decirme como se hace con un ejemplo se lo agradeceria.
Código Delphi [-]

library My_Dll;

 uses
  System.SysUtils,
  System.Classes,winapi.windows,Vcl.Dialogs,Vcl.ExtCtrls;

 var i:Integer;

{$R *.res}

procedure TiemTimer;
begin
 inc(i);
 ShowMessage(inttostr(i)) ;
end;

procedure pruebaTiempo ; stdcall;
var
 Tiempo:TTimer;
 pp:TComponent;
begin
  i:=0;
  Tiempo:=TTimer.Create(pp);                // Esto no creo que esté bien
  Tiempo.Name:='Tiempo'+IntToStr(I);
  tiempo.Interval:=5000;
  tiempo.Enabled:=True;
  tiempo.OnTimer:=TiemTimer;               // Error: Incompatible types: 'method pointer and regular procedure'
end;                                                

exports pruebaTiempo;

begin

end.

Gracias de antemano

Neftali [Germán.Estévez] 29-10-2014 17:34:43

Tal vez algo así es lo que necesitas...
No acabo de tener claro lo que quieres hacer, pero esto creo que hace lo que pretendes.

Código Delphi [-]
library My_DLL;

uses
  SysUtils,
  Classes, windows, Dialogs, ExtCtrls, forms;

type
  TestClass = class
  private
    Tiempo:TTimer;
    procedure MyOnTimer(Sender:TObject);
  public
    procedure init();
  end;

var
  i:Integer;
  t:TestClass;
  running:boolean;

{$R *.res}


procedure Finalizar; stdcall;
begin
  t.Tiempo.Enabled := False;
  running := true;
  t.Free;
end;

procedure pruebaTiempo ; stdcall;
begin
  t := TestClass.Create();
  running := true;
  t.init;
end;

procedure TestClass.init;
begin
  Tiempo := TTimer.Create(nil);
  i:=0;
  // Tiempo.Name:='Tiempo'+IntToStr(I);
  tiempo.Interval:=2000;
  tiempo.OnTimer := MyOnTimer;
  tiempo.Enabled:=True;  
end;


procedure TestClass.MyOnTimer(Sender: TObject);
begin
  inc(i);
  ShowMessage(inttostr(i)) ;

end;

exports pruebaTiempo;
exports Finalizar;

end.

(1) El componente puedes crearlo pasando un nil, pues no "pertenece" a ningún formulario.
(2) En cuanto al segundo error te está diciendo que el evento que asignes debe pertenecer a una clase.

Neftali [Germán.Estévez] 29-10-2014 17:35:16

Para hacer la llamada; Un proyecto con lo siguiente:

Código Delphi [-]
...

  procedure pruebaTiempo(); cdecl;  external 'My_DLL.dll';
  procedure Finalizar(); cdecl;  external 'My_DLL.dll';

var
  Form1: TForm1;

implementation

procedure TForm1.Iniciar(Sender: TObject);
begin
  pruebaTiempo;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Finalizar;
end;


La franja horaria es GMT +2. Ahora son las 21:04:47.

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