unit LiensDLL;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Dynlibs, Menus, Utils;
type
TListSousMenu = array of TMenuItem;
TAddMenu = function(var NbFct: Integer): PChar; stdcall;
TEnvoiDirLib = function(Dir: PChar): Boolean; stdcall;
TArticleEnBdD = function(var Ref: PChar; var IndA, Qte: Integer; var PU: Double): PChar; stdcall;
TMaJStock = function(Ref, Qte, QtePrec: Integer): Integer; stdcall;
var
HndLib: THandle;
LibOK: Boolean; {Indique si la librairie est utilisée et valide}
ListSousMenu: TListSousMenu;
ArticleEnBdD: TArticleEnBdD;
MaJStock: TMajStock;
function InstalleLib(FchLib: String): Boolean;
procedure ClickMenuLib(Sender: TObject);
function LibereLib: Boolean;
implementation
uses
FntBase;
var
AddMenu: TAddMenu;
EnvoiDirLib: TEnvoiDirLib;
function InstalleLib(FchLib: String): Boolean;
var
Fch: String;
Menu, SousMenu: TMenuItem;
i, N, NbM, NoM, PosM: Integer;
begin
Fch:= DirEXE;
Fch:= IncludeTrailingPathDelimiter(Fch+FchLib);
{$IFDEF WINDOWS}
FchLib:= Fch+FchLib+'.dll';
{$ENDIF}
{$IFDEF LINUX}
FchLib:= Fch+'lib'+LowerCase(FchLib)+'.so';
{$ENDIF}
{$IFDEF DARWIN}
FchLib:= Fch+'lib'+LowerCase(FchLib)+'.dylib';
{$ENDIF}
HndLib:= LoadLibrary(PChar(FchLib));
if HndLib <> 0 then
begin
Pointer(AddMenu):= GetProcAddress(HndLib, 'AddMenu');
Pointer(EnvoiDirLib):= GetProcAddress(HndLib, 'EnvoiDirLib');
Pointer(ArticleEnBdD):= GetProcAddress(HndLib, 'ArticleEnBdD');
Pointer(MajStock):= GetProcAddress(HndLib, 'MajStock');
if (@AddMenu <> nil)
and (@EnvoiDirLib <> nil)
and (@ArticleEnBdD <> nil)
and (@MajStock <> nil) then
begin
EnvoiDirLib(PChar(Fch)); {On envoie le Dir de la Lib}
Menu:= TMenuItem.Create(Base.MenuPrinc);
PosM:= 1; {Position d'insertion des menus de la Lib}
N:= 0;{AddMenu(0) retourne le nom du menu à ajouter et le Nb de Sousmenu}
Menu.Caption:= string(AddMenu(N));
SetLength(ListSousMenu, Length(ListSousMenu)+N);
NbM:= Base.MenuPrinc.Items.Count; {Nombre de menus prédéfinis}
Base.MenuPrinc.Items.Insert(PosM, Menu); {Insertion du menu}
for i:= 1 to N do {Création des N sous-menus}
begin
SousMenu:= TMenuItem.Create(Base.MenuPrinc);
ListSousMenu[High(ListSousMenu)-N+i]:= SousMenu;
NoM:= i;
SousMenu.ImageIndex:= i+11; {11 = dernier indice des des menus fixes}
SousMenu.Caption:= string(AddMenu(NoM));
SousMenu.Enabled:= True;
SousMenu.Tag:= i; {Mémo de l'indice du sous-menu dans son Tag}
SousMenu.OnClick:= @Base.ClickMenu; {Affectation de la fonction Click}
Base.MenuPrinc.Items[PosM].Add(SousMenu); {Installe le sous-menu}
end;
LibOK:= True;
Result:= True;
end
else
LibOK:= False;
end
else
LibOk:= False;
Result:= LibOK;
end;
procedure ClickMenuLib(Sender: TObject);
var
// Str: String;
Pch: PChar;
i: Integer;
d: Double;
begin
case TMenuItem(Sender).Tag of
1: begin {Connexion pour consultation, modification de la BdD}
i:= 0;
ArticleEnBdD(Pch, i, i, d);
end;
{ 2: Str:= '';
3: Str:= '';}
end;
end;
function LibereLib: Boolean;
begin
FreeLibrary(HndLib);
end;
end.