Recent

Author Topic: Problems accessing a library under MacOS  (Read 352 times)

ChPr

  • Jr. Member
  • **
  • Posts: 52
Problems accessing a library under MacOS
« on: August 31, 2024, 11:43:44 am »
Hello everyone,

In an application, I use a library (“libarticles.dylib” in the case of MacOS).

Code: Pascal  [Select][+][-]
  1. unit LiensDLL;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Dynlibs, Menus, Utils;
  9.  
  10. type
  11.   TListSousMenu = array of TMenuItem;
  12.   TAddMenu = function(var NbFct: Integer): PChar; stdcall;
  13.   TEnvoiDirLib = function(Dir: PChar): Boolean; stdcall;
  14.   TArticleEnBdD = function(var Ref: PChar; var IndA, Qte: Integer; var PU: Double): PChar; stdcall;
  15.   TMaJStock = function(Ref, Qte, QtePrec: Integer): Integer; stdcall;
  16.  
  17. var
  18.   HndLib: THandle;
  19.   LibOK: Boolean;              {Indique si la librairie est utilisée et valide}
  20.   ListSousMenu: TListSousMenu;
  21.   ArticleEnBdD: TArticleEnBdD;
  22.   MaJStock: TMajStock;
  23.  
  24. function InstalleLib(FchLib: String): Boolean;
  25. procedure ClickMenuLib(Sender: TObject);
  26. function LibereLib: Boolean;
  27.  
  28. implementation
  29.  
  30. uses
  31.   FntBase;
  32.  
  33. var
  34.   AddMenu: TAddMenu;
  35.   EnvoiDirLib: TEnvoiDirLib;
  36.  
  37. function InstalleLib(FchLib: String): Boolean;
  38. var
  39.   Fch: String;
  40.   Menu, SousMenu: TMenuItem;
  41.   i, N, NbM, NoM, PosM: Integer;
  42. begin
  43.   Fch:= DirEXE;
  44.   Fch:= IncludeTrailingPathDelimiter(Fch+FchLib);
  45.   {$IFDEF WINDOWS}
  46.     FchLib:= Fch+FchLib+'.dll';
  47.   {$ENDIF}
  48.   {$IFDEF LINUX}
  49.     FchLib:= Fch+'lib'+LowerCase(FchLib)+'.so';
  50.   {$ENDIF}
  51.   {$IFDEF DARWIN}
  52.     FchLib:= Fch+'lib'+LowerCase(FchLib)+'.dylib';
  53.   {$ENDIF}
  54.   HndLib:= LoadLibrary(PChar(FchLib));
  55.   if HndLib <> 0 then
  56.   begin
  57.     Pointer(AddMenu):= GetProcAddress(HndLib, 'AddMenu');
  58.     Pointer(EnvoiDirLib):= GetProcAddress(HndLib, 'EnvoiDirLib');
  59.     Pointer(ArticleEnBdD):= GetProcAddress(HndLib, 'ArticleEnBdD');
  60.     Pointer(MajStock):= GetProcAddress(HndLib, 'MajStock');
  61.     if (@AddMenu <> nil)
  62.     and (@EnvoiDirLib <> nil)
  63.     and (@ArticleEnBdD <> nil)
  64.     and (@MajStock <> nil) then
  65.     begin
  66.       EnvoiDirLib(PChar(Fch));                     {On envoie le Dir de la Lib}
  67.       Menu:= TMenuItem.Create(Base.MenuPrinc);
  68.       PosM:= 1;                      {Position d'insertion des menus de la Lib}
  69.       N:= 0;{AddMenu(0) retourne le nom du menu à ajouter et le Nb de Sousmenu}
  70.       Menu.Caption:= string(AddMenu(N));
  71.       SetLength(ListSousMenu, Length(ListSousMenu)+N);
  72.       NbM:= Base.MenuPrinc.Items.Count;            {Nombre de menus prédéfinis}
  73.       Base.MenuPrinc.Items.Insert(PosM, Menu);              {Insertion du menu}
  74.       for i:= 1 to N do                             {Création des N sous-menus}
  75.       begin
  76.         SousMenu:= TMenuItem.Create(Base.MenuPrinc);
  77.         ListSousMenu[High(ListSousMenu)-N+i]:= SousMenu;
  78.         NoM:= i;
  79.         SousMenu.ImageIndex:= i+11;   {11 = dernier indice des des menus fixes}
  80.         SousMenu.Caption:= string(AddMenu(NoM));
  81.         SousMenu.Enabled:= True;
  82.         SousMenu.Tag:= i;          {Mémo de l'indice du sous-menu dans son Tag}
  83.         SousMenu.OnClick:= @Base.ClickMenu;  {Affectation de la fonction Click}
  84.         Base.MenuPrinc.Items[PosM].Add(SousMenu);       {Installe le sous-menu}
  85.       end;
  86.       LibOK:= True;
  87.       Result:= True;
  88.     end
  89.     else
  90.       LibOK:= False;
  91.   end
  92.   else
  93.     LibOk:= False;
  94.   Result:= LibOK;
  95. end;
  96.  
  97. procedure ClickMenuLib(Sender: TObject);
  98. var
  99. //  Str: String;
  100.   Pch: PChar;
  101.   i: Integer;
  102.   d: Double;
  103. begin
  104.   case TMenuItem(Sender).Tag of
  105.  1: begin                 {Connexion pour consultation, modification de la BdD}
  106.       i:= 0;
  107.       ArticleEnBdD(Pch, i, i, d);
  108.     end;
  109. { 2: Str:= '';
  110.  3: Str:= '';}
  111.   end;
  112. end;
  113.  
  114. function LibereLib: Boolean;
  115. begin
  116.   FreeLibrary(HndLib);
  117. end;
  118.  
  119. end.

On Windows and Linux, this poses no problem. On Mac OS, it crashes on line 66:
Code: Pascal  [Select][+][-]
  1. EnvoiDirLib(PChar(Fch));
with the following error code:

Code: Text  [Select][+][-]
  1. "Process stopped with reason : EXC_BAD_ACCESS (code=1, address=0x0)".

Any idea of the problem?

Best regards.

Pierre.

cdbc

  • Hero Member
  • *****
  • Posts: 1534
    • http://www.cdbc.dk
Re: Problems accessing a library under MacOS
« Reply #1 on: August 31, 2024, 12:26:07 pm »
Hi
You could try: setlenght(Fch,1024); or something that fits memorywise, I think the crash comes from the library writing to an empty string ~ nil; a pchar can't reallocate like a string does. with setlength you preallocate a /buffer/ in Fch.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ChPr

  • Jr. Member
  • **
  • Posts: 52
Re: Problems accessing a library under MacOS
« Reply #2 on: August 31, 2024, 07:57:06 pm »
Thank you "cdbc" for this solution. However, it doesn't work. In fact, “Fch” is already allocated (not nil) to lines 43 and 44.

Best regards.

Pierre.

cdbc

  • Hero Member
  • *****
  • Posts: 1534
    • http://www.cdbc.dk
Re: Problems accessing a library under MacOS
« Reply #3 on: August 31, 2024, 09:40:12 pm »
Oh, yeah I see that now, sorry my bad...
As far as the error-message goes, it seems something in that call, references a nil pointer?!? Mysterious...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018