views:

85

answers:

1

Hi guys,

I've created a very small automation object (using delphi 7). It works at all, but I have problem to register it in the running object table so that I can use getActiveOleObject function to retrieve a running instance of the server. The problem is that the Initialize and Destroy events doensn't fire.

EDIT: I've just noted that the initialize is fired when I create the application via createOleObject in an client application.

EDIT2: Download the sample projekt here

Here's the sourcecode:

unit mycomserver;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, server_TLB, StdVcl, dialogs;

type
  Tmyserver = class(TAutoObject, Imyserver)
  private
    FROTCookie: Longint;
  public
    procedure Initialize; override;
    destructor Destroy; override;
  protected
    procedure hello; safecall;

  end;

implementation

uses ComServ;

procedure Tmyserver.Initialize;
begin
  inherited;
  //Register object in ROT
  showmessage('Why the init event doesnt fire?');
  OleCheck(RegisterActiveObject(Self, CLASS_myserver, ActiveObject_Weak, FROTCookie))
end;

destructor Tmyserver.Destroy;
begin
  // unegister object in ROT
  showmessage('And destroy event also doesnt fire...');
  OleCheck(RevokeActiveObject(FROTCookie, nil));
  inherited;
end;

procedure Tmyserver.hello;
begin
  showmessage('hello its me the comserver');
end;

initialization
  showmessage('com server init works...');
  TAutoObjectFactory.Create(ComServer, Tmyserver, Class_myserver,
    ciMultiInstance, tmApartment);
end.
A: 

Hi,

I was assuming that the com server is initialized automatically when the server is started. But that is not the case. So I've created a global var in the comServer, something like

 GlobalCOMInstance : Tmyserver;

In the servcer application in the onShow event, I simply created an instance of the com object:

  if not assigned(GlobalCOMInstance) then
    mycomserver.Tmyserver.Create;

And thats all ;)

ben