[firebase-br] Fwd: Criando uma UDF no Firebird 2.1

Pha lista em pha.com.br
Ter Out 27 08:51:56 -03 2009


Tente declarar a função com CDECL.

Exemplo:

interface

function F_Mod(var iNum1, iNum2: Int64): Int64; cdecl; export;


implementation

uses
  SysUtils, Math;

{
  Retorna o resto da divisao
}
function F_Mod(var iNum1, iNum2: Int64): Int64;
begin
  try
    if (iNum1 = 0) or (iNum2 = 0) then begin
      Result := 0;
      Exit;
    end;
    Result := iNum1 mod iNum2;
  except
    Result := 0;
  end;
end;



Você tem que declarar a mesma no banco também.

Exemplo:

declare external function FMod
  Numeric(18, 0), Numeric(18, 0)
  returns
  Numeric(18, 0) by value
  entry_point 'F_Mod' module_name 'udfpha';


2009/10/26 Eliseu Corrona <eliseucorrona em jbsoft.com.br>

>  Olá amigos, tudo bem?
> Preciso criar uma UDF no Firebird 2.1, porém, apesar de seguir todos os
> passos de alguns exemplos que encontrei na internet, consegui criá-la porém
> não executá-la.
>
> O exemplo que criei é simples. Criei uma Dll no delphi para retornar o
> módulo de um número e postei todos os passos abaixo.
> É gerado um erro na execução e não teve jeito de fazer funcionar. Li alguns
> relatos no fórum sobre o mesmo erro. O meu sistema operacional é o Windows
> XP 32bits e o Firebird é a versão 2.1.218118.
>
> Grato se puderem ajudar.
>
>
>
> *Unit de funções da DLL:*
>
> unit UdfFunctions;
>
> interface
>
> function Modulo(var i, j: Integer): Integer; cdecl; export;
>
> implementation
>
> function Modulo(var i, j: Integer): Integer;
> begin
>  if (j = 0) then
>    result := -1 // just check the boundary condition, and
>    // return a reasonably uninteresting answer.
>  else
>    result := i mod j;
> end;
>
> end.
>
>
> *Declaração da DLL:*
> library DllUtils;
>
> { 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,
>  UdfFunctions in 'UdfFunctions.pas';
>
>
>
> {$R *.res}
>
>
> exports
>  Modulo;
>
> begin
>
> end.
>
>
> *Código para criar a UDF no firebird:*
> declare external function f_Modulo integer, integer
> returns
> integer free_it
> entry_point 'Modulo' module_name 'DllUtils';
>
>
> *SQL de execução:*
> select f_Modulo(3, 2) from rdb$database;
>
> *Erro gerado:*
> Invalid token.
> invalid request BLR at offset 60.
> function F_MODULO is not defined.
> module name or entrypoint could not be found.
>
>
> ______________________________________________
> FireBase-BR (www.firebase.com.br) - Hospedado em www.locador.com.br
> Para saber como gerenciar/excluir seu cadastro na lista, use:
> http://www.firebase.com.br/fb/artigo.php?id=1107
> Para consultar mensagens antigas: http://firebase.com.br/pesquisa
>



-- 
Paulo Henrique Albanez (PHA)
VirtualSystem Informática Ltda
Nova Odessa / SP - Brazil



Mais detalhes sobre a lista de discussão lista