Ajuda - Busca - Membros - Calendário
Versão Completa: (Resolvido) Colocar uma string na DLL
Fórum Script Brasil > Programação & Desenvolvimento > Delphi, Kylix
Aleksander
Olá amigos, abaixo tem o exemplo de uma Dll que pequei aqui no forum, troquei algumas letras e inseri o Clear e o SetFocus no Edit, essa Dll possui números e funciona perfeitamenta.

A Dll:
CODE
Library Serial;
function Chave(Senha : integer): integer; export;
begin
Senha := 123;
Chave := Senha;
end;
exports
Chave;
begin
end.


Para usar:

CODE
Function Chave(Senha : Integer):Integer; external 'Serial.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
N, C : Integer;
begin
N := StrToInt(Edit1.Text);
C := Chave(StrToInt(Edit1.Text));
if N = C then
ShowMessage('Serial válido, você acertou!')
else
ShowMessage('Serial inválido!');
edit1.Clear;
edit1.SetFocus;
end;
end.


Desejo colocar uma palavra no lugar dos números, funciona e depois dá erro.
Fiz assim:

A Dll alterada por mim:

CODE
Library Senha;
function Chave(Senha : string):string; export;
begin
Senha := 'Delphi';
Chave := Senha;
end;
exports
Chave;
begin
end.



Para usar (se funcionasse é claro)

CODE
Function Chave(Senha : string):string; external 'Senha.dll';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
N, C : string;
begin
N :=Edit1.Text;
C := Chave(Edit1.Text);
if N = C then
ShowMessage('Serial válido, você acertou!')
else
ShowMessage('Serial inválido!');
edit1.Clear;
edit1.SetFocus;
end;
end.


Depois do clique no botão da erro com essa mensagem:
Project1.exe raised exception class EinvalidPointer with message ‘Invalid pointer operation’. Process stopped. Use Step or Run to continue.

Pergunto:
Como onde está o meu erro?

Grato a todos pela atenção


Jhonas
QUOTE

Depois do clique no botão da erro com essa mensagem:
Project1.exe raised exception class EinvalidPointer with message ‘Invalid pointer operation’. Process stopped. Use Step or Run to continue.
Voce quando cria uma dll inicialmente ela te da um Aviso importante

QUOTE

passe informação de uma string usando PChar ou parâmetros de ShortString.


então voce não pode utilizar string na passagem de parametros e sim Pchar.. desta maneira para o seu codigo funcionar sem erros, voce deve fazer estas modificações:


No EXE

CODE
implementation

{$R *.dfm}
Function Chave(Senha : Pchar):Pchar; external 'Senha.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
   N, C : String;
begin

   N := Pchar(Edit1.Text);
   C := Pchar(Chave(Pchar(Edit1.Text)));
   if N = C then
      ShowMessage('Serial válido, você acertou!')
   else
      ShowMessage('Serial inválido!');

   edit1.Clear;
   edit1.SetFocus;
end;


Na DLL

CODE
library Senha;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, [b]pass string information
  using PChar or ShortString parameters[/b]. }

uses
  SysUtils,
  Classes;

{$R *.RES}

function Chave(Senha : Pchar):Pchar; export;
begin
   Senha := 'Delphi';
   Chave := Senha;
end;

exports
Chave;

begin
end.


abraço
Aleksander
Jhonas, agora funcionou.
Muito obrigado.

Grande abraço!
Esta é uma versão simplificada de nosso conteúdo principal. Para ver a versão completa com maiores informações, formatação e imagens, por favor clique aqui.
Invision Power Board © 2001-2012 Invision Power Services, Inc.