Criação de dll

08/05/2003

0

Bom dia pessoal. estou começando a estudar sobre como cria dll. peguei um exemplo e estou tentando desenvolver um projetinho do tipo.. inicia o form principal, ele clica num botão chamado receber, este botão carrega a dll(esta dll tem uma função para abrir o formulário e retornar um texto),
este texto é passado ao formulário principal dinovo.

Aí está todo meu código..

Parte da criação da dll

Testandodll.dpr
library testandodll;
uses
SysUtils,
Classes,
testedll in ´testedll.pas´ ;
exports enviardll;
begin
end.

Testandodll.pass
unit testedll;

interface

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

type

Tfrmenviodll = class(TForm)
edtexto: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmenviodll: Tfrmenviodll;
function enviardll(AHandle:Thandle,ACaption:string):string; stdcall;

implementation

{$R *.DFM}
function enviardll(AHandle:Thandle,ACaption:string):string; stdcall;
var
frmenviodll: Tfrmenviodll;
begin
Application.Handle:= AHandle;
frmenviodll:= tfrmenviodll.Create(application);
try
frmenviodll.Caption:= ACaption;
frmenviodll.ShowModal;
result:= frmenviodll.edtexto.Text;
finally
frmenviodll.Free;
end;
end;


procedure Tfrmenviodll.Button1Click(Sender: TObject);
begin
close;
end;

end.

Agora o projeto com o formulário principal que chama a dll

Testandodllmain.pas

unit testedllmain;

interface

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

type
Tenviardll= function (AHandle:tHandle;ACaption:string):string;stdcall;
Edllloaderror= class(exception);
Tfrmrecebe = class(TForm)
btnreceber: TButton;
Label1: TLabel;
procedure btnreceberClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmrecebe: Tfrmrecebe;

implementation

{$R *.DFM}

procedure Tfrmrecebe.btnreceberClick(Sender: TObject);
var
LibHandle : THandle;
enviardll: Tenviardll;
begin

{ Attempt to load the DLL }
LibHandle := LoadLibrary(´testandodll.DLL´);
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create(´Unable to Load DLL´);
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL´s exported function so that it can be called. }
@enviardll := GetProcAddress(LibHandle, ´enviardll´);
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@enviardll = nil) then
label1.Caption := enviardll(Application.Handle,Caption)
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;

end.



Tá dando erro de acesso de violação de endereço...coisa deste tipo

Será que poderia me esclarecer esta dúvida?
obrigado
Eric


Eric.miranda

Eric.miranda

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar