PDA

Ver la Versión Completa : Pasar Esta Funcion de Calculo de edad a dll


webmasterplc
09-04-2014, 07:00:02
Buenas la consulta es como hago para pasar una función a una dll aca esta la funcion es especificamente para calcular antiguedad o la edad, es para hacer una dll y colocarla a los proyectos y solo pasarle parametros de fecha y ella regrese la edad la funcion es esta.

Código Delphi [-] (http://clubdelphi.com/foros/#)function anosMesesDias(fechaInicio : TDate; fechaFin : TDate) : string; var anos, meses, dias, m : Integer; begin anos := YearOf (fechaFin) - YearOf (fechaInicio); if MonthOf (fechaInicio) > MonthOf (fechaFin) then anos := anos - 1; if MonthOf (fechaFin) < MonthOf (fechaInicio) then meses := 12 - MonthOf (fechaInicio) + MonthOf (fechaFin) else meses := MonthOf (fechaFin) - MonthOf (fechaInicio); if DayOf (fechaFin) < DayOf (fechaInicio) then begin meses := meses - 1; if MonthOf (fechaFin) = MonthOf (fechaInicio) then begin anos := anos - 1; meses := 11; end; end; dias := DayOf (fechaFin) - DayOf (fechaInicio); if dias < 0 then begin m := MonthOf (fechaFin) - 1; if m = 0 then m := 12; case m of 1, 3, 5, 7, 8, 10, 12 : dias := 31 + dias; 4, 6, 9, 11 : dias := 30 + dias; 2 : begin if ((YearOf(fechaFin) mod 4 = 0) and (YearOf(fechaFin) mod 100 <> 0)) or (YearOf(fechaFin) mod 400 = 0) then dias := 29 + dias else dias := 28 + dias; end; end; end; result := IntToStr (anos) + ' años, ' + IntToStr (meses) + ' meses, ' + IntToStr (dias) + ' días'; end;

ecfisa
09-04-2014, 09:41:44
Hola webmasterplc.

No es muy complicado, declara la DLL de este modo:

library FuncFechDLL; // (el nombre que desees)

{$R *.res}

uses
SysUtils, Classes,
Controls, DateUtils; // las necesita tu código

// Aquí pone el código de tu función (Fijate que cambié el resultado de string a PChar)
function AnosMesesDias(fechaInicio: TDate; fechaFin: TDate) : PChar; stdcall;
var
anos, meses, dias, m : Integer;
begin
...
Result:= PChar(IntToStr(anos)+' años, '+IntToStr(meses)+' meses, '+IntToStr(dias)+' días');
end;

exports AnosMesesDias;

begin

end.

y compilala.

Ejemplo de uso:

...
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;

var
Form1: TForm1;

implementation {$R *.dfm}

// Esta linea declara a AnosMesesDias como una función
// externa que deberá ser buscada en la DLL
function AnosMesesDias(fechaInicio: TDate; fechaFin: TDate): PChar;
stdcall external 'FuncFechDLL.dll'

procedure TForm1.Button1Click(Sender: TObject);
begin
Caption:= AnosMesesDias(StrToDate('15/08/2003'), StrToDate('09/04/2014'));
end;

El ejemplo supone la dll en el mismo sitio que el ejecutable, también podes ubicarla en la ruta del sistema para que pueda ser accedida desde cualquier ruta.

No voy a explayarme mas en detalles ya que desde hace años exísten muchos artículos, tutoriales y videos que lo hacen de manera excelente. Te pongo algunos enlaces externos ya que supongo que has buscado en nuestros foros y no hallaste al respecto... :rolleyes:

How To Create And Call A Dynamic Link Library (DLL) In Delphi (http://www.onlinedelphitraining.com/newsletters/dllexample.htm)
Crear dll (librería) en Delphi (http://www.ajpdsoft.com/modules.php?name=News&file=article&sid=261)
Crear una DLL en Delphi (http://www.programa.128-k.com/es/delphi-creeacion-dll.php)
Creating and Using DLLs from Delphi (http://delphi.about.com/od/windowsshellapi/a/dll_basics.htm)
Static vs. Dynamic Dynamic Link Library Loading - A Comparison (http://delphi.about.com/od/windowsshellapi/a/delphi-dll-loading-static-dynamic.htm)
Creation of an Unmanaged DLL in Delphi (http://edn.embarcadero.com/article/40311)


Saludos :)

webmasterplc
09-04-2014, 12:23:22
Gracias hermano ya este ejemplo me sirve para extenderme saludos