We help IT Professionals succeed at work.

Test if some program is installed...

ssdjgru1
ssdjgru1 asked
on
Does anybody know how to test if some program has been installed on computer. Can this be done in any other way then teseting the fileexistence?
(I would like to test if Powerpoint viewer has been installed)
Comment
Watch Question

Commented:
HI,
Read the Registry.
Sincerely,
Nestorua.

Commented:
Hi,

Try this code:


uses
  Registry;
   
procedure TForm1.Button1Click(Sender: TObject);
const
  UNINST_PATH = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
var
  Reg: TRegistry;
  SubKeys: TStringList;
  ListItem: TlistItem;
  i: integer;
  sDisplayName, sUninstallString: string;
begin
{
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.add;
  ListView1.Columns.add;
  ListView1.Columns[0].caption := 'DisplayName';
  ListView1.Columns[1].caption := 'UninstallString';
  ListView1.Columns[0].Width := 300;
  ListView1.Columns[1].Width := 300;
}
  Reg := TRegistry.Create;
  with Reg do
    try
      with ListView1.Items do
        try
          BeginUpdate;
          Clear;
          RootKey := HKEY_LOCAL_MACHINE;
          if OpenKeyReadOnly(UNINST_PATH) then
          begin
            SubKeys := TStringList.Create;
            try
              GetKeyNames(SubKeys);
              CloseKey;
              for i := 0 to subKeys.Count - 1 do
                if OpenKeyReadOnly(Format('%s\%s', [UNINST_PATH, SubKeys[i]])) then
                  try
                    sDisplayName     := ReadString('DisplayName');
                    sUninstallString := ReadString('UninstallString');
                    if sDisplayName <> '' then
                    begin
                      ListItem         := Add;
                      ListItem.Caption := sDisplayName;
                      ListItem.subitems.Add(sUninstallString);
                    end;
                  finally
                    CloseKey;
                  end;
            finally
              SubKeys.Free;
            end;
          end;
        finally
          ListView1.AlphaSort;
          EndUpdate;
        end;
    finally
      CloseKey;
      Free;
    end;
end;

HTH

tas

Commented:
A tips found some time ago . Work perfect and return a list of all instaled programs . Just search the list and see if your program is installed .




unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//  IF  THE  FUNCTION  SUCCEEDS,  THE  RETURN  VALUE  IS  THE  NUMBER  OF  INSTALLED  PROGRAMS
//  OTHERWISE  THE  RETURN  VALUE  IS  -1  
FUNCTION  GETINSTALLEDPROGRAMS(PRGLISTBOX  :  TLISTBOX)  :  INTEGER;  
VAR  
RS :  STRING;
OPENKEY  :  HKEY;

KEYINDEX,  
KEYNAMELENGTH,  
RETVALUE :  CARDINAL;
KEYNAMEBUF :  ARRAY  OF  CHAR;
 
FUNCTION  GETDISPLAYNAME(KEYNAME : STRING) : STRING;
VAR  
  PRGKEY : HKEY;
  NAMEBUF : ARRAY[0..255]  OF  CHAR;
  VALUETYPE,  
  VALUESIZE : CARDINAL;
  ERR : CARDINAL;
BEGIN  
  RESULT := '';
  IF  REGOPENKEY(HKEY_LOCAL_MACHINE,  
      PCHAR('SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL'+'\'  +  KEYNAME),PRGKEY)
        <>  ERROR_SUCCESS  THEN  RESULT  :=  '*'  ELSE  
  BEGIN
    VALUESIZE  :=  LENGTH(NAMEBUF);  
    IF  REGQUERYVALUEEX(PRGKEY,  PCHAR('DISPLAYNAME'),  NIL,  @VALUETYPE,  @NAMEBUF,  @VALUESIZE)  
          <>  ERROR_SUCCESS  THEN  
    BEGIN  
      VALUESIZE  :=  LENGTH(NAMEBUF);
      IF  REGQUERYVALUEEX(PRGKEY,  PCHAR('QUIETDISPLAYNAME'),  NIL,  @VALUETYPE,  @NAMEBUF,  @VALUESIZE)
            <>  ERROR_SUCCESS  THEN  RESULT  :=  KEYNAME;  
    END;  
    IF  RESULT  =  ''  THEN  RESULT  :=  STRING(NAMEBUF);  
    REGCLOSEKEY(PRGKEY);  
  END;  
END;

 
BEGIN  
RESULT  :=  0;  
SETLENGTH(KEYNAMEBUF,  256);  
//  OPEN  KEY
IF  REGOPENKEY(HKEY_LOCAL_MACHINE,  
    PCHAR('SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL'),OPENKEY)
      <>  ERROR_SUCCESS  THEN  RESULT  :=  -1  ELSE  
BEGIN  
  KEYINDEX  :=  0;  
  REPEAT
    KEYNAMELENGTH := LENGTH(KEYNAMEBUF);
    RETVALUE := REGENUMKEYEX(OPENKEY,  KEYINDEX,  PCHAR(KEYNAMEBUF),
    KEYNAMELENGTH,  NIL,  NIL,  NIL,  NIL);

    IF  RETVALUE  =  ERROR_SUCCESS  THEN  
    BEGIN
      RS  :=  GETDISPLAYNAME(PCHAR(KEYNAMEBUF));  
      IF  RS  <>  '*'  THEN  
      BEGIN  
         //ADD  THE  PRG  NAME  TO  THE  LISTBOX
         PRGLISTBOX.ITEMS.ADD(RS);
        INC(RESULT);
      END  ELSE  RESULT  :=  -1;  
    END;  
    INC(KEYINDEX);
    APPLICATION.PROCESSMESSAGES;  
  UNTIL  (RETVALUE  <>  ERROR_SUCCESS)OR(RESULT  =  -1);  
  REGCLOSEKEY(OPENKEY);
END;
SETLENGTH(KEYNAMEBUF,  0);
END;  
 

PROCEDURE  TFORM1.BUTTON1CLICK(SENDER:  TOBJECT);  
BEGIN
IF  GETINSTALLEDPROGRAMS(LISTBOX1)  =  -1  THEN  SHOWMESSAGE('ERROR');
END;  
 
//AND  NOW  THAT  U  HAVE  HOW  MANY  PROGRAMS  THAT  ARE  INSTALLED,  U  CAN  JUST
//LOOP  THROUGH  ALL  THE  KEYS  IN  HERE:  
//HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\UNINSTALL


end.
Manuel Lopez-MichelonePhD candidate
CERTIFIED EXPERT

Commented:
listening...

Explore More ContentExplore courses, solutions, and other research materials related to this topic.