Recent

Author Topic: How do I link a GUI application to a library and DLL?  (Read 3205 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
How do I link a GUI application to a library and DLL?
« on: April 26, 2018, 01:11:52 pm »
Hi

I'm struggling to find the answer because I'm not sure what terminologies to use! I did find this and have read it (https://www.freepascal.org/docs-html/prog/progse55.html)

Basically :

There is a software tool that makes it's own API accessible via a pas file (it supports C, Python, Delphi and Pascal languages) and they have several Delphi demo projects.

I have taken one of the demo projects and converted it to a Lazarus library using Lazarus. The result is library project and there are several functions in that. Lets assume it is called MyLibrary. At the end of the library file is an exports section where each function is exported by name. When compiling in Lazarus, a DLL is produced. When I use the internal mechanisms of the software to call this DLL plugin called MyLibrary.dll, it works fine.

It looks a bit like this :
Code: Pascal  [Select][+][-]
  1. library MyLibrary
  2. {$MODE Delphi}
  3.  
  4. uses TheAPI, LCLIntf, LCLType, Windows, strings, sysutils;
  5.  
  6. function Example1(Parameters....) : LongInt :stdcall; export;
  7. begin
  8.   // does stuff, and calls things from TheAPI.pas
  9. end;
  10.  
  11. function Example2(Parameters....) : LongInt :stdcall; export;
  12. begin
  13.   // does stuff, and calls things from TheAPI.pas
  14. end;
  15.  
  16. function Example3(Parameters....) : LongInt :stdcall; export;
  17. begin
  18.   // does stuff, and calls things from TheAPI.pas
  19. end;
  20.  
  21. function Example4(Parameters....) : LongInt :stdcall; export;
  22. begin
  23.   // does stuff, and calls things from TheAPI.pas
  24. end;
  25.  
  26. exports
  27.   Example1,
  28.   Example2,
  29.   Example3,
  30.   Example4;
  31.  
  32. end.
  33.  

Compiled result is MyLibrary.dll

What I want to do next is build an interface so that the user can pass some preferred parameters to the functions such as Example1 and Example2 in the DLL instead of it just doing things that I have hard coded into it. I know this can be done with the API because other plugins for this tool have their own interfaces which appear when the calling program executes the DLL so that the user can pass options to it.

However, I am totally unclear how I create a new application with a new form and some buttons that can then call the functions of this API based DLL or MyLibrary file. How do I link form1 and unit1.pas to the DLL, or better still, the MyLibrary pas file so that all the functions in my library file (and ergo the DLL) are accessible from the form? I have tried just adding the name of it (MyLibrary) to the uses of unit.pas but that is not working.

I have done something similar in the past (calling a DLL from my program) but in that case I did not have the source code to the DLL...only the name of the functions that I could call from it. So I used

Code: Pascal  [Select][+][-]
  1. if FileExists(SomeDLL.dll) then
  2. begin
  3. @SomeValue1 := GetProcAddress(THandle, 'Function1');
  4. @SomeValue2 := GetProcAddress(THandle, 'Function2');
  5. @SomeValue3 := GetProcAddress(THandle, 'Function3');
  6. end;
  7.  

But in my case, I do have the functions, in MyLibrary file. So I don't think I need to link to the DLL directly. I just want my application and unit to be able to utilise the functions in MyLibrary and have it all wrapped up together at compile time as a single DLL plugin that can be called from the parent program that they are designed to work with
« Last Edit: April 26, 2018, 01:53:19 pm by Gizmo »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How do I link a GUI application to a library and DLL?
« Reply #1 on: April 26, 2018, 02:34:44 pm »
What I want to do next is build an interface so that the user can pass some preferred parameters to the functions such as Example1 and Example2 in the DLL instead of it just doing things that I have hard coded into it. I know this can be done with the API because other plugins for this tool have their own interfaces which appear when the calling program executes the DLL so that the user can pass options to it.

Sounds like you should put the form in your DLL. I don't know how well this works with LCL - it's a common thing that people used to do with Delphi, but there were problems with LCL in a dynamic library. Again, I don't know what the status is - normally, I wouldn't put any GUI in a dynamic library.

Host apps these days tend to use Python or something for implementing plugins, so the plugin UI and code both would be in a script. Lots of advantages over compiled libraries, as you can see.



Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How do I link a GUI application to a library and DLL?
« Reply #2 on: April 26, 2018, 03:41:43 pm »
Quote
Sounds like you should put the form in your DLL

That's what I am trying to do. But how do you do that? If I add a form to my library project the elements of the form cannot access the functions of the library, even after I add the library name in the uses.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How do I link a GUI application to a library and DLL?
« Reply #3 on: April 26, 2018, 03:44:23 pm »
Quote
Sounds like you should put the form in your DLL

That's what I am trying to do. But how do you do that? If I add a form to my library project the elements of the form cannot access the functions of the library, even after I add the library name in the uses.

Separate out the code you need to call into a separate unit, or even into the form unit if it's trivial code. Don't confuse the library declaration stuff, which is called by external software like the host app, and the internal implementation of the exported functions.

balazsszekely

  • Guest
Re: How do I link a GUI application to a library and DLL?
« Reply #4 on: April 26, 2018, 03:51:56 pm »
Hi Gizmo,

Unfortunately I did not understand entirely what are you trying to achieve(English is not my first language), however I attach a project where forms are used in a library. The library(plugin) is loaded, then the form is displayed by the "loader" application. It may or may not help you, I don't know. Please test attachment. After you build the projects, don't forget to copy the library in the same directory as your exe.
« Last Edit: April 26, 2018, 03:54:37 pm by GetMem »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How do I link a GUI application to a library and DLL?
« Reply #5 on: April 27, 2018, 04:06:11 pm »
Thanks GetMem for that example. I hope you didn't do all that from scratch just for my benefit?!!!

I've had a quick look - I still don't quite get it but it is helpful. With more time I might crack it. I think one of my problems is the DLL is being compiled in DELPHI mode.

balazsszekely

  • Guest
Re: How do I link a GUI application to a library and DLL?
« Reply #6 on: April 27, 2018, 05:55:06 pm »
Quote
I hope you didn't do all that from scratch just for my benefit?!!!
I created a plugin system a long time ago. I reused the code to create the demo.

Quote
I've had a quick look - I still don't quite get it but it is helpful. With more time I might crack it. I think one of my problems is the DLL is being compiled in DELPHI mode.
Feel free to ask more questions if you like. Maybe somebody knows the answer.

PS: Another idea...there is a package "NotepadppPlugin". As the name suggest, you can create plugins for Notepad++(demo project included). You can give it a try.

 

TinyPortal © 2005-2018