unit KEEPASSAPI_dynamic;
{**************************************************************************}
{                                                                          }
{    This C DLL header file first (automatic) conversion generated by:     }
{    HeadConv 4.0 (c) 2000 by Bob Swart (aka Dr.Bob - www.drbob42.com)     }
{      Final Delphi-Jedi (Darth) command-line units edition                }
{                                                                          }
{    Generated Date:   7-1-2006                                            }
{    Generated Time: 10:43:16                                              }
{                                                                          }
{**************************************************************************}

interface
uses
  Sysutils,
  Dialogs,
{$IFDEF WIN32}
  Windows;
{$ELSE}
  Wintypes, WinProcs;
{$ENDIF}


{=> c:\delphi\keepass\keepasslib-1.04-060105\keepasslib\KEEPASSAPI.H <=}

{+// }
{-KeePass Password Safe - The Open-Source Password Manager }
{-Copyright (C) 2003-2006 Dominik Reichl <dominik.reichl@t-online.de> }

{-This program is free software; you can redistribute it and/or modify }
{-it under the terms of the GNU General Public License as published by }
{-the Free Software Foundation; either version 2 of the License, or }
{-(at your option) any later version. }

{-This program is distributed in the hope that it will be useful, }
{-but WITHOUT ANY WARRANTY; without even the implied warranty of }
{-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{-GNU General Public License for more details. }

{-You should have received a copy of the GNU General Public License }
{-along with this program; if not, write to the Free Software }
{-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA }
{= }

{$IFNDEF ___KEEPASS_API_H___}
{$DEFINE ___KEEPASS_API_H___}

var
  GetKeePassVersionString:  function: PChar cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
//  InitManager: procedure(var pMgr: Pointer;
  InitManager: procedure(var pMgr: Pointer;
                         bIsFirstInstance: Bool) cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
  DeleteManager: procedure(pMgr: Pointer) cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
  SetMasterKey: function(pMgr: Pointer;
                         const pszMasterKey: PChar;
                         bDiskDrive: Bool;
                         const pszSecondKey: PChar;
                         const pARI: Integer; //PCNEWRANDOMINTERFACE;
                         bOverwrite: Bool): Integer cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
  GetNumberOfEntries: function(pMgr: Pointer): LongInt cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
  NewDatabase: procedure(pMgr: Pointer) cdecl  {$IFDEF WIN32} stdcall {$ENDIF};
var
  SaveDatabase: function(pMgr: Pointer;
                         const pszFile: PChar): Integer cdecl  {$IFDEF WIN32} stdcall {$ENDIF};
var
  OpenDatabase: function(pMgr: Pointer;
                         const pszFile: PChar;
                         pRepair: Pointer {PWDB_REPAIR_INFO}): Integer cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

{/// Access entry information }
var
  GetEntry: function(pMgr: Pointer; 
                     dwIndex: Cardinal): Pointer cdecl  {$IFDEF WIN32} stdcall {$ENDIF};
var
  GetLastEditedEntry: function(pMgr: Pointer): Pointer cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

var
  UnlockEntryPassword: procedure(pMgr: Pointer; 
                                 pEntry: Pointer) cdecl; 
// Struct info

var
  PE_GetTitle: function(pEntry: Pointer): PChar cdecl  {$IFDEF WIN32} stdcall {$ENDIF};
var
  PE_GetUserName: function(pEntry: Pointer): PChar cdecl;
var
  PE_GetPassword: function(pEntry: Pointer): PChar cdecl  {$IFDEF WIN32} stdcall {$ENDIF};
var
  PE_GetNotes: function(pEntry: Pointer): PChar cdecl  {$IFDEF WIN32} stdcall {$ENDIF};

{$ENDIF}

var
  DLLLoaded: Boolean { is DLL (dynamically) loaded already? }
    {$IFDEF WIN32} = False; {$ENDIF}

implementation

var
  SaveExit: pointer;
  DLLHandle: THandle;
{$IFNDEF MSDOS}
  ErrorMode: Integer;
{$ENDIF}
  LastError : LongInt;

  procedure NewExit; far;
  begin
    ExitProc := SaveExit;
    FreeLibrary(DLLHandle)
  end {NewExit};

procedure LoadDLL;
begin
  if DLLLoaded then Exit;
{$IFNDEF MSDOS}
  ErrorMode := SetErrorMode($8000{SEM_NoOpenFileErrorBox});
{$ENDIF}
  DLLHandle := LoadLibrary('KEEPASSlib.DLL');
  if DLLHandle >= 32 then
  begin
    DLLLoaded := True;
    SaveExit := ExitProc;
    ExitProc := @NewExit;
    @GetKeePassVersionString := GetProcAddress(DLLHandle,'GetKeePassVersionString');
  {$IFDEF WIN32}
    Assert(@GetKeePassVersionString <> nil);
  {$ENDIF}
    @InitManager := GetProcAddress(DLLHandle,'InitManager');
  {$IFDEF WIN32}
    Assert(@InitManager <> nil);
  {$ENDIF}
    @DeleteManager := GetProcAddress(DLLHandle,'DeleteManager');
  {$IFDEF WIN32}
    Assert(@DeleteManager <> nil);
  {$ENDIF}
    @SetMasterKey := GetProcAddress(DLLHandle,'SetMasterKey');
  {$IFDEF WIN32}
    Assert(@SetMasterKey <> nil);
  {$ENDIF}
    @GetNumberOfEntries := GetProcAddress(DLLHandle,'GetNumberOfEntries');
  {$IFDEF WIN32}
    Assert(@GetNumberOfEntries <> nil);
  {$ENDIF}
    @NewDatabase := GetProcAddress(DLLHandle,'NewDatabase');
  {$IFDEF WIN32}
    Assert(@NewDatabase <> nil);
  {$ENDIF}
    @OpenDatabase := GetProcAddress(DLLHandle,'OpenDatabase');
  {$IFDEF WIN32}
    Assert(@OpenDatabase <> nil);
  {$ENDIF}
    @SaveDatabase := GetProcAddress(DLLHandle,'SaveDatabase');
  {$IFDEF WIN32}
    Assert(@SaveDatabase <> nil);
  {$ENDIF}

    @GetEntry := GetProcAddress(DLLHandle,'GetEntry');
  {$IFDEF WIN32}
    Assert(@GetEntry <> nil);
  {$ENDIF}

    @GetLastEditedEntry := GetProcAddress(DLLHandle,'GetLastEditedEntry');
  {$IFDEF WIN32}
    Assert(@GetLastEditedEntry <> nil);
  {$ENDIF}

    @UnlockEntryPassword := GetProcAddress(DLLHandle,'UnlockEntryPassword');
  {$IFDEF WIN32}
    Assert(@UnlockEntryPassword <> nil);
  {$ENDIF}

    @PE_GetTitle := GetProcAddress(DLLHandle,'PE_GetTitle');
  {$IFDEF WIN32}
    Assert(@PE_GetTitle <> nil);
  {$ENDIF}
    @PE_GetUserName := GetProcAddress(DLLHandle,'PE_GetUserName');
  {$IFDEF WIN32}
    Assert(@PE_GetUserName <> nil);
  {$ENDIF}
    @PE_GetPassword := GetProcAddress(DLLHandle,'PE_GetPasswordPtr');
  {$IFDEF WIN32}
    Assert(@PE_GetPassword <> nil);
  {$ENDIF}
    @PE_GetNotes := GetProcAddress(DLLHandle,'PE_GetNotes');
  {$IFDEF WIN32}
    Assert(@PE_GetNotes <> nil);
  {$ENDIF}
  end
  else
  begin
    LastError := GetLastError;

    ShowMessage(SysErrorMessage(LastError));
    DLLLoaded := False;
    { Error: KEEPASSAPI.DLL could not be loaded !! }
  end;
{$IFNDEF MSDOS}
  SetErrorMode(ErrorMode)
{$ENDIF}
end {LoadDLL};

begin
  LoadDLL;
end.


