Recent

Author Topic: Accessing dll within a class  (Read 1740 times)

DelphiDinosaur

  • New Member
  • *
  • Posts: 15
Accessing dll within a class
« on: May 11, 2023, 12:26:48 pm »
I'm having some trouble accessing a dll from within a class.

Code: Pascal  [Select][+][-]
  1. const
  2.   DLL_NAME = 'Adder.dll';
  3. type
  4. TAdd = function(A: byte; b: byte): word; cdecl;
  5.  
  6. TMyClass = class
  7. private
  8.   fnAdd: TAdd;
  9.   dllHandle: LongWord;
  10. public
  11.   constructor Create;
  12.   destructor Destroy;
  13.  
  14.   function AddA(A: byte; B: byte): word;
  15.   function AddB(A: byte; B: byte): word;
  16. end;
  17.  
  18. constructor TMyClass .Create;
  19. begin
  20.   dllHandle := LoadLibrary(pchar(DLL_NAME));
  21.   if (dllHandle <> 0) then
  22.     Pointer(fnAdd) := GetProcAddress(dllHandle, 'Add');
  23. end;
  24.  
  25.  
  26. destructor TMyClass.Destroy;
  27. begin
  28.    FreeLibrary(dllHandle);
  29. end;
  30.  
  31.  
  32. function TMyClass.AddA(A: byte; B: byte): word;
  33. begin
  34.   Result := fnAdd(A, B);
  35. end;
  36.  
  37. function TMyClass.AddB(A: byte; B: byte): word;
  38. var
  39.   tmpHandle: LongWord;
  40.   tmpAdd: TAdd;
  41. begin
  42.  
  43.   tmpHandle:= LoadLibrary(pchar(DLL_NAME));
  44.   if (tmpHandle<> 0) then
  45.   begin
  46.     Pointer(tmpAdd) := GetProcAddress(tmpHandle, 'Add');
  47.     if Assigned(tmpAdd) then
  48.       Result := tmpAdd(A, B)
  49.   else
  50.  
  51.   end;
  52. end;
  53.  

balazsszekely

  • Guest
Re: Accessing dll within a class
« Reply #1 on: May 11, 2023, 01:21:39 pm »
@DelphiDinosaur
Quote
I'm having some trouble accessing a dll from within a class.
What trouble exactly?  Which part of your code fails? Try this:
Code: Pascal  [Select][+][-]
  1. uses dynlibs, LCLType;
  2.  
  3. var
  4.   DllHandle: TLibHandle;
  5.   FarProc: TFarProc;
  6. begin
  7.   DllHandle := LoadLibrary('Adder.dll');
  8.   if DllHandle <> 0 then
  9.   begin
  10.     FarProc := GetProcedureAddress(DllHandle, 'Add');
  11.     if FarProc <> nil then
  12.     begin
  13.       //...
  14.     end
  15.     else
  16.       ShowMessage(SysErrorMessage(GetLastOSError))
  17.   end
  18.   else
  19.     ShowMessage(SysErrorMessage(GetLastOSError));
  20. end;

Also on windows the calling convention is usually stdcall not cdecl.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: Accessing dll within a class
« Reply #2 on: May 11, 2023, 01:29:58 pm »
Also on windows the calling convention is usually stdcall not cdecl.
Not neccessarily if he's written the dll himself
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

systemdesigner

  • Newbie
  • Posts: 6
  • First Design, Last Make
Re: Accessing dll within a class
« Reply #3 on: June 11, 2023, 05:03:24 pm »
First of all, hello everyone.
I need your help about a problem in my project; debugging a dll unit, is broken.
In fact, I have been debugging many times before nowamdays. I was amazed to see how well
it was working. But it was broken later on. I always upgrade IDE and now it's the latest.
I think, it is caused by changing the IDE or changing project preferences, but no idea...
I removed the latest Lazarus IDE and replaced with its old copies to catch the problem.
I compiled and tested my old versions with old IDE versions and different preferences.
Although I tried all combinations, I could not find the reason. Unfortunately, no way.
The dll project and main project were always compiled with the same Lazarus IDE copy.
The main calls: "DynLibs.LoadLibrary(), DynLibs.GetProcedureAddress(), DynLibs.TLibHandle".
Then I put many breakpoints onto the "ALibName" unit codes. Just after the LoadLibrary(),
main code calls a method from the dll by an ALibHandle code line. However, it doesn't stop
on any breakpoint anymore. Please inform me for the solution. Thanks.
Now Lazarus IDE is 2.2.6 Win32 on Windows10 64bit.
« Last Edit: June 11, 2023, 05:08:53 pm by systemdesigner »
"Lazarus w/ FPC: Write Once, Compile Anywhere"

cdbc

  • Hero Member
  • *****
  • Posts: 1079
    • http://www.cdbc.dk
Re: Accessing dll within a class
« Reply #4 on: June 11, 2023, 05:46:20 pm »
Hi
@systemdesigner: We need to see some code...
@DelphiDinosaur: What kind trouble are you having? Have you tried GetMem's
 tip, what did it say?
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

systemdesigner

  • Newbie
  • Posts: 6
  • First Design, Last Make
Re: Accessing dll within a class
« Reply #5 on: June 13, 2023, 06:58:24 pm »
Hi CDBC
Does debugging into or putting breakpoint in dll, matter the pascal code!?
Does not it matter with lazarus version, compiling options or project preferences?
I used LoadLibrary etc. as I mentioned...
"Lazarus w/ FPC: Write Once, Compile Anywhere"

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Accessing dll within a class
« Reply #6 on: June 13, 2023, 09:07:12 pm »
No. If you want to debug a dll, load the dll sources and tell the ide which program is the host. Simple.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

systemdesigner

  • Newbie
  • Posts: 6
  • First Design, Last Make
Re: Accessing dll within a class
« Reply #7 on: June 14, 2023, 09:38:39 pm »
Hello @Thaddy.
I have a main prj that loads the lib. Library also have own project, having the "library testlib;" unit.
In the lib prj, Run->Run Parameters->Host Application, I typed the main prj file (...\prj1.exe).
Anyway, it doesn't stop on breakpoint on the lib unit.
« Last Edit: June 16, 2023, 09:22:53 pm by systemdesigner »
"Lazarus w/ FPC: Write Once, Compile Anywhere"

systemdesigner

  • Newbie
  • Posts: 6
  • First Design, Last Make
Re: Accessing dll within a class
« Reply #8 on: June 20, 2023, 03:03:39 pm »
No. If you want to debug a dll, load the dll sources and tell the ide which program is the host. Simple.

Could you please describe to me here...?
"Lazarus w/ FPC: Write Once, Compile Anywhere"

 

TinyPortal © 2005-2018