我有一个以下的COM对象:
unit Unit4;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, ActiveX, Classes, ComObj, Project1_TLB, StdVcl;
const
IID_IMyTestIntf: TGUID = '{D22502A1-B76C-4E1E-A77C-558B8A6450C1}';
type
IMyTestIntf = interface(IUnknown)
['{D22502A1-B76C-4E1E-A77C-558B8A6450C1}']
end;
TMyTestObj = class(TTypedComObject, IMyTestObj, IMyTestIntf)
private
protected
public
end;
var
UsedIntf: IMyTestObj;
implementation
uses ComServ;
initialization
TTypedComObjectFactory.Create(ComServer, TMyTestObj, Class_MyTestObj,
ciMultiInstance, tmApartment);
end.
这是类型库:
const
// TypeLibrary Major and minor versions
Project1MajorVersion = 1;
Project1MinorVersion = 0;
LIBID_Project1: TGUID = '{70CAFFB4-B90D-42EF-9673-F81DB403B9BF}';
IID_IMyTestObj: TGUID = '{26F41FF7-9C49-45D8-86BC-C38AB302B9E6}';
CLASS_MyTestObj: TGUID = '{6F6E4B1D-100D-4DC5-A9EB-10C90BF422AB}';
type
IMyTestObj = interface;
MyTestObj = IMyTestObj;
IMyTestObj = interface(IUnknown)
['{26F41FF7-9C49-45D8-86BC-C38AB302B9E6}']
end;
CoMyTestObj = class
class function Create: IMyTestObj;
class function CreateRemote(const MachineName: string): IMyTestObj;
end;
implementation
uses ComObj;
class function CoMyTestObj.Create: IMyTestObj;
begin
Result := CreateComObject(CLASS_MyTestObj) as IMyTestObj;
end;
class function CoMyTestObj.CreateRemote(const MachineName: string): IMyTestObj;
begin
Result := CreateRemoteComObject(MachineName, CLASS_MyTestObj) as IMyTestObj;
end;
end.
当我尝试获取 IMyTestInterface 时, QueryInterface 返回E_NOINTERFACE:
procedure TForm3.Button1Click(Sender: TObject);
var
o: IMyTestObj;
i: IMyTestIntf;
r: HRESULT;
begin
o := CreateComObject(CLASS_MyTestObj) as IMyTestObj;
r := o.QueryInterface(IID_IMyTestIntf, i); //returns E_NOINTERFACE
ShowMessage(IntToStr(r));
end;
但是如果我把 IMyTestInterface 放到类型库中,一切都按预期工作。
由于 IMyTestInterface 将在多个模块中使用,因此我不会这样做。想要每次都将它添加到类型库中。
我做了一些调试,发现 QueryInterface 实际上对对象 QueryInterface 进行了两次调用:一次使用 IID_IMyTestIntf 返回S_OK,另一次使用GUID {AF86E2E0-B12D-4C6A-9C5A-D7AA65101E90} IInspectable ,这个返回E_NOINTERFACE。
是否有一个简单的解决方案可以将其他接口实现到COM对象中,而无需将其放入类型库中?