I see you are using the HKLM key to check the default client but this can be user depended, so you should really check the HKCU entry (and fall back to HKLM if HKCU does not have the entry).
I also removed the With
statement and used the ContainsText
(include StrUtils
unit) function instead of Pos
:
function IsOutlookTheDefaultEmailClient:Boolean;
var
Reg : TRegistry;
begin
Result := False;
Reg := TRegistry.Create;
try
// first check HKCU
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKeyReadOnly('Software\Clients\Mail') then
begin
Result := ContainsText(Reg.ReadString(''), 'Microsoft Outlook');
Reg.CloseKey;
end
// fall back to HKLM
else
begin
Reg.RootKey := HKEY_LOCAL_MACHINE;
// this part is susceptible to registry virtualization and my need elevation!
if Reg.OpenKeyReadOnly('Software\Clients\Mail') then
begin
Result := ContainsText(Reg.ReadString(''), 'Microsoft Outlook');
Reg.CloseKey;
end;
end;
finally
Reg.Free;
end;
end;
EDIT
The only time this code can fail is when Registry virtualization comes into play. This is typical in UAC scenarios. Please check on a failing computer if this key exists in regedit:
HKCU\Software\Classes\VirtualStore\MACHINE\SOFTWARE\Clients\Mail
if that is the case, your application will read this key and not the real HKLM key. The only solution is request elevation or run your application as administrator.