EIntfCastError 'Interface not supported' when run as a TServiceApplication

人走茶凉 提交于 2019-12-10 11:53:08

问题


I'm having problems using a COM-object when I run my application as a Windoes Service, i.e. TServiceApplication. The exception EIntfCastError 'Interface not supported' is raised.

If I run the application as a normal Delphi app then it works fine, including if I run as a service using 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;

It seems like the error is raised after the call to CreateComObject when the result is going to be assigned to FMyInter. Both the application and COM-object are 32-bit. I'm running on Windows 7 64bit and using Delphi XE3. The COM-object has been registered with regsvr32.exe

Any help would be appreciated


回答1:


I finally managed to solve the problem which resided on the COM-server side. When creating the object, i.e. TComObjectFactory.Create I changed the threading model from tmSingle to tmApartment. Then I unregistered and re-registered the server. Presto! Not quite sure why but it works for me.

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


来源:https://stackoverflow.com/questions/16195992/eintfcasterror-interface-not-supported-when-run-as-a-tserviceapplication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!