如何获取Delphi中可通过IDispatch访问的实例支持的所有接口?
获取IDispatch指针可访问的所有接口问题
背景
我之前尝试从Delphi应用访问所有已打开的Excel实例,最终得到了IDispatch指针。我推测该IDispatch指针可以访问Excel的ActiveX实用接口,比如ExcelApplication、Workbook、Worksheet等。
尝试的代码
我用以下代码检查IDispatch指针:
var IDisp: IDispatch; Report: TStringList; TypeInfo: ITypeInfo; TypeAttr: PTypeAttr; ii: Integer; RefTypeInfoCard: Cardinal; RefTypeInfo: ITypeInfo; RefTypeInfoAttr: PTypeAttr; begin //... if Succeeded(IDisp.GetTypeInfo(0, 0, TypeInfo)) then begin // Get the TYPEATTR structure for the interface if Succeeded(TypeInfo.GetTypeAttr(TypeAttr)) then begin try // Loop through all of the supported interfaces and print their GUIDs for ii := 0 to TypeAttr.cImplTypes - 1 do begin if Succeeded(TypeInfo.GetRefTypeOfImplType(ii, RefTypeInfoCard)) then begin if Succeeded(TypeInfo.GetRefTypeInfo(RefTypeInfoCard, RefTypeInfo)) then begin if Succeeded(RefTypeInfo.GetTypeAttr(RefTypeInfoAttr)) then begin Report.Add(' '+GUIDToString(RefTypeInfoAttr.guid)); end; end; end; end; finally TypeInfo.ReleaseTypeAttr(TypeAttr); end; end; end; end;
当前问题
我的思路是从IDisp读取TypeInfo,获取所有实现的接口,再读取每个接口的TypeInfo并获取其GUID。但结果仅得到IDispatch自身的GUID {00020400-0000-0000-C000-000000000046},没有获取到其他新接口。
请问如何改写代码以获取给定IDispatch指针可访问的所有接口?我使用的是Delphi 6和Delphi 2009,推测新版本情况类似。
内容的提问来源于stack exchange,提问作者TomR
相关产品推荐
相关产品推荐

