Forum > Packages and Libraries
Accessing dll within a class
DelphiDinosaur:
I'm having some trouble accessing a dll from within a class.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const DLL_NAME = 'Adder.dll';typeTAdd = function(A: byte; b: byte): word; cdecl; TMyClass = classprivate fnAdd: TAdd; dllHandle: LongWord;public constructor Create; destructor Destroy; function AddA(A: byte; B: byte): word; function AddB(A: byte; B: byte): word;end; constructor TMyClass .Create;begin dllHandle := LoadLibrary(pchar(DLL_NAME)); if (dllHandle <> 0) then Pointer(fnAdd) := GetProcAddress(dllHandle, 'Add');end; destructor TMyClass.Destroy;begin FreeLibrary(dllHandle);end; function TMyClass.AddA(A: byte; B: byte): word;begin Result := fnAdd(A, B);end; function TMyClass.AddB(A: byte; B: byte): word;var tmpHandle: LongWord; tmpAdd: TAdd;begin tmpHandle:= LoadLibrary(pchar(DLL_NAME)); if (tmpHandle<> 0) then begin Pointer(tmpAdd) := GetProcAddress(tmpHandle, 'Add'); if Assigned(tmpAdd) then Result := tmpAdd(A, B) else end;end;
GetMem:
@DelphiDinosaur
--- Quote ---I'm having some trouble accessing a dll from within a class.
--- End quote ---
What trouble exactly? Which part of your code fails? Try this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses dynlibs, LCLType; var DllHandle: TLibHandle; FarProc: TFarProc;begin DllHandle := LoadLibrary('Adder.dll'); if DllHandle <> 0 then begin FarProc := GetProcedureAddress(DllHandle, 'Add'); if FarProc <> nil then begin //... end else ShowMessage(SysErrorMessage(GetLastOSError)) end else ShowMessage(SysErrorMessage(GetLastOSError));end;
Also on windows the calling convention is usually stdcall not cdecl.
Zvoni:
--- Quote from: GetMem on May 11, 2023, 01:21:39 pm ---Also on windows the calling convention is usually stdcall not cdecl.
--- End quote ---
Not neccessarily if he's written the dll himself
systemdesigner:
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.
cdbc:
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
Navigation
[0] Message Index
[#] Next page