Solved

COM server instancing

Posted on 1998-07-28
6
206 Views
Last Modified: 2010-05-18
Consider an application X that uses interfaces iA and iB.
iA and iB are implemented by txA and txB, which are tAutoObject descendants.
Suppose iB stores some configuration information, and iA implements some functionality that needs the config info from iB.

A problem arises when iA's client (X) wants to touch the config-info aswell, since both X and iA need *the same* reference to iB. (I think!)

In the unit that implements txA, i have the following line:
  tAutoObjectFactory.Create(ComServer, txA, Class_A, ciSingleInstance);

However, after digging into the VCL sourcecode, it seems that the ciSingleInstance parameter is never used again. It is only stored in the classfactory as a private variable.

Given the above scenario, i do NOT get the same references to iB in iA and X.

I've thought of creating a dll-global variable that gets set in txB.initialize if it is nil.. But how do I tell the second instance of iB to abort it's own initialization and use the dll-global variable instead?

And even then, it does not solve my original problem (X and iA using iB) when X and iA are in different address spaces. That is not a practical situation yet, but I can see it becoming one :(

Got any help?
0
Comment
Question by:lowlevel
  • 3
  • 2
6 Comments
 

Expert Comment

by:_art_
Comment Utility
Have you tried ciMultiInstance???? That is it I believe, at least thats what help says.
0
 
LVL 1

Author Comment

by:lowlevel
Comment Utility
Nope, it ain't.
0
 
LVL 1

Accepted Solution

by:
AndersWP earned 250 total points
Comment Utility
I think I have a solution to your problem: To get a handle to an already running instance of an automation server, use the GetActiveObject function.

The following code snippet should give you an example:

Var
  Server: Variant;

Server:=Nil;

Try
  Server:=GetActiveOLEObject(<Your server name>);
Except
End;

Try
  If VarType(Server)<>varDispatch Then
    Server:=CreateOLEObject(<Your Server Name>);
Except
  ShowMessage('Could not create server');
End;

In this example, <Your Server Name> should be replaced with the name of your automation server. After executing this code, the variant variable Server will contain a reference to the server.
The code will first try to establish a reference to an existing instance of the server. If this fails (because no instance is running), a new instance is created.

I hope this will help you.

Greetings,
AndersWP

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:lowlevel
Comment Utility
Works like a dream :)

Would you also happen to know why i cannot do something like
var dispatch : variant;
    een      : iEen;
begin
  try
    dispatch:=getActiveOLEObject (serverName);
  except
  end;

  if varType(dispatch)<>varDispatch
    then een:=coEen.create
    else een:=iEen(getActiveOLEObject (serverName));
end;

this will compile, it'll even run, but the iEen pointer is invalid and thus causes access violations.
I'd like to be able to convert it to a specific interface type because I like the type-checking that delphi does pre-compiling.
0
 
LVL 1

Expert Comment

by:AndersWP
Comment Utility
You cannot typecast a Variant referencing an automation server directly to an interface type.

To work with interfaces rather than variants, you can do something like the following:

  Try
    een:=GetActiveOleObject(<Your Server Name>) As IEen;
  Except
    On e:EOleError Do
      een:=CoEen.Create;
  End;

The trick here is that the As operator will do the appropriate conversion to your interface type.

I hope this is what you ment.

Regards,
AndersWP
0
 
LVL 1

Author Comment

by:lowlevel
Comment Utility
yup, thanx :)

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

11 Experts available now in Live!

Get 1:1 Help Now