Solved

communication beetween dll's and service application, interface

Posted on 2006-10-26
5
558 Views
Last Modified: 2010-08-05
hi experts,
i'm not searching for an source :) just some ideas how to do it different way
the idea is.......
following structure:
        1. service application that cann't be stoped.
        2. the service has an tray icon with an dynamicly loaded menu.
        3. the menuitems cames from dlls that the service is loaded into (service dosn't know the dlls at designtime)
        4. an loaded dll will register all aviable function for export and give the service application acces to.
       
       so any dll have also an list of exportet functions and the structure of variables he needs to execute for the correct running of the function.
       all what the service knows is that the dll must have 2 standart function (exportet)
       1. function init ( var varname: tStringlist ) : Boolean
       2. function execute ( varname: Tstringlist ) : Boolean

      i will explain the communication in the example

example:

     - service is loading the 'main.dll' , and calling the init function
     - the initfunction results his structure in an Tinifile format back to the service with functions like:

        [Internals]
        name=main.dll
        exportetfunctioncount=2
        .....
        [Exportet]
        function1= donothingloop
        function2= dosomthingloop
        [menu]
        item1=donothingloop_item
        item2=dosomthingloop_item
 
     now the service know he cann call 2 functions inside the dll donothingloop and dosomthingloop and service also know he need to add
     2 menu items into systrey menu.

     if the user press now for example "dosomthingloop_item" the service will call the dll function "execute" with the name of the procedure
     the dll have to run.

     there is also a list in the service application with all of the loaded function so other dll's cann also access on

the question now to all of you
     
     do anyone have an idea how to do it in a different way.... maybe there are some components that do such interface options i cann use.
     

i hope all of you understand what i mean, sorry for my english.
0
Comment
Question by:dMa
  • 3
5 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 17811427
well, you can use dynamic loading and standard ini component for reading that config file.
then you will do somethingl ike this: (some kind of pseudo-code follows)

type myfun = function(varname:tstringlist):boolean;
  myfunwrapper=class
    ff:myfun;
  public
    constructor create(f:myfun);
    function callit(varname:tstringlist):boolean;// same prototype as myfun
  end;
constructor myfunwrapper.create;
begin
  ff:=f;
end;

function callit(params):boolean;
begin
  result:=ff(params);
end;

var dll:thandle; i,c:integer; m:TMethod;
begin
  dll:=loadlibrary('main.dll');// replace with ini call to get name value
  c:=inttostr('2');// replace with ini call to get exportetfunctioncount value
  setlength(f,c);
  for i:=1 to c do
  with tmenuitem.create(the menu) do
  begin
    tag:=integer(myfunwrapper.create(getprocaddress(dll,'donothingloop')))// replace with ini call to get function i
    onclick:=myfunwrapper(tag).callit;// and when you free the menuitems, you will iterate through all and call myfunwrapper(tag).free; for each item. this way you don't have to make another variable to keep the references to the wrapper calss ;)
  end;
end;


and that should be all. note that I have written everything directly here and will not compile. but this is the idea that should work right out of the box.
if you don't know how to work with ini files, see this: http://delphi.about.com/od/objectpascalide/l/aa120401a.htm

let me know if you need further guiding or have issues with the idea presented above
0
 
LVL 2

Author Comment

by:dMa
ID: 17811806
thx, that a nice solution how to handle the idea i propose :)
i'm actually searching for an different way how to make the comunication like this.
maybe some kind of plugin manager cann handle that ...do know, maybe there is an easy or more professional way to do.
my idea was to communicate by using tinifiles but actually i'm not so happy with it.

0
 
LVL 28

Expert Comment

by:ciuly
ID: 17811855
well, I don't exactl;y see where is the problem of doing the "plugin" yourself :) you could serach for such a plugin but I never stubled across one yet.

If I have time, I can make the function that gets an ini/xml file as input and returns a popupmenu with all needed stuff. I'm just caught up in things and don't know if I can do it in the next few hours. maybe after about 5-6 hours.
other than this function I don't see what else you might need.
0
 
LVL 3

Expert Comment

by:cobi100
ID: 17812836
if you're writing both the service and the dlls, what is you pass the whole Menu to the dll? then you can manipulate the menu from the dlls at your will. so you'll have one single exported function in the dlls called something like RegisterItems(aMenu: TPopupMenu) and there you do something like this:

unit Unit2;

interface
uses
 Menus, Dialogs;

type
  TDllProcMethods = class
    procedure SomeProc(Sender: TObject);
    procedure SomeOtherProc(Sender: TObject);
  end;

var
  DllProcMethods : TDllProcMethods;

procedure InternalRegisterItems(aMenu: TPopupMenu);

implementation

procedure InternalRegisterItems(aMenu: TPopupMenu);
var
  aMenuItem: TMenuItem;
begin
  aMenuItem:= TMenuItem.Create(aMenu);
  aMenuItem.Caption := 'some Caption';
  aMenuItem.OnClick := DllProcMethods.SomeProc;
  aMenu.Items.Add(aMenuItem);
  //Register more menu items here
end;

procedure TDllProcMethods.SomeProc(Sender: TObject);
begin
  //Do something Here
  ShowMessage('some message');
end;

procedure TDllProcMethods.SomeOtherProc(Sender: TObject);
begin
  //Do something Here
end;

end.

In the case above you create your Dll, make sure ShareMem is included on top of the uses list, and use a similar unit to this one, then on your single exported method call the InternalRegisterItems from the unit.
0
 
LVL 28

Accepted Solution

by:
ciuly earned 250 total points
ID: 17812878
found some time and made a small demo: http://www.ciuly.com/delphi/dll_plugin_xml.zip
all you need to do is pass teh xml string to the loadPlugin function and it will return you a popupmenu with all functionality included.
for testing the app, just compile the dll, compile the test app, run the test app, right click on the form and click one of the 2 items.
you can further extend the xml and functionality from this point
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now