Index: scripts/setup.iss =================================================================== diff -u -r45f4f963e795a1c2d96aca7b061d56ea54811c9b -r58de8d7360813537b384eff808c031f9e63db4de --- scripts/setup.iss (.../setup.iss) (revision 45f4f963e795a1c2d96aca7b061d56ea54811c9b) +++ scripts/setup.iss (.../setup.iss) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -169,6 +169,8 @@ [Run] Filename: "{app}\{code:ExpandArch|ExeFilename}"; Flags: nowait postinstall skipifsilent; Description: "{cm:LaunchProgram,{#MyAppName}}" +Filename: "Reg.exe"; Parameters: "delete ""SOFTWARE\Microsoft\Windows\CurrentVersion\Run"" /v ""Copy Handler"" /f"; Flags: runasoriginaluser; +Filename: "Reg.exe"; Parameters: "add ""SOFTWARE\Microsoft\Windows\CurrentVersion\Run"" /v ""Copy Handler"" /t REG_SZ /d ""{app}\{code:ExpandArch|ExeFilename}"" /f"; Flags: runasoriginaluser postinstall; Tasks: startatboot [Registry] Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ValueType: none; ValueName: "Copy Handler"; Flags: deletevalue uninsdeletevalue @@ -188,6 +190,9 @@ [ThirdParty] CompileLogMethod=append +[UninstallRun] +Filename: "Reg.exe"; Parameters: "delete ""SOFTWARE\Microsoft\Windows\CurrentVersion\Run"" /v ""Copy Handler"" /f"; Flags: runasoriginaluser; + [Code] function ExpandArch(ConstantStr: String): String; begin @@ -206,16 +211,3 @@ end; end; end; - -procedure CurStepChanged(CurStep: TSetupStep); -var - ResultCode: Integer; -begin - if CurStep = ssPostInstall then - begin - if IsTaskSelected('startatboot') then - ExecAsOriginalUser(ExpandConstant('{app}\{code:ExpandArch|ExeFilename}'), '--EnableLaunchAtStartup=1', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) - else - ExecAsOriginalUser(ExpandConstant('{app}\{code:ExpandArch|ExeFilename}'), '--EnableLaunchAtStartup=0', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) - end; -end; Index: src/ch/AppAutorun.cpp =================================================================== diff -u --- src/ch/AppAutorun.cpp (revision 0) +++ src/ch/AppAutorun.cpp (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -0,0 +1,37 @@ +#include "stdafx.h" +#include "AppAutorun.h" +#include "../common/TRegistry.h" +#include "../libstring/TString.h" +#include "../libchcore/TPath.h" + +AppAutorun::AppAutorun(const string::TString& strAppName, const chcore::TSmartPath& pathApp) : + m_strAppName(strAppName), + m_pathApp(pathApp) +{ +} + +bool AppAutorun::IsAutorunEnabled() const +{ + TRegistry reg(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), true); + + std::wstring wstrPath; + if (!reg.QueryString(m_strAppName.c_str(), wstrPath)) + return false; + + if (m_pathApp.Compare(chcore::PathFromString(wstrPath.c_str())) != 0) + return false; + + return true; +} + +void AppAutorun::SetAutorun(bool bEnable) +{ + if (IsAutorunEnabled() == bEnable) + return; + + TRegistry reg(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), false); + if (bEnable) + reg.SetString(m_strAppName.c_str(), m_pathApp.ToString()); + else + reg.DeleteValue(m_strAppName.c_str()); +} Index: src/ch/AppAutorun.h =================================================================== diff -u --- src/ch/AppAutorun.h (revision 0) +++ src/ch/AppAutorun.h (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -0,0 +1,34 @@ +// ============================================================================ +// Copyright (C) 2001-2019 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#pragma once +#include "../libstring/TString.h" +#include "../libchcore/TPath.h" + +class AppAutorun +{ +public: + AppAutorun(const string::TString& strAppName, const chcore::TSmartPath& pathApp); + + bool IsAutorunEnabled() const; + void SetAutorun(bool bEnable); + +private: + string::TString m_strAppName; + chcore::TSmartPath m_pathApp; +}; Index: src/ch/AppHelper.cpp =================================================================== diff -u -rd9527df01ee91b35d9a5fdccb80ded25a9c8265f -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision d9527df01ee91b35d9a5fdccb80ded25a9c8265f) +++ src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -112,6 +112,14 @@ return m_pathProcessor.GetProgramPath(); } +CString CAppHelper::GetFullProgramPath() const +{ + CString strKey; + strKey.Format(_T("%s\\%s"), (PCTSTR)m_pathProcessor.GetProgramPath(), m_pszProgramName); + + return strKey; +} + bool CAppHelper::IsInPortableMode() { if(!m_optPortableMode.is_initialized()) @@ -126,66 +134,3 @@ return m_optPortableMode.get(); } - -bool CAppHelper::SetAutorun(bool bEnable) -{ - // check the current key value (to avoid irritating messages from some firewall software) - HKEY hkeyRun = nullptr; - CString strValue; - CString strKey; - DWORD dwType = REG_SZ; - DWORD dwCount = _MAX_PATH * sizeof(TCHAR); - - LSTATUS lStatus = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_QUERY_VALUE, &hkeyRun); - if(lStatus != ERROR_SUCCESS) - return false; - - lStatus = RegQueryValueEx(hkeyRun, m_pszAppName, nullptr, &dwType, (BYTE*)strValue.GetBufferSetLength(_MAX_PATH), &dwCount); - RegCloseKey(hkeyRun); - - if(lStatus != ERROR_SUCCESS && lStatus != ERROR_FILE_NOT_FOUND) - { - strValue.ReleaseBuffer(0); - return false; - } - if(lStatus == ERROR_FILE_NOT_FOUND) - { - strValue.ReleaseBuffer(0); - - // if there is no key in registry and we don't want it, then return with ok status - if(!bEnable) - return true; - - // format the data to be written to registry - strKey.Format(_T("%s\\%s"), (PCTSTR)m_pathProcessor.GetProgramPath(), m_pszProgramName); - } - else - { - // key found - strValue.ReleaseBuffer(dwCount / sizeof(TCHAR)); - - if(bEnable) - { - // key exists in registry, check if the value is correct - strKey.Format(_T("%s\\%s"), (PCTSTR)m_pathProcessor.GetProgramPath(), m_pszProgramName); - - if(strValue.CompareNoCase(strKey) == 0) - return true; - } - } - - // we want to write information to the registry - // storing key in registry - lStatus = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hkeyRun); - if(lStatus != ERROR_SUCCESS) - return false; - - if(bEnable) - lStatus = RegSetValueEx(hkeyRun, m_pszAppName, 0, REG_SZ, (BYTE*)(PCTSTR)strKey, (DWORD)(strKey.GetLength() + 1) * sizeof(TCHAR)); - else - lStatus = RegDeleteValue(hkeyRun, m_pszAppName); - - RegCloseKey(hkeyRun); - - return lStatus == ERROR_SUCCESS; -} Index: src/ch/AppHelper.h =================================================================== diff -u -rf102f95617e81f56adbed6403e42602ea8c509b5 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/AppHelper.h (.../AppHelper.h) (revision f102f95617e81f56adbed6403e42602ea8c509b5) +++ src/ch/AppHelper.h (.../AppHelper.h) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -28,8 +28,6 @@ CAppHelper(); virtual ~CAppHelper(); - bool SetAutorun(bool bState); // changes state of "run with system" option - bool IsFirstInstance() const { return m_bFirstInstance; } PCTSTR GetAppName() const { return m_pszAppName; } @@ -42,6 +40,8 @@ CString ExpandPath(CString strPath); CString GetProgramPath() const; + CString GetFullProgramPath() const; + bool IsInPortableMode(); protected: Index: src/ch/CfgProperties.h =================================================================== diff -u -r0d5b67ee96b435d63f7bf075dc8e28603793b187 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/CfgProperties.h (.../CfgProperties.h) (revision 0d5b67ee96b435d63f7bf075dc8e28603793b187) +++ src/ch/CfgProperties.h (.../CfgProperties.h) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -29,7 +29,6 @@ { PP_PCLIPBOARDMONITORING = 0, PP_PMONITORSCANINTERVAL, - PP_PRELOADAFTERRESTART, PP_PCHECK_FOR_UPDATES_FREQUENCY, PP_PUPDATECHANNEL, @@ -200,7 +199,6 @@ // General settings PROPERTY(PP_PCLIPBOARDMONITORING, bool, _T("CHConfig.General.Program.EnableClipboardMonitoring"), false); PROPERTY_MINMAX(PP_PMONITORSCANINTERVAL, unsigned int, _T("CHConfig.General.Program.ClipboardMonitorScanInterval"), 1000, 0, 3600UL*1000UL); -PROPERTY(PP_PRELOADAFTERRESTART, bool, _T("CHConfig.General.Program.RunWithSystem"), false); PROPERTY_MINMAX(PP_PCHECK_FOR_UPDATES_FREQUENCY, unsigned int, _T("CHConfig.General.Program.Updates.Frequency"), eFreq_Weekly, eFreq_Never, eFreq_Max - 1); PROPERTY_MINMAX(PP_PUPDATECHANNEL, int, _T("CHConfig.General.Program.Updates.UpdateChannel"), UpdateVersionInfo::eReleaseCandidate, UpdateVersionInfo::eStable, UpdateVersionInfo::eMax - 1); Index: src/ch/MainWnd.cpp =================================================================== diff -u -r3d5880c6661c3ed500e0c1c739a923ae9ede0364 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision 3d5880c6661c3ed500e0c1c739a923ae9ede0364) +++ src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -685,8 +685,6 @@ case WM_CONFIGNOTIFY: { - GetApp().SetAutorun(GetPropValue(GetConfig())); - // set this process class HANDLE hProcess=GetCurrentProcess(); ::SetPriorityClass(hProcess, (DWORD)GetPropValue(GetConfig())); Index: src/ch/OptionsDlg.cpp =================================================================== diff -u -r0d5b67ee96b435d63f7bf075dc8e28603793b187 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/OptionsDlg.cpp (.../OptionsDlg.cpp) (revision 0d5b67ee96b435d63f7bf075dc8e28603793b187) +++ src/ch/OptionsDlg.cpp (.../OptionsDlg.cpp) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -41,7 +41,8 @@ COptionsDlg::COptionsDlg(CWnd* pParent /*=nullptr*/) :ictranslate::CLanguageDialog(IDD_OPTIONS_DIALOG, pParent, &m_bLock), - m_spLog(logger::MakeLogger(GetLogFileData(), L"OptionsDlg")) + m_spLog(logger::MakeLogger(GetLogFileData(), L"OptionsDlg")), + m_autoRun(GetApp().GetAppName(), chcore::PathFromString(GetApp().GetFullProgramPath())) { } @@ -237,8 +238,9 @@ PROP_SEPARATOR(IDS_PROGRAM_STRING); PROP_BOOL(IDS_CLIPBOARDMONITORING_STRING, GetPropValue(GetConfig())); PROP_UINT(IDS_CLIPBOARDINTERVAL_STRING, GetPropValue(GetConfig())); - PROP_BOOL(IDS_AUTORUNPROGRAM_STRING, GetPropValue(GetConfig())); + PROP_BOOL(IDS_AUTORUNPROGRAM_STRING, IsAutorunEnabled()); + PROP_COMBO(IDS_CFG_CHECK_FOR_UPDATES_FREQUENCY, IDS_UPDATE_FREQUENCIES, GetPropValue(GetConfig())); PROP_COMBO(IDS_CFG_UPDATECHANNEL, IDS_CFGUPDATECHANNELITEMS_STRING, GetPropValue(GetConfig())); PROP_COMBO(IDS_CFG_USE_SECURE_CONNECTION, IDS_SECURE_CONNECTION_TYPES, GetPropValue(GetConfig())); @@ -370,7 +372,7 @@ SKIP_SEPARATOR(iPosition); SetPropValue(rConfig, GetBoolProp(iPosition++)); SetPropValue(rConfig, GetUintProp(iPosition++)); - SetPropValue(rConfig, GetBoolProp(iPosition++)); + EnableAutorun(GetBoolProp(iPosition++)); SetPropValue(rConfig, GetIndexProp(iPosition++)); SetPropValue(rConfig, GetBoolProp(iPosition++)); @@ -533,6 +535,33 @@ return strText; } +bool COptionsDlg::IsAutorunEnabled() const +{ + try + { + return m_autoRun.IsAutorunEnabled(); + } + catch (const std::exception& e) + { + LOG_ERROR(m_spLog) << L"Failed to determine if autorun is enabled. Assuming it is not. Error: " << e.what(); + return false; + } +} + +bool COptionsDlg::EnableAutorun(bool bEnable) +{ + try + { + m_autoRun.SetAutorun(bEnable); + return true; + } + catch (const std::exception& e) + { + LOG_ERROR(m_spLog) << L"Failed to change autorun to '" << bEnable << "'. Error: " << e.what(); + return false; + } +} + bool COptionsDlg::GetBoolProp(int iPosition) { int iSel = 0; Index: src/ch/OptionsDlg.h =================================================================== diff -u -r19b135b50b55b75d008526a799344bda9a62dc22 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/OptionsDlg.h (.../OptionsDlg.h) (revision 19b135b50b55b75d008526a799344bda9a62dc22) +++ src/ch/OptionsDlg.h (.../OptionsDlg.h) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -20,6 +20,7 @@ #define __OPTIONSDLG_H__ #include "PropertyListCtrl.h" +#include "AppAutorun.h" #define WM_CONFIGNOTIFY WM_USER+13 @@ -52,6 +53,9 @@ bool GetBoolProp(int iPosition); CString MakeCompoundString(UINT uiBase, int iCount, LPCTSTR lpszSeparator); + bool IsAutorunEnabled() const; + bool EnableAutorun(bool bEnable); + // Generated message map functions BOOL OnInitDialog() override; void OnOK() override; @@ -62,6 +66,7 @@ private: logger::TLoggerPtr m_spLog; + AppAutorun m_autoRun; static bool m_bLock; // locker std::vector m_cvRecent; Index: src/ch/TCommandLineParser.cpp =================================================================== diff -u -rb165add706c4fab9d783f0564b1dd398492da491 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/TCommandLineParser.cpp (.../TCommandLineParser.cpp) (revision b165add706c4fab9d783f0564b1dd398492da491) +++ src/ch/TCommandLineParser.cpp (.../TCommandLineParser.cpp) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -43,8 +43,7 @@ po::options_description desc(""); desc.add_options() - ("ImportTaskDefinition", po::wvalue< std::vector >(), "") - ("EnableLaunchAtStartup", po::wvalue(), ""); + ("ImportTaskDefinition", po::wvalue< std::vector >(), ""); po::variables_map vm; po::store(po::wcommand_line_parser(args).options(desc).run(), m_mapVariables); @@ -74,14 +73,3 @@ } } } - -bool TCommandLineParser::HasSystemStartupOption() const -{ - return m_mapVariables.count("EnableLaunchAtStartup") == 1; -} - -bool TCommandLineParser::IsSystemStartupEnabled() const -{ - bool bValue = m_mapVariables["EnableLaunchAtStartup"].as(); - return bValue; -} Index: src/ch/TCommandLineParser.h =================================================================== diff -u -r694e362bf3ce049a7bbba8b4cf442d07e6741901 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/TCommandLineParser.h (.../TCommandLineParser.h) (revision 694e362bf3ce049a7bbba8b4cf442d07e6741901) +++ src/ch/TCommandLineParser.h (.../TCommandLineParser.h) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -43,10 +43,6 @@ bool HasTaskDefinitionPath() const; void GetTaskDefinitionPaths(chcore::TPathContainer& vPaths) const; - // enable run at system startup - bool HasSystemStartupOption() const; - bool IsSystemStartupEnabled() const; - private: boost::program_options::variables_map m_mapVariables; }; Index: src/ch/ch.cpp =================================================================== diff -u -r3d5880c6661c3ed500e0c1c739a923ae9ede0364 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/ch.cpp (.../ch.cpp) (revision 3d5880c6661c3ed500e0c1c739a923ae9ede0364) +++ src/ch/ch.cpp (.../ch.cpp) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -325,17 +325,6 @@ EnableHtmlHelp(); - // ================================= Handle early command line options ======================================== - if(m_cmdLineParser.HasCommandLineParams() && m_cmdLineParser.HasSystemStartupOption()) - { - SetPropValue(rCfg, m_cmdLineParser.IsSystemStartupEnabled()); - rCfg.Write(); - - SetAutorun(GetPropValue(rCfg)); - - return FALSE; - } - // ================================= Checking for running instances of CH ======================================== // check instance - return false if it's the second one LOG_INFO(m_spLog) << _T("Checking for other running instances of Copy Handler"); Index: src/ch/ch.vc140.vcxproj =================================================================== diff -u -rf9a0580b36061d769a3c997893da2c925c8692b1 -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/ch.vc140.vcxproj (.../ch.vc140.vcxproj) (revision f9a0580b36061d769a3c997893da2c925c8692b1) +++ src/ch/ch.vc140.vcxproj (.../ch.vc140.vcxproj) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -522,6 +522,7 @@ + @@ -776,6 +777,7 @@ + Index: src/ch/ch.vc140.vcxproj.filters =================================================================== diff -u -r6609ba39811176f4803f0556db3da30e9e457b9d -r58de8d7360813537b384eff808c031f9e63db4de --- src/ch/ch.vc140.vcxproj.filters (.../ch.vc140.vcxproj.filters) (revision 6609ba39811176f4803f0556db3da30e9e457b9d) +++ src/ch/ch.vc140.vcxproj.filters (.../ch.vc140.vcxproj.filters) (revision 58de8d7360813537b384eff808c031f9e63db4de) @@ -257,6 +257,9 @@ Source Files\Shared + + Source Files\Tools + @@ -436,6 +439,9 @@ Source Files\Shared + + Source Files\Tools +