Ver Mensaje Individual
  #3  
Antiguo 13-05-2010
Aldo Aldo is offline
Miembro
 
Registrado: ene 2004
Posts: 46
Reputación: 0
Aldo Va por buen camino
OK. Creo que lo mejor es lo que dices.

Aquí os va part edel código que tiene una clase para tratar un String y poder convertirlo en Pchar para que sea devuleto por la UDF en cuestión.

Código Delphi [-]
...
Type
   TUDFString = class
  private
    FString: PChar;
    FSize: Integer;
    FLength: Integer;
    procedure SetPChar(Value: PChar);
    function GetString: String;
    procedure SetString(Value: String);
    procedure SetSize(Value: Integer);
  public
    constructor Create;
    destructor Destroy; override;
    property AsString: String read GetString write SetString;
    property AsPChar: PChar read FString write SetPChar;
    property StrLength: Integer read FLength;
    property StrSize: Integer read FSize write SetSize;
  end;
...
threadvar
  gResult: TUDFString;
...

implementation

const
  ShrinkLen = 100;

constructor TUDFString.Create;
begin
  FString := nil;
  SetSize(ShrinkLen);
  FLength := 0;
end;

destructor TUDFString.Destroy;
begin
  if (Self <> nil) and (FSize > 0) then
    ReallocMem(FString, 0);
  inherited;
end;

procedure TUDFString.SetPChar(Value: PChar);
begin
  FLength := StrLen(Value);
  SetSize(FLength);
  StrCopy(FString, Value);
end;

function TUDFString.GetString: String;
begin
  result := String(FString);
end;

procedure TUDFString.SetString(Value: String);
begin
  if Self <> nil then begin
    FLength := Length(Value);
    SetSize(FLength);
    StrPCopy(FString, Value);
  end;
end;

procedure TUDFString.SetSize(Value: Integer);
begin
  if (Value > FSize) or
     (Value <= FSize - ShrinkLen) then begin
    ReallocMem(FString, Value + 1);
    FSize := Value;
  end;
end;
...

Ahora la declaración de la UDF

Código Delphi [-]
function T_AlgoToStr( var nFVToCheck: Integer ): PChar; cdecl; export;

La implemetación de dicha función. Se asume que la variable gResult se ha creado en Initialization section de la DLL.

Código Delphi [-]
function T_AlgoToStr( var nFVToCheck: Integer ): PChar; cdecl; export;
   function IsValueOk( nValue: Integer ): Boolean;
   begin
      .....
      Result := True;
   end;
   
   function GetAlgoAsStr( nValue: Integer ): String;
   begin
      case nValue of
         1 : Result := 'Cadena 1' ;
         ...
         else
            Result := 'Cadena else';   
      end;
begin
   Result           := '';
   if  not IsValueOk( nFVToCheck ) then
      Exit;
   
   gResult.AsString := GetAlgoAsStr( nFVToCheck ); 
   Result           := gResult.AsPChar; 
end;

NOTA: Esto funciona correctamente en Delphi 5 pero no en Delphi 2010 que en vez de devolver la cadena completa solo devuelte el primer elemento de la cadena.
Responder Con Cita