C++ Builder 参考手册 ➙ System ➙ TObject ➙ GetInterface
头文件:#include <systobj.h>
命名空间:System
函数原型:
bool __fastcall GetInterface(const GUID &IID, /* out */ void *Obj);
template <typename T>
bool __fastcall GetInterface(DelphiInterface<T> &smartIntf)
{
return GetInterface(__uuidof(T), reinterpret_cast<void*>(static_cast<T**>(&smartIntf)));
}
System::TObject::GetInterface 是 System::TObject 的成员函数,获取指定的接口。GetInterface 相当于 Delphi 里面的 as 和 C++ 里面的 dynamic_cast,但是不会抛出异常,如果指定的接口不支持。
参数:
Delphi 的 IID 允许用接口名称,编译器会自动采用类型的 GUID。
C++ 可以用模板版本的 GetInterface,相当于 dynamic_cast,GetInterface 在转为不支持的类型的时候不会抛出异常,只是得到的指针为 NULL。
参数 Obj 虽然是 void * 类型的,但是实质上是 void ** 类型的,必须把 void ** 强制转为 void * 作为 Obj 参数。
返回值:
true: 成功,通过 Obj 参数返回接口;
false: 失败。
参考:
- System::TObject::GetInterfaceEntry
- System::TObject::GetInterfaceTable
- System::Classes::TComponent::QueryInterface
- System::Classes::TComponent::IsImplementorOf
- System::Classes::TComponent::ReferenceInterface
- System::Classes::TComponent::GetTypeInfo
- System::Classes::TComponent::GetTypeInfoCount
- System::Classes::TComponent::GetIDsOfNames
- System::Classes::TComponent::operator _di_IInterface
- System::Classes::TInterfacedPersistent::operator _di_IInterface
- System::TObject
- VCL 类继承关系
C++ Builder 参考手册 ➙ System ➙ TObject ➙ GetInterface