Browse Source

Logging Un/Registering to handle protocol URL addresses

Source commit: 8448f6de5b55dc642fb6114c0830b85812360b71
Martin Prikryl 1 year ago
parent
commit
d82e155904
3 changed files with 43 additions and 7 deletions
  1. 8 2
      source/core/Common.cpp
  2. 1 1
      source/core/Common.h
  3. 34 4
      source/windows/Setup.cpp

+ 8 - 2
source/core/Common.cpp

@@ -137,7 +137,7 @@ UnicodeString MakeValidFileName(UnicodeString FileName)
   return FileName;
 }
 //---------------------------------------------------------------------------
-UnicodeString RootKeyToStr(HKEY RootKey)
+UnicodeString RootKeyToStr(HKEY RootKey, const UnicodeString & Default)
 {
   if (RootKey == HKEY_USERS) return L"HKU";
     else
@@ -151,7 +151,13 @@ UnicodeString RootKeyToStr(HKEY RootKey)
     else
   if (RootKey == HKEY_DYN_DATA) return L"HKDD";
     else
-  {  Abort(); return L""; };
+  {
+    if (Default.IsEmpty())
+    {
+      Abort();
+    }
+    return Default;
+  };
 }
 //---------------------------------------------------------------------------
 UnicodeString BooleanToEngStr(bool B)

+ 1 - 1
source/core/Common.h

@@ -48,7 +48,7 @@ void __fastcall Shred(RawByteString & Str);
 UnicodeString AnsiToString(const RawByteString & S);
 UnicodeString AnsiToString(const char * S, size_t Len);
 UnicodeString MakeValidFileName(UnicodeString FileName);
-UnicodeString RootKeyToStr(HKEY RootKey);
+UnicodeString RootKeyToStr(HKEY RootKey, const UnicodeString & Default = EmptyStr);
 UnicodeString BooleanToStr(bool B);
 UnicodeString BooleanToEngStr(bool B);
 UnicodeString DefaultStr(const UnicodeString & Str, const UnicodeString & Default);

+ 34 - 4
source/windows/Setup.cpp

@@ -281,6 +281,11 @@ void __fastcall RemoveSearchPath(const UnicodeString Path)
 //---------------------------------------------------------------------------
 static const UnicodeString SoftwareClassesBaseKey = L"Software\\Classes\\";
 //---------------------------------------------------------------------------
+static UnicodeString KeyName(HKEY RootKey, const UnicodeString & Key)
+{
+  return FORMAT(L"%s\\%s", (RootKeyToStr(RootKey, L"Unknown"), Key));
+}
+//---------------------------------------------------------------------------
 static void __fastcall DeleteKeyIfEmpty(TRegistry * Registry, const UnicodeString & Key, bool AllowRootValues)
 {
   if (Registry->OpenKey(Key, false))
@@ -310,6 +315,7 @@ static void __fastcall DeleteKeyIfEmpty(TRegistry * Registry, const UnicodeStrin
 
     Registry->CloseKey();
 
+    UnicodeString AKeyName = KeyName(Registry->RootKey, Key);
     if (CanDelete)
     {
       for (int Index = 0; Index < List->Count; Index++)
@@ -319,6 +325,11 @@ static void __fastcall DeleteKeyIfEmpty(TRegistry * Registry, const UnicodeStrin
 
       // will fail, if not all subkeys got removed
       Registry->DeleteKey(Key);
+      AppLogFmt(L"Deleted key %s", (AKeyName));
+    }
+    else
+    {
+      AppLogFmt(L"Cannot delete non-empty key %s", (AKeyName));
     }
   }
 }
@@ -335,7 +346,8 @@ static void __fastcall RegisterProtocol(TRegistry * Registry,
   UnicodeString ProtocolKey = SoftwareClassesBaseKey + Protocol;
   if (Force || !Registry->KeyExists(ProtocolKey))
   {
-    if (Registry->OpenKey(SoftwareClassesBaseKey + Protocol, true))
+    UnicodeString Key = SoftwareClassesBaseKey + Protocol;
+    if (Registry->OpenKey(Key, true))
     {
       Registry->WriteString(L"", Description);
       Registry->WriteString(L"URL Protocol", L"");
@@ -350,6 +362,7 @@ static void __fastcall RegisterProtocol(TRegistry * Registry,
       {
         Abort();
       }
+      AppLogFmt(L"Created %s", (KeyName(Registry->RootKey, Key)));
     }
     else
     {
@@ -381,13 +394,15 @@ static void __fastcall RegisterAsUrlHandler(HKEY RootKey,
 
   RegisterProtocol(Registry.get(), Protocol, Description, true);
 
-  if (Registry->OpenKey(SoftwareClassesBaseKey + Protocol, false) &&
+  UnicodeString Key = SoftwareClassesBaseKey + Protocol;
+  if (Registry->OpenKey(Key, false) &&
       Registry->OpenKey(L"shell", true) &&
       Registry->OpenKey(L"open", true) &&
       Registry->OpenKey(L"command", true))
   {
     Registry->WriteString(L"", FORMAT(L"\"%s\" %s \"%%1\"", (Application->ExeName, TProgramParams::FormatSwitch(UNSAFE_SWITCH))));
     Registry->CloseKey();
+    AppLogFmt(L"Added command to %s", (KeyName(RootKey, Key)));
   }
   else
   {
@@ -521,15 +536,20 @@ static void __fastcall RegisterProtocolForDefaultPrograms(HKEY RootKey, const Un
   Registry->WriteString(Protocol, GenericUrlHandler);
   Registry->CloseKey();
 
+  AppLogFmt(L"Added capabilities to %s", (KeyName(RootKey, CapabilitiesKey)));
+
   // register application
 
-  if (!Registry->OpenKey(L"Software\\RegisteredApplications", true))
+  UnicodeString ApplicationsKey = L"Software\\RegisteredApplications";
+  if (!Registry->OpenKey(ApplicationsKey, true))
   {
     Abort();
   }
 
-  Registry->WriteString(AppNameString(), CapabilitiesKey);
+  UnicodeString AppName = AppNameString();
+  Registry->WriteString(AppName, CapabilitiesKey);
   Registry->CloseKey();
+  AppLogFmt(L"Registered %s in %s", (AppName, KeyName(RootKey, ApplicationsKey)));
 }
 //---------------------------------------------------------------------------
 static void __fastcall UnregisterProtocolForDefaultPrograms(HKEY RootKey,
@@ -640,16 +660,21 @@ static void __fastcall NotifyChangedAssociations()
 //---------------------------------------------------------------------------
 void __fastcall RegisterForDefaultProtocols()
 {
+  AppLog(L"Registering to handle protocol URL addresses");
   if (IsWinVista())
   {
+    AppLog(L"Registering as default program");
     RegisterForDefaultPrograms();
   }
   else
   {
+    AppLog(L"Registering for non-browser protocols");
     RegisterAsNonBrowserUrlHandler(UnicodeString());
   }
 
+  AppLog(L"Registering for non-browser protocols with prefix");
   RegisterAsNonBrowserUrlHandler(WinSCPProtocolPrefix);
+  AppLog(L"Registering for browser protocols with prefix");
   RegisterAsUrlHandler(WinSCPProtocolPrefix + FtpProtocol.UpperCase());
   RegisterAsUrlHandler(WinSCPProtocolPrefix + FtpsProtocol.UpperCase());
   RegisterAsUrlHandler(WinSCPProtocolPrefix + FtpesProtocol.UpperCase());
@@ -657,11 +682,14 @@ void __fastcall RegisterForDefaultProtocols()
   RegisterAsUrlHandler(WinSCPProtocolPrefix + HttpsProtocol.UpperCase());
   RegisterAsUrlHandler(WinSCPProtocolPrefix + SshProtocol.UpperCase());
 
+  AppLog(L"Notifying about changes");
   NotifyChangedAssociations();
+  AppLog(L"Registration done");
 }
 //---------------------------------------------------------------------------
 void __fastcall UnregisterForProtocols()
 {
+  AppLog(L"Unregistering from handling protocol URL addresses");
   UnregisterAsUrlHandlers(UnicodeString(), false);
   UnregisterAsUrlHandlers(WinSCPProtocolPrefix, true);
   UnregisterAsUrlHandler(WinSCPProtocolPrefix + FtpProtocol.UpperCase(), true);
@@ -674,7 +702,9 @@ void __fastcall UnregisterForProtocols()
   UnregisterProtocolsForDefaultPrograms(HKEY_CURRENT_USER, false);
   UnregisterProtocolsForDefaultPrograms(HKEY_LOCAL_MACHINE, false);
 
+  AppLog(L"Notifying about changes");
   NotifyChangedAssociations();
+  AppLog(L"Unregistration done");
 }
 //---------------------------------------------------------------------------
 void __fastcall LaunchAdvancedAssociationUI()