6

i need to know how can detect if an OCX class (ClassID) is registred in Windows

something like

function IsClassRegistered(ClassID:string):boolean;
begin
//the magic goes here
end;

begin
  if IsClassRegistered('{26313B07-4199-450B-8342-305BCB7C217F}') then
  // do the work
end;
Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Salvador
  • 16,132
  • 33
  • 143
  • 245
  • 1
    Be warned that this does not mean the actual ocx is on the disk and in the right location. And even then it does not mean the ocx can be loaded without problems. And since there is registration free com (http://msdn.microsoft.com/en-us/library/ms973913.aspx) it also does not tell if the ocx is potentially usable. – Lars Truijens Oct 31 '10 at 21:50

3 Answers3

8

you can check the existence of the CLSID under the HKEY_CLASSES_ROOT in the windows registry.

check this sample

function ExistClassID(const ClassID :string): Boolean;
var
    Reg: TRegistry;
begin
 try
     Reg := TRegistry.Create;
   try
     Reg.RootKey := HKEY_CLASSES_ROOT;
     Result      := Reg.KeyExists(Format('CLSID\%s',[ClassID]));
   finally
     Reg.Free;
   end;
 except
    Result := False;
 end;
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483
3

The problem with (many, many) suggestions of crawling the registry is that:

  • there is more than one registry location you would need to look at
  • a class can be registered and not exist in the registry

Registration-free COM allows a class to be available without it being registered. Conceptually you don't want to know if a class is "registered", you just want to know it is registered enough to be created.

Unfortunately the only (and best) way to do that is to create it:

//Code released into public domain. No attribution required.
function IsClassRegistered(const ClassID: TGUID): Boolean;
var
    unk: IUnknown;
    hr: HRESULT;
begin
    hr := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, {out}unk);
    unk := nil;

    Result := (hr <> REGDB_E_CLASSNOTREG);
end;
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
2

ActiveX/COM is a complex beast, registrations have many pieces to them, and Vista+ onward make it more complicated with UAC Registry Virtualization rules.

The best option is to simply attempt to instantiate the OCX and see if it succeeds or fails. That will tell you whether the OCX is registered correctly, all the pieces are hooked up, whether the OCX is even usable within the calling user's context, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770