当作为TServiceApplication运行时,EIntfCastError'不支持接口'

时间:2013-04-24 15:25:58

标签: delphi com windows-services

当我将应用程序作为Windoes服务(即TServiceApplication)运行时,我遇到了使用COM对象的问题。引发了EIntfCastError'不支持接口'的异常。

如果我将应用程序作为普通的Delphi应用程序运行,那么它运行正常,包括我使用srvany.exe作为服务运行

type IMyInter = interface (IUnknown)
['{9E6B311E-C6D3-4687-B272-3FBE9DBC2DD6}']
//...
end;

type 
  TMyObject = class
  private
    FMyInter: IMyInter;
  published
    constructor Create(const ClassID: TGUID);
  end;

constructor TMyObject.Create(const ClassID:TGUID);
begin
  CoInitialize(nil);
  FMyInter := CreateComObject(ClassID) as IMyInter;  
  //.... 
end;

在将结果分配给FMyInter时,似乎在调用CreateComObject之后引发了错误。应用程序和COM对象都是32位。我在Windows 7 64bit上运行并使用Delphi XE3。 COM对象已在regsvr32.exe

中注册

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

我终于设法解决了COM服务器端的问题。在创建对象时,即TComObjectFactory.Create,我将线程模型从tmSingle更改为tmApartment。然后我取消注册并重新注册了服务器。普雷斯托!不太清楚为什么,但它对我有用。

...    
initialization
    TComObjectFactory.Create( ComServer, TMyComServerClass, Class_ComServerClassGUID, ‘My Com Server Class’, ‘My Descriptive text’, ciMultiInstance, tmApartment);