PChar em DLL
Bom dia a todos....
Estou com muitas dificuldades para manipular um PChar, andei lendo alguns topicos aqui mesmo no Forum, mas nao tive muito sucesso!
Preciso criar uma dll que efetue uma busca em um arquivo DBF e retorne alguns dados no em um parametro PChar.
Veja o codigo da DLL abaixo:
Do lado do aplicativo, a função retorna 0 se a busca for bem sucedida e 1 caso contrário!
Alem de escrever no form o resultado, estou tentando mostrar os dados do produto no Caption do form, mas nao estou tendo sucesso nesta tarefa.
Veja o codigo do aplicativo:
[size=18:0580eef549]Fico feliz se algum amigo ao menos apontar uma boa referencia de estudo sobre PChar.[/size:0580eef549]
Abraços :wink:
Estou com muitas dificuldades para manipular um PChar, andei lendo alguns topicos aqui mesmo no Forum, mas nao tive muito sucesso!
Preciso criar uma dll que efetue uma busca em um arquivo DBF e retorne alguns dados no em um parametro PChar.
Veja o codigo da DLL abaixo:
library VidaLink;
{ 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, pass string information
using PChar or ShortString parameters. }
uses
SysUtils, Classes, Dbf, StrUtils, DB, Dialogs;
{$R *.res}
function DadosProduto(sCateg, sKey: PChar; byTrataPeso, byDeciPeso, byArredonda,
byDescr40: Byte; ptrBuffProd: PChar;ptrBuffAssoc: PChar): ShortInt; stdcall;
var
Dbf: TDBF;
Str: String;
PrV: String;
PrP: String;
begin
Dbf := TDBF.Create(nil);
try
Dbf.TableName := ´G:\Prog\RCA\Estoque.dbf´;
DBF.Open;
Str := sKey;
if Dbf.Locate(´ECODBARRA´,Str,[]) then
begin
PrV := FormatFloat(´00000000.00´,DBF.FieldByName(´EPRVENDA´).AsFloat);
Delete(PrV,9,1);
PrP := FormatFloat(´00000000.00´,DBF.FieldByName(´EPRPROMO´).AsFloat);
Delete(PrP,9,1);
ptrBuffProd := StrAlloc(66);
ptrBuffProd := PChar(LeftStr(DBF.FieldByName(´EDESCRICAO´).AsString,35) +
PrV + PrP);
Result := 0;
ShowMessage(ptrBuffProd)
end
else
begin
Result := 1;
end;
finally
DBF.Close;
FreeAndNil(DBF);
end;
end;
exports DadosProduto;
end.Do lado do aplicativo, a função retorna 0 se a busca for bem sucedida e 1 caso contrário!
Alem de escrever no form o resultado, estou tentando mostrar os dados do produto no Caption do form, mas nao estou tendo sucesso nesta tarefa.
Veja o codigo do aplicativo:
procedure TForm1.Button1Click(Sender: TObject); var p1: pchar; //p1: array [0..65] of char; p2: pchar; Str: String; begin //Str := ´7896509946269´; //T.VIENA H GEL CAST CLARO Str := ´7898049799008´; //AASEDATIL 100MG INF CX 200 COMP //p1 := StrAlloc(66); p2 := StrAlloc(66); Canvas.TextOut(10,10,´Retorno: ´ + IntToStr(DadosProduto(´´,PChar(str),0,0,0,0,p1,p2))); Str := StrPas(p1); Caption := Str; //Str := strpp1; //StrDispose(p1); StrDispose(p2); end;
[size=18:0580eef549]Fico feliz se algum amigo ao menos apontar uma boa referencia de estudo sobre PChar.[/size:0580eef549]
Abraços :wink:
Aloizio Castro
Curtidas 0
Respostas
Massuda
28/03/2007
Aloque os buffer na rotina que chama a DLL; a rotina na DLL deve simplesmente usar os buffer que ela recebe, sem se preocupar em alocar buffer.
GOSTEI 0