Jarosław Wiechecki
Jarosław Wiechecki

Reputation: 39

Service applications and com plugins

I have written a set of plugins which work fine in normal application. But when I try to use them in service application my service stops when it reach CreateComObject function. Can I use COM plugins in service application? This is code:

procedure TWCMService.CreateControllerList;
var
  List: TAutoFreeList<TController>>;
  i: integer;
  Plugin: IPluginInterfaces;
begin
  try
    List := TAutoFreeList<TController>.Create;
    DatabaseModule.IBDatabase1.Connected := true;
    DatabaseModule.SelectControllers(List);
    DatabaseModule.IBDatabase1.Connected := false;
    Plugin := CreateComObject(StringToGuid('{F2959AEC-644F-49E4-9012-B9B3BF34B43F}')) as  IPluginInterfaces;
    for i := 0 to List.Count - 1 do
      begin
        Plugin.Init(StringToGuid(List[i].PluginId));
        FAvailControllers.Add(CreateComObject(Plugin.GetCommunicationPluginGuid) as ICommunicationPlugin);
        FAvailControllers[i].Init(Self as IServiceApplication);
        FAvailControllers[i].SetMAC(List[i].ControllerMAC);
        MessageBox(0, 'Dodany', 'Uwaga', MB_OK);
      end;
    List.Free;
    FAvailControllersCurrentIndex := 0;
    Timer1.Enabled := true;
  except
    raise Exception.Create('WCM Serwis: Error Message');
  end;
end;

Upvotes: 0

Views: 567

Answers (5)

Lars
Lars

Reputation: 1438

Could your problem be similar to the one I experienced?
EIntfCastError 'Interface not supported' when run as a TServiceApplication
When creating the object, i.e. TComObjectFactory.Create I changed the threading model from tmSingle to tmApartment. Then I unregistered and re-registered the server. Presto! Not quite sure why but it works for me.

Upvotes: 0

Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10372

It could be something security related. Depends on your operating system. Services may run under another account. The rights of this account might not be sufficient to create the COM object you want. Just guessing.

Upvotes: 0

Wim ten Brink
Wim ten Brink

Reputation: 26682

As has been said, you need to make sure COM is initialized. But that might not be the main reason.
You're using Interbase and to connect to the database, it's probably communicating over the network with the database server. (Even if DB is on the same system!) But a Windows Service doesn't have network access by default so you must add a dependency to the service to the network functionality.
Your problem could be the same as in this question, making this one a duplicate!

Upvotes: 0

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

It could be that you need to initialize the COM library for each thread calling CoInitialize Described here. http://chrisbensen.blogspot.com/2007/06/delphi-tips-and-tricks_20.html

Upvotes: 0

Tim Jarvis
Tim Jarvis

Reputation: 18815

Untested and just off the top of my head...You probably will find that you need to initialize COM (call coinitialize), this is normally done for you with a windows app (in application.Run or higher up the chain) with a service applett you won't have had this done for you, you will need to do it yourself.

Upvotes: 3

Related Questions