首页  编辑  

判断一个COM接口是否注册

Tags: /超级猛料/COM、ActiveX,DDE/COM_ DCOM/   Date Created:
Borland news: Nick Hodges - TeamB
function ClassIsRegistered(const clsid: TCLSID): Boolean;
var
 OleStr: POleStr;
 Reg: TRegInifile;
 Key, Filename: String;
begin
 // First, check to see if there is a ProgID.  This will tell if the
 // control is registered on the machine.  No ProgID, control won't
run
 Result := ProgIDFromCLSID(clsid, OleStr) = S_OK;
 if not Result then Exit; / //Bail as soon as anything goes wrong.
 // Next, make sure that the file is actually there by rooting it out
 // of the registry
 Key := Format('\SOFTWARE\Classes\CLSID\%s', [GUIDToString(clsid)]);
 Reg := TRegInifile.Create;
 try
   Reg.RootKey := HKEY_LOCAL_MACHINE;
   Result := Reg.OpenKeyReadOnly(Key);
   if not Result then Exit; // Bail as soon as anything goes wrong.
   FileName := Reg.ReadString('InProcServer32', '', EmptyStr);
   if (Filename = EmptyStr) then // try another key for the file name
   begin
     FileName := Reg.ReadString('InProcServer', '', EmptyStr);
   end;
   Result := Filename <> EmptyStr;
   if not Result then Exit;
   Result := FileExists(Filename);
 finally
   Reg.Free;
 end;
end;

function IsClassRegistered(GUID: TGUID): Boolean; var  ClassID: String; begin  with TRegistry.Create do  try    ClassID := GUIDToString(GUID);    RootKey := HKEY_CLASSES_ROOT;    Result := OpenKey('\CLSID\' + ClassID, False);  finally    Free;  end; end;

function CheckCOMInstalled(const Name : string): Boolean; overload;
begin
 try
   Result := CreateOleObject(Name) <> nil;
 except
   Result := False;
 end;
end;

function CheckCOMInstalled(const CLSID : TGUID): Boolean; overload;
begin
 try
   Result := CreateComObject(CLSID) <> nil;
 except
   Result := False;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
 GID : TGUID='{D27CDB6E-AE6D-11cf-96B8-444553540000}';
begin
if CheckCOMInstalled(GID) then
  Caption := 'OK'
else
  Caption := 'Not';
end;