views:

208

answers:

1

How can I get all installed components in TStrings? I think this code work only in packages:

    uses TypInfo, ToolIntf, Exptintf;

procedure GetComponentNames(lst: TStrings);
var
  i, k: Integer;
  CRef: TClass;
  strName: ShortString;
begin
  lst.Clear;
  for i := 0 to ToolServices.GetModuleCount-1 do
  begin
    for k := 0 to ToolServices.GetComponentCount(i)-1 do
      begin
       CRef := TClass(GetClass(ToolServices.GetComponentName(i, k)));
       while CRef <> nil do
       begin
         strName := CRef.ClassName;
         if lst.IndexOf(strName) = -1 then
           lst.Add(strName);
         if str <> 'TComponent' then
          CRef := CRef.ClassParent
         else
           CRef := nil;
       end;
      end;
  end;    
end;

Or:

uses ToolsApi;

{....}

var
  a, i: Integer;
begin
  with (BorlandIDEServices as IOTAPackageServices) do
  begin
    for a := 0 to GetPackageCount - 1 do
    begin
      for i := 0 to GetComponentCount(a) - 1 do
      begin
        {get each component name with GetComponentName(a, i);}
        // DoSomething   
      end;
    end;
  end;
end;

Thanks for help.

+2  A: 

This example doesn't use the OpenAPI, it uses the Registry. It works but it also lists non-visual components amongst other hidden items.

procedure GetComponentNames(lst: TStrings);
var
  i, j, iPos: Integer;
  Reg: TRegistry;
  sComponent: String;
  slValues, slData: TStrings;
begin
  Reg := TRegistry.Create;
  slValues := TStringList.Create;
  slData := TStringList.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    Reg.OpenKey('Software\Borland\Delphi\6.0\Palette', False); // Change reg key where appropriate
    Reg.GetValueNames(slValues);
    for i := 0 to Pred(slValues.Count) do
    begin
      lst.Append(slValues[i]);
      lst.Append('----------');
      slData.Delimiter := ';';
      slData.DelimitedText := Reg.ReadString(slValues[i]);
      for j := 0 to Pred(slData.Count) do
      begin
        sComponent := slData[j];
        iPos := Pos('.', sComponent);
        if (iPos > 0) then
          Delete(sComponent, 1, iPos);
        lst.Append(sComponent);
      end;
    end;
  finally
    slData.Free;
    slValues.Free;
    Reg.Free;
  end; {try..finally}
end;

I'm not saying this is ideal but it does give you a list and a headstart.

_J_
Thanks a lot, Its intelligent idea to use DelimitedText, I’ll perform a small filter to get only components, may be add something like:if (Length(sComponent) > 1) and (sComponent[1] = 'T') then
Kachwahed
Yes, it's all a bit of a hack but it does the job required!
_J_