2010-06-03

取得 ActiveX 內的 CoClass 清單

如何在 Delphi 中取得一個 ActiveX 內部所有的 CoClass 清單呢?
const
  // This enumeration specifies various types of data and functions.
  // typedef enum tagTYPEKIND {
  TKIND_ENUM      = 0;
  TKIND_RECORD    = 1;
  TKIND_MODULE    = 2;
  TKIND_INTERFACE = 3;
  TKIND_DISPATCH  = 4;
  TKIND_COCLASS   = 5;
  TKIND_ALIAS     = 6;
  TKIND_UNION     = 7;
  TKIND_MAX       = 8;
  // } TYPEKIND;
function GetCoClassNames(aFileName: String): String;
  function DropUnderLine(AString:string):string;
  begin
    if AString[1] = '_' then
       AString[1]:= ' ';
    if AString[2] = '_' then
       AString[2]:= ' ';

    Result := Trim(AString);
  end;
var
  ii: Integer;
  vCoClassStrs: TStrings;
  vName,vDocStr : WideString;
  vTypeLib: ITypeLib;
  vTypeKind: TTypeKind;
begin
  vCoClassStrs := TStringList.Create;

  OleCheck(LoadTypeLib(PWideChar(aFileName), vTypeLib));

  // Get All CoClass
  for ii := 0 to vTypeLib.GetTypeInfoCount - 1 do
  begin
    OleCheck(vTypeLib.GetDocumentation(ii, @vName, @vDocStr, nil, nil));
    OleCheck(vTypeLib.GetTypeInfoType(ii, vTypeKind));
    if vTypeKind = TKIND_COCLASS then
    begin
      vCoClassStrs.Append(vName);
    end;
  end;
  Result := vCoClassStrs.Text;
  vCoClassStrs.Free;
end;
ActiveX

沒有留言: