(*****************************************************************************)
(* MODULE: BTRINTRF*                                                         *)
(* RIGHTS: Reggatta Systems - COPYRIGHT 1995 - 2002                          *)
(* DESCR.: This module contains the interface to Btrieve                     *)
(*****************************************************************************)
Unit BtrIntrf;
{$I BSE.INC}
{$I BAPI.INC}

Interface


Type
  TBtrCallFunc =
  Function
    (
      Operation    : SmallInt;
      Var posblk;
      Var databuf;
      Var datalen  : LongInt;
      Var keybuf;
      keylen       : ShortInt;
      keynum       : ShortInt
    ) : SmallInt;
    StdCall;

  TBtrCallIDFunc = 
  Function
    (
      Operation    : SmallInt;
      Var posblk;
      Var databuf;
      Var datalen  : LongInt;
      Var keybuf;
      keylen       : ShortInt;
      keynum       : ShortInt;
      Var ClientID
    ) : SmallInt;
  StdCall;


  TWBtrvInitFunc =
  Function
  (
     IntializationString : PChar
  ): SmallInt;
  StdCall;

  TWBtrvStopFunc = Function : SmallInt;
    StdCall;


Var
  BtrCall           : TBtrCallFunc;
  BtrCallId         : TBtrCallIDFunc;
  WBtrvInit         : TWBtrvInitFunc;
  WBtrvStop         : TWbtrvStopFunc;

Function BtrvInit( InitializationString, DLL : PChar ): SmallInt;


Function BtrvStop: SmallInt;

Implementation
Uses
  Windows,
  Dialogs,
  SysUtils;

{$I BTRIEVE.INC}

Const
  DLLNAME = 'wbtrv32.dll';

Const
  BtrvLibraryHandle : THandle = HINSTANCE_ERROR;

  {..$DEFINE MAKE_THREAD_SAFE}

Function BtrvInit( InitializationString, DLL : PChar ): SmallInt;
Var
  FCSect   : TRTLCriticalSection;
begin
  Result := ISAM_NOT_INITIALIZED;
  InitializeCriticalSection( FCSect );
  Try
    EnterCriticalSection( FCSect );
    Try
      If ( BtrvLibraryHandle <= HINSTANCE_ERROR ) then
      begin
        if ( DLL <> NIL ) and ( StrLen( DLL ) > 0 ) then
          BtrvLibraryHandle := LoadLibrary( DLL )
        else
          BtrvLibraryHandle := LoadLibrary( DLLNAME );
        If ( BtrvLibraryHandle > HINSTANCE_ERROR ) then
        begin
          @BtrCall   := GetProcAddress( BtrvLibraryHandle, 'BTRCALL' );
          @BtrCallId := GetProcAddress( BtrvLibraryHandle, 'BTRCALLID' );
          @WBtrvInit := GetProcAddress( BtrvLibraryHandle, 'WBTRVINIT' );
          @WBtrvStop := GetProcAddress( BtrvLibraryHandle, 'WBTRVSTOP' );
        end;
      end;
      If ( BtrvLibraryHandle > HINSTANCE_ERROR ) then
        Result := WBtrvInit( InitializationString );
    Finally
      LeaveCriticalSection( FCSect );
    end;
  Finally
    DeleteCriticalSection( FCSect );
  end;
end;


Function BtrvStop : SmallInt;
Var
  FCSect : TRTLCriticalSection;
begin
  Result := ISAM_SUCCESS;
  InitializeCriticalSection( FCSect );
  Try
    EnterCriticalSection( FCSect );
    Try
      If ( BtrvLibraryHandle > HINSTANCE_ERROR ) then
      begin
        Result := WBtrvStop;
        FreeLibrary( BtrvLibraryHandle );
      end;
      BtrvLibraryHandle := HINSTANCE_ERROR;
    Finally
      LeaveCriticalSection( FCSect );
    end;
  Finally
    DeleteCriticalSection( FCSect );
  end;
end;



Function Btrv (   Operation   : SmallInt;
                var PositionBlock,
                    DataBuffer;
                var DataLen     : LongInt;
                var KeyBuffer;
                    KeyNumber, KeySize : ShortInt ) : SmallInt;
Var
  KeyLen : ShortInt;
begin
  KeyLen := ShortInt( $FF );
  Result := BtrCall( Operation,  PositionBlock,  DataBuffer,
              DataLen, KeyBuffer, KeyLen, KeyNumber );
end;


end.

