Author: Bertrand Goetzmann
How getting the IUnknown reference on a specific COM object's instance created by
an application ?
Answer:
The RegisterActiveObject function -from the Win32 API- can register an object by
passing its IUnknown reference and its CLSID to make it the active object for its
CLSID.
Registration causes the object to be listed in OLE's running object table, a
globally accessible lookup table that keeps track of the objects that are currently
running on your computer.
An application can then create an OLE automation object for example, register it as
the active object at startup.
Other application can have access to this particular instance by getting a
IDispatch reference with the Delphi's GetActiveOleObject using its progID.
I've placed the registration mecanism in the TActiveObject class showed bellow and
you can download the demo applications.
1 unit ActiveObject;
2
3 // Written by Bertrand Goetzmann (http://www.object-everywhere.com)
4 // Keywords : RegisterActiveObject, CoLockObjectExternal, RevokeActiveObject,
5 CoDisconnectObject, GetActiveOleObject, GetActiveObject
6
7 interface
8
9 type
10 TActiveObject = class
11 private
12 FUnk: IInterface;
13 FRegister: Integer;
14 public
15 constructor Create(Unk: IInterface; const clsid: TGUID); overload;
16 constructor Create(Unk: IInterface; const ProgId: string); overload;
17 destructor Destroy; override;
18 end;
19
20 implementation
21
22 uses ActiveX, ComObj;
23
24 { TActiveObject }
25
26 constructor TActiveObject.Create(Unk: IInterface; const clsid: TGUID);
27 begin
28 inherited Create;
29 FUnk := Unk;
30 OleCheck(RegisterActiveObject(FUnk, clsid, ACTIVEOBJECT_WEAK, FRegister));
31 OleCheck(CoLockObjectExternal(FUnk, True, True));
32 end;
33
34 constructor TActiveObject.Create(Unk: IInterface; const ProgId: string);
35 begin
36 Create(Unk, ProgIDToClassID(ProgId));
37 end;
38
39 destructor TActiveObject.Destroy;
40 begin
41 OleCheck(CoLockObjectExternal(FUnk, False, True));
42 OleCheck(RevokeActiveObject(FRegister, nil));
43 OleCheck(CoDisconnectObject(FUnk, 0));
44 inherited;
45 end;
46
47 end.
In the demo applications, OleObject.dll is the implementation of the OLE automation
object with "OleObject.Test" as progId and supporting the ITest interface. This
interface has a single property named Message : you can read or write a simple
string of characters.
The AppTest.exe creates an instance of this OLE automation object and register it
with an instance of TActiveObject. When the applicatino shut down, the registration
of the active objet is revoked.
Start several instances of ClientTest. ClientTest gets the IDispatch reference, via
a Variant variable, on the active object by using a call of
GetActiveOleObject('OleObject.Test'), to set or get the Message property value.
I think it is a powerful way to make applications more collaborative.
Component Download: http://perso.worldonline.fr/objecteverywhere/ActiveObject.ziphttp://perso.worldonline.fr/objecteverywhere/ActiveObject.zip
|