Dephi Callback Example

Example:

type
  IKeyCallback = class(ICryptKeyCallbackImpl)
  private
    fKey : String;
    function callback(dataLength: Cardinal; data: Pointer; bufferLength: Cardinal; buffer: Pointer): Cardinal; override;
  end;


function IKeyCallback.callback(dataLength: Cardinal; data: Pointer; bufferLength: Cardinal; buffer: Pointer): Cardinal;
Var b:TBytes;
begin
  b:=TEncoding.ASCII.GetBytes(fKey);
  If (bufferLength > 0) and (buffer <> nil) then begin
    Result := Length(b);
    Move(b[0],buffer^,Result);
  end else
    Result := 0;
end;
{
function TFXClientLibrary.Call_attach_database(Const db_name,db_crypt_key:String;Const db_handle:PISC_DB_HANDLE;Const parm_buffer_length:FXShort;Const parm_buffer:PFXByte): ISC_STATUS;
Var key:IKeyCallback;
    cb:Pointer ABSOLUTE key;
    lDBN:FXShort;
    sDBN:TBytes;
Begin
  sDBN := TEncoding.Convert(TEncoding.Default, TEncoding.ANSI, BytesOf(db_name));
  lDBN := length(sDBN);

  if (db_crypt_key<>EmptyStr)and(Assigned(fdatabase_crypt_callback)) then Begin
    key:=IKeyCallback.create;key.fKey:=db_crypt_key;
Result:=fdatabase_crypt_callback(@fStatusVector,cb);
    if Result>0 Then
      Exit;
    end;

  Result:=fisc_attach_database( @fStatusVector,
                                   lDBN,
                                   PFXByte(sDBN),
                                   db_handle,
                                   parm_buffer_length,
                                   parm_buffer
                                   );
  // key is released
end;