[英]Delphi - register a com server in the ROT
我创建了一个非常小的自动化对象(使用delphi 7)。 它完全可以工作,但是我在将它注册到运行对象表中时遇到了问题,因此我可以使用getActiveOleObject函数来检索服务器的运行实例。 问题在于Initialize和Destroy事件不会触发。
编辑:我刚刚注意到,当我通过客户端应用程序中的createOleObject创建应用程序时,会触发初始化 。
EDIT2:在此处下载示例项目
这是源代码:
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.
我以为com服务器在启动时会自动初始化。 但事实并非如此。 所以我在comServer中创建了一个全局变量
GlobalCOMInstance : Tmyserver;
在onShow事件的servcer应用程序中,我只是创建了com对象的实例:
if not assigned(GlobalCOMInstance) then
mycomserver.Tmyserver.Create;
就这样 ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.