Ver Mensaje Individual
  #14  
Antiguo 15-01-2008
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Una solución puede ser comprobar la existencias de las funciones antes de utilizarlas, para eso vamos a cargarlas de forma dinámica en vez de vincularlas a nuestro programa de forma estática, y si no las encontramos lanzamos una excepción que deberás de manejar en tu programa utilizando un bloque try ... except.

La cosa queda mas o menos así:
Código Delphi [-]
unit base64;

interface

uses Windows, SysUtils, Classes;

function BinToStr(Binary: PByte; Len: Cardinal): String;
procedure StrToStream(Str: String; Stream: TStream);

implementation

const
  CRYPT_STRING_BASE64HEADER = 0;
  CRYPT_STRING_BASE64 = 1;
  CRYPT_STRING_BINARY = 2;
  CRYPT_STRING_BASE64REQUESTHEADER = 3;
  CRYPT_STRING_HEX = 4;
  CRYPT_STRING_HEXASCII = 5;
  CRYPT_STRING_BASE64X509CRLHEADER = 9;
  CRYPT_STRING_HEXADDR = 10;
  CRYPT_STRING_HEXASCIIADDR = 11;
  CRYPT_STRING_HEXRAW = 12;
  CRYPT_STRING_NOCRLF = $40000000;
  CRYPT_STRING_NOCR = $80000000; 

function BinToStr(Binary: PByte; Len: Cardinal): String;
var
  Dll: HModule;
  Count: DWORD;
  CryptBinaryToString: function (pbBinary: PByte; cbBinary: DWORD; dwFlags: DWORD;
    pszString: PChar; var pcchString: DWORD): BOOL; stdcall;
begin
  Dll:= LoadLibrary('Crypt32.dll');
  if Dll <> 0 then
  try
    @CryptBinaryToString:= GetProcAddress(Dll,'CryptBinaryToStringA');
    if @CryptBinaryToString <> nil then
    begin
      Count:= 0;
      if CryptBinaryToString(Binary,Len,12,nil,Count) then
      begin
        SetLength(Result,Count);
        if not CryptBinaryToString(Binary,Len,12,PChar(Result),
          Count) then
          Result:= EmptyStr;
      end;
    end else
      raise Exception.Create('No encuentro CryptBinaryToStringA');
  finally
    FreeLibrary(Dll);
  end else
    raise Exception.Create('No encuentro Crypt32.dll');
end;

procedure StrToStream(Str: String; Stream: TStream);
var
  Dll: HModule;
  Buffer: PByte;
  Count: DWORD;
  CryptStringToBinary: function (pszString: PChar; cchString: DWORD; dwFlags: DWORD;
    pbBinary: PByte; var pcbBinary: DWORD; pdwSkip: PDWORD;
    pdwFlags: PDWORD): BOOL; stdcall;
begin
  Dll:= LoadLibrary('Crypt32.dll');
  if Dll <> 0 then
  try
    @CryptStringToBinary:= GetProcAddress(Dll,'CryptStringToBinaryA');
    if @CryptStringToBinary <> nil then
    begin
      Count:= 0;
      if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,nil,Count,
        nil,nil) then
      begin
        GetMem(Buffer,Count);
        try
          if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,Buffer,
            Count,nil,nil) then
            Stream.WriteBuffer(Buffer^,Count);
        finally
          FreeMem(Buffer);
        end;
      end;
    end else
      raise Exception.Create('No encuentro CryptStringToBinaryA');
  finally
    FreeLibrary(Dll);
  end else
    raise Exception.Create('No encuentro Crypt32.dll');
end;

end.

Y cuando la uses utiliza algo como esto:
Código Delphi [-]
try
  BinToStr( ...
except
  // Si llegamos hasta aqui es que las funciones no existen
end;

¿Que te parece? es solo una improvisación pero puede servir.
Responder Con Cita