DLL feita Delphi XE2 passando PAnsiChar p/ prog. em xHarbour

Projeto [x]Harbour - Compilador de código aberto compatível com o Clipper.

Moderador: Moderadores

etag
Usuário Nível 1
Usuário Nível 1
Mensagens: 1
Registrado em: 30 Set 2014 11:57
Localização: São Paulo / SP

DLL feita Delphi XE2 passando PAnsiChar p/ prog. em xHarbour

Mensagem por etag »

Bom dia! Tenho uma DLL feita no Delphi XE2 com uma função que retorna o conteúdo de um arquivo XML como PAnsiChar (se tento utilizar WideString tenho o erro "Out of Memory"), e estou tentando consumir esta função no xharbour (no próprio Delphi funciona redondinho). O que ocorre é que no xHarbour o retorno chega na forma de caracteres estranhos. Alguém tem alguma idéia do que pode estar ocorrendo, ou o que fazer para resolver esta questão? Agradeço desde já!
Avatar do usuário
asimoes
Colaborador
Colaborador
Mensagens: 4919
Registrado em: 26 Abr 2007 16:48
Localização: RIO DE JANEIRO-RJ

DLL feita Delphi XE2 passando PAnsiChar p/ prog. em xHarbour

Mensagem por asimoes »

Em harbour eu faço assim:

Código: Selecionar todos

   #include "hbdyn.ch"
   cStatus:=hb_dynCall( { "DiaSemana", "Project2.dll",hb_bitOr( HB_DYN_CTYPE_CHAR_PTR , HB_DYN_CALLCONV_STDCALL )},HB_DTOC(Date(),'DD/MM/YYYY'))
   info(cStatus)
A dll em delphi.

Código: Selecionar todos

library Project2;

{ 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,
  WinTypes,
  WinProcs,
  Dialogs;

{declaração de uma função MaiorValor}

{$R *.res}
function Triple(N: Integer): Integer; stdcall;
begin
Result := N * 3;
end;

function Double (N: Integer): Integer; stdcall;
begin
Result := N * 2;
end;

function DiaSemana(Data: pchar): pAnsiChar; stdcall;
{Retorna o dia da semana em Extenso de uma determinada data}
var
var1:string;
const
  Dias : Array[1..7] of String[07] = ('DOMINGO', 'SEGUNDA', 'TERCA','QUARTA','QUINTA', 'SEXTA','SABADO');
begin
  //showmessage(Data);
  var1   := Dias[DayOfWeek(strtodate(Data))];
  Result := pchar('Dia da semana = '+var1);
end;

function TrimAllChar(const S:PChar; const ch : PChar) : pAnsiChar; stdcall;
var
  buf : String;
  var1: String;
  var2: String;
begin
  buf := S;
  Result := '';
  Var1   := '';
  {while Pos finds a blank}
  //showmessage(s);
  //showmessage(ch);
  while (Pos(ch, buf) > 0) do begin
    {copy the substrings before the blank in to Result}
    Var1   := Var1   + Copy(buf, 1, Pos(ch, buf) - 1);
    buf := Copy(buf, Pos(ch, buf) + 1, Length(buf) - Pos(ch, buf));
  end;
  var2:=var1;
  //showmessage(var2);
  {There will still be a remainder in buf, so copy remainder into Result}
  Result := pchar(Var2+ buf);
end;

function Nada(_s:pchar):PAnsiChar;stdcall;
var
teste: string;
teste2: string;
   begin
   teste:=_s;
   teste2:=_s;
   showmessage(teste);
   showmessage(inttostr(Length (_s)));
   showmessage('ESTOU NO DELPHI');
   Result:=pchar(teste);
end;

function pcDLLVersion:pchar;stdcall;
begin
showmessage('teste');
result:='1.0.0';
end;

exports
Triple, Double, TrimAllChar, DiaSemana, Nada, pcDLLVersion;
end.
►Harbour 3.x | Minigui xx-x | HwGui◄
Pense nas possibilidades abstraia as dificuldades.
Não corrigir nossas falhas é o mesmo que cometer novos erros.
A imaginação é mais importante que o conhecimento. (Albert Einstein)
Responder