2005. március 10., csütörtök

How to get a list of all registered typelibs


Problem/Question/Abstract:

Does anybody know how to get a list of all registered typelibs (like the list in the typelib import window in Delphi)? I found a place in the registry (HKCR\TypeLib\...) but a lot of libs are listed more then one time (up to 4 or 5 times) in that place. Do I have to grab all libs from this place? I have found no API (like EnumTypeLibs) that does this.

Answer:

procedure EnumTypeLibs(TypeLibNames: TStringList);
var
  f: TRegistry;
  keyNames, keyVersions, keyInfos: TStringList;
  keyName, keyVersion, keyInfo, tlName: string;
  i, j, k: Integer;
begin
  TypeLibNames.Clear;
  {TypeLibNames.Sorted := True;}
  keyNames := nil;
  keyVersions := nil;
  keyInfos := nil;
  f := TRegistry.Create;
  try
    keyNames := TStringList.Create;
    keyVersions := TStringList.Create;
    keyInfos := TStringList.Create;
    f.RootKey := HKEY_CLASSES_ROOT;
    if not f.OpenKey('TypeLib', False) then
      raise Exception.Create('TRegistry.Open');
    f.GetKeyNames(keyNames);
    f.CloseKey;
    for i := 0 to keyNames.Count - 1 do
    begin
      keyName := keyNames.Strings[i];
      if not f.OpenKey(Format('TypeLib\%s', [keyName]), False) then
        Continue;
      f.GetKeyNames(keyVersions);
      f.CloseKey;
      for j := 0 to keyVersions.Count - 1 do
      begin
        keyVersion := keyVersions.Strings[j];
        if not f.OpenKey(Format('TypeLib\%s\%s', [keyName, keyVersion]), False) then
          Continue;
        tlName := f.ReadString('');
        f.GetKeyNames(keyInfos);
        f.CloseKey;
        for k := 0 to keyInfos.Count - 1 do
        begin
          keyInfo := keyInfos.Strings[k];
{$B-}
          if (keyInfo = '') or (keyInfo[1] < '0') or (keyInfo[1] > '9') then
            Continue;
          if not f.OpenKey(Format('TypeLib\%s\%s\%s\win32', [keyName, keyVersion,
            keyInfo]), False) then
            Continue;
          f.CloseKey;
          TypeLibNames.Add(Format('%s ver.%s', [tlName, keyVersion]));
        end;
      end;
    end;
  finally
    f.Free;
    keyNames.Free;
    keyVersions.Free;
    keyInfos.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése