Adding and removing a shell extension.


Adding and removing a shell extension.  
    

{ Add shell extension to registry } 
procedure AddShellExt(const GUID: TGUID; const ShellExtDll, FileExt, UtilityName: string); 
var Key: string; 
    BufGUID: array [0..255] of WideChar; 
    FileType: string; 
begin 
  with TRegistry.Create do 
  try 
    RootKey := HKEY_CLASSES_ROOT; 
    StringFromGUID2(GUID, BufGUID, SizeOf(BufGUID)); 
  
    if not OpenKey(FileExt, False) then Exit; 
  
    if FileExt <> '*' then 
    begin 
      FileType := ReadString(''); 
      CloseKey; 
      OpenKey(FileType, False); 
    end; 
  
    Key := Format('shellex\ContextMenuHandlers\%s', [UtilityName]); 
    OpenKey(Key, TRUE); 
    WriteString('', BufGUID); 
    CloseKey; 
  
    Key := Format('CLSID\%s', [BufGUID]); 
    OpenKey(Key, True); 
    WriteString('', UtilityName); 
    OpenKey('InprocServer32', True); 
    WriteString('', ShellExtDll); 
    WriteString('ThreadingModel', 'Apartment'); 
    CloseKey; 
  finally 
    Free; 
  end; {try} 
end; 
  
  
{ Remove shell extension from registry } 
procedure RemoveShellExt(const GUID: TGUID; const ShellExtDll, FileExt, UtilityName: string); 
var Key: string; 
    BufGUID: array [0..255] of WideChar; 
    FileType: string; 
begin 
  with TRegistry.Create do 
  try 
    RootKey := HKEY_CLASSES_ROOT; 
    StringFromGUID2(GUID, BufGUID, SizeOf(BufGUID)); 
    FileType := FileExt; 
    if FileType <> '*' then 
    begin 
      if not OpenKey(FileExt, False) then Exit; 
      FileType := ReadString(''); 
      CloseKey; 
    end; 
  
    Key := Format('%s\shellex\ContextMenuHandlers\%s', [FileType,UtilityName]); 
    DeleteKey(Key); 
    Key := Format('CLSID\%s', [BufGUID]); 
    DeleteKey(Key); 
  finally 
    Free; 
  end; {try} 
end;