Ver Mensaje Individual
  #10  
Antiguo 12-04-2011
Lizeth Lizeth is offline
Miembro
 
Registrado: ago 2005
Ubicación: Mexico D.F
Posts: 50
Reputación: 19
Lizeth Va por buen camino
Hola, bueno pues despúes de muchas semanas resolvimos el problema. Es en delphi 7. He aqui una posible solución para pasar el contenido de un puntero de un PSafeArray a String en hexadecimal y de String en hexadecimal a PSafeArray. Espero le sirva a alguien.

Código Delphi [-]
unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtnrs, OleServer, StdCtrls, ActiveX; // Activex más lo que se necesite
  
type
   TByteDynArray = array of Byte;//Declaro mi tipo TBytes 
   TBytes = TByteDynArray;
  TForm2 = class(TForm)   
    Button1: TButton;
    //...
  private
    { Private declarations }
    otoSafe, otoSafe2 : PSafeArray; //Mi variables tipo PSafeArray; 
  public
    { Public declarations }  
  
  end;
var
  Form2: TForm2;
implementation

{$R *.dfm}
 //-------------------------- funciones propias ------------------
function SafeArrayAsByteArray(Arr: PSafeArray): TBytes;
// Convierto de PSafeArray a TBytes
var
 LBound, UBound, i: Integer;
begin
 SafeArrayGetLBound(Arr, 1, LBound);
 SafeArrayGetUBound(Arr, 1, UBound);
 SetLength(result, UBound-LBound+1);
 for i := LBound to UBound do 
   SafeArrayGetElement(Arr,i,result[i]); 
end;

function ByteToHex(InByte: byte): shortstring;
// Pasa de Byte a un shortstring en hexadecimal
const Digits:array[0..15] of char='0123456789ABCDEF';
begin
   result:=digits[InByte shr 4] + digits[InByte and $0F];
end;

function ByteToStr(const Value: TBytes): String;
// Paso de Byte a String
var
    I: integer;
    S, CR : String;
    Letra: char;
begin
    S := '';
    for I := 0 to Length(Value)-1 do
     begin
        CR:= ByteToHex(Value[i])+':';
        S := S+CR;
    end;
    Result := S;
end;
function StrToByte(const Value: String): TBytes;
// Hacemos la inversa de mi string a TByte 
var
   I,j: integer;
   iPos  : integer;
   Hex : TBytes;
   sCaden, sCopia : string;
begin
   SetLength(Result, Length(Value)div 3);
    j:= 0;
    for I := 1 to Length(Value) - 1 do
    begin
      if Value[i] = ':' then
       begin
         Result[j]:= strToInt('$'+ sCaden);
         sCaden := '';
         inc(j);
       end
      else
       sCaden:= sCaden + Value[i];
    end;
end;
function ByteArrayAsSafeArray(Arr: TBytes): PSafeArray;
// La inversa de TBytes a PSafeArray;
var
  pvData: PByteArray;
  Bounds: TVarArrayBoundArray;
begin
   Bounds[0].LowBound:=0;
   Bounds[0].ElementCount:=Length(Arr);
   Result := SafeArrayCreate(varByte, 1, Bounds);
   SafeArrayAccessData(Result, Pointer(pvData));
   Move(Arr[0], pvData[0], Length(Arr));
end;
//--------------------------------------- funciones <-------------------------------
// Como las uso
procedure TForm2.Button2Click(Sender: TObject);
var 
   mibyte, prueba : TBytes;  
begin
  
    otoSafe   := coderResult2.Template; // yo tengo esto que es de tipo PSafeArray;
    mibyte    := SafeArrayAsByteArray(otoSafe);  
    Edit1.Text:= ByteToStr(mibyte);
    prueba    := StrToByte(Edit1.Text);//la inversa
    otoSafe2  := ByteArrayAsSafeArray(prueba);
    
end;
Responder Con Cita