Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Coloboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #4  
Antiguo 22-04-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Poder: 23
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Francisco,

Cita:
Empezado por Francisco
...Uso de Dynamic-Link Library (DLL)...me gustaría si alguien tiene algún tutorial de como utilizarlas correctamente y sacarles el maximo beneficio...


Revisa esta código:
Código Delphi [-]
library MathDLL;

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  StrUtils;

{$R *.res}

// Suma dos números reales
function SumNum(N1, N2 : Double) : Double; stdcall;
begin
   Result := N1 + N2;
end;

// Resta dos números reales
function SubNum(N1, N2 : Double) : Double; stdcall;
begin
   Result := N1 - N2;
end;

// Multiplica dos números reales
function MulNum(N1, N2 : Double) : Double; stdcall;
begin
   Result := N1 * N2;
end;

// Divide dos números reales
function DivNum(N1, N2 : Double) : Double; stdcall;
begin
   Result := N1 / N2;
end;

exports
   SumNum,
   SubNum,
   MulNum,
   DivNum;

begin
end.
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit3: TEdit;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // Funciones de DLL implementadas de forma estática
  function SumNum(N1, N2 : Double) : Double; stdcall; external 'MathDLL.dll';
  function SubNum(N1, N2 : Double) : Double; stdcall; external 'MathDLL.dll';
  function MulNum(N1, N2 : Double) : Double; stdcall; external 'MathDLL.dll';
  function DivNum(N1, N2 : Double) : Double; stdcall; external 'MathDLL.dll';

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
   N1, N2 : Double;

begin
   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);
   Edit3.Text := FloatToStr(SumNum(N1,N2));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   N1, N2 : Double;

begin
   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);
   Edit3.Text := FloatToStr(SubNum(N1,N2));
end;

procedure TForm1.Button3Click(Sender: TObject);
var
   N1, N2 : Double;

begin
   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);
   Edit3.Text := FloatToStr(MulNum(N1,N2));
end;

procedure TForm1.Button4Click(Sender: TObject);
var
   N1, N2 : Double;

begin
   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);
   Edit3.Text := FloatToStr(DivNum(N1,N2));
end;

end.
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit3: TEdit;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // Funciones de DLL implementadas de forma dinámica
  TSumNum = function (N1, N2 : Double) : Double; stdcall;
  TSubNum = function (N1, N2 : Double) : Double; stdcall;
  TMulNum = function (N1, N2 : Double) : Double; stdcall;
  TDivNum = function (N1, N2 : Double) : Double; stdcall;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
   N1, N2 : Double;
   hDLL : THandle;
   SumNum : TSumNum;

begin

   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);

   hDLL := LoadLibrary('MathDLL.dll');
   if hDLL <> 0 then
   begin
     @SumNum := GetProcAddress(hDLL, 'SumNum');
     if @SumNum <> nil then
     begin
        Edit3.Text := FloatToStr(SumNum(N1,N2));
        FreeLibrary(hDLL);
     end
     else
        MessageDlg('Error en Llamada de Función/Procedimiento',mtInformation,[mbOK],0);
   end
   else
      MessageDlg('Error en LoadLibrary',mtInformation,[mbOK],0);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
   N1, N2 : Double;
   hDLL : THandle;
   SubNum : TSubNum;

begin

   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);

   hDLL := LoadLibrary('MathDLL.dll');
   if hDLL <> 0 then
   begin
     @SubNum := GetProcAddress(hDLL, 'SubNum');
     if @SubNum <> nil then
     begin
        Edit3.Text := FloatToStr(SubNum(N1,N2));
        FreeLibrary(hDLL);
     end
     else
        MessageDlg('Error en Llamada de Función/Procedimiento',mtInformation,[mbOK],0);
   end
   else
      MessageDlg('Error en LoadLibrary',mtInformation,[mbOK],0);

end;

procedure TForm1.Button3Click(Sender: TObject);
var
   N1, N2 : Double;
   hDLL : THandle;
   MulNum : TMulNum;

begin

   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);

   hDLL := LoadLibrary('MathDLL.dll');
   if hDLL <> 0 then
   begin
     @MulNum := GetProcAddress(hDLL, 'MulNum');
     if @MulNum <> nil then
     begin
        Edit3.Text := FloatToStr(MulNum(N1,N2));
        FreeLibrary(hDLL);
     end
     else
        MessageDlg('Error en Llamada de Función/Procedimiento',mtInformation,[mbOK],0);
   end
   else
      MessageDlg('Error en LoadLibrary',mtInformation,[mbOK],0);

end;

procedure TForm1.Button4Click(Sender: TObject);
var
   N1, N2 : Double;
   hDLL : THandle;
   DivNum : TDivNum;

begin

   N1 := StrToFloatDef(Edit1.Text,0);
   N2 := StrToFloatDef(Edit2.Text,0);

   hDLL := LoadLibrary('MathDLL.dll');
   if hDLL <> 0 then
   begin
     @DivNum := GetProcAddress(hDLL, 'DivNum');
     if @DivNum <> nil then
     begin
        Edit3.Text := FloatToStr(DivNum(N1,N2));
        FreeLibrary(hDLL);
     end
     else
        MessageDlg('Error en Llamada de Función/Procedimiento',mtInformation,[mbOK],0);
   end
   else
      MessageDlg('Error en LoadLibrary',mtInformation,[mbOK],0);

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32, Crea una DLL con funciones matemáticas básicas e implementa la misma en formas estática y dinámica, como se puede ver en la siguiente imagen:



El ejemplo esta disponible en el siguiente link : Example DLL Static vs Dinamic.rar

Revisa esta información:
Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 22-04-2015 a las 22:21:28.
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
label link link???? darkcuevas Varios 6 13-04-2011 21:17:37
Dynamic Sql Error -901 trex2000 Conexión con bases de datos 2 16-09-2008 09:37:04
Dynamic sql error -303 amkalzada Conexión con bases de datos 2 27-06-2008 11:12:33
Dynamic Sql error -303 amkalzada Firebird e Interbase 1 16-06-2008 15:37:51
dynamic Sql Error digital Firebird e Interbase 1 03-03-2004 09:48:26


La franja horaria es GMT +2. Ahora son las 17:57:46.


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
Copyright 1996-2007 Club Delphi