EIntfCastError 'Interface not supported' w

2019-06-09 21:56发布

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条回答
我只想做你的唯一
2楼-- · 2019-06-09 22:48

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);
查看更多
登录 后发表回答