Author: Tomas Rutkauskas
At startup I need to determine if a COM server has been registered. If not, I will
call RegisterComServer. Now I am calling RegisterComServer every time the
application starts, which I do not think is a good idea. Is there a proper way to
test for it being registered?
Answer:
You can try several methods:
a) Use CLSIDFromProgID method:
1 { ... }2 var3 strOLE: string;
4 begin5 strOLE = "YourCOMServer.Application"; {your ProgID}6 if (CLSIDFromProgID(PWideChar(WideString(strOLE), ClassID) = S_OK) then7 begin8 { ... }9 end;
10 end;
b) Check the registry:
11 { ... }12 const13 cKEY = '\SOFTWARE\Classes\CLSID\%s\InprocServer32';
14 var15 sKey: string;
16 sComServer: string;
17 exists: boolean;
18 Reg: TRegistry;
19 begin20 Reg := TRegistry.Create;
21 try22 Reg.RootKey := HKEY_LOCAL_MACHINE;
23 sKey := format(cKEY, [GuidToString(ClassID)]);
24 if Reg.OpenKey(sKey, False) then25 begin26 sComServer := Reg.ReadString('');
27 if FileExists(sComServer) then28 begin29 { ... }30 end;
31 end;
32 finally33 Reg.free;
34 end;
35 end;