Index: src/ch/AppHelper.cpp =================================================================== diff -u -r2457755b4084e3d1c80a8e7c77c9f0996312941b -r4213a6bbd2b543810d29c5a1964917903055d60c --- src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision 2457755b4084e3d1c80a8e7c77c9f0996312941b) +++ src/ch/AppHelper.cpp (.../AppHelper.cpp) (revision 4213a6bbd2b543810d29c5a1964917903055d60c) @@ -241,26 +241,66 @@ return true; } -void CAppHelper::SetAutorun(bool bState) +bool CAppHelper::SetAutorun(bool bEnable) { - // storing key in registry - HKEY hkeyRun; - if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hkeyRun) != ERROR_SUCCESS) - return; - - if (bState) + // check the current key value (to avoid irritating messages from some firewall software) + HKEY hkeyRun = NULL; + LSTATUS lStatus = ERROR_SUCCESS; + CString strValue; + CString strKey; + DWORD dwType = REG_SZ; + DWORD dwCount = _MAX_PATH * sizeof(TCHAR); + + 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, NULL, &dwType, (BYTE*)strValue.GetBufferSetLength(_MAX_PATH), &dwCount); + RegCloseKey(hkeyRun); + + if(lStatus != ERROR_SUCCESS && lStatus != ERROR_FILE_NOT_FOUND) { - TCHAR *pszPath=new TCHAR[_tcslen(m_pszProgramPath)+_tcslen(m_pszProgramName)+2]; - _tcscpy(pszPath, m_pszProgramPath); - _tcscat(pszPath, _T("\\")); - _tcscat(pszPath, m_pszProgramName); + strValue.ReleaseBuffer(0); + return false; + } + if(lStatus == ERROR_FILE_NOT_FOUND) + { + strValue.ReleaseBuffer(0); - RegSetValueEx(hkeyRun, m_pszAppName, 0, REG_SZ, (BYTE*)pszPath, (DWORD)(_tcslen(pszPath)+1)*sizeof(TCHAR)); + // if there is no key in registry and we don't want it, then return with ok status + if(!bEnable) + return true; - delete [] pszPath; + // format the data to be written to registry + strKey.Format(_T("%s\\%s"), m_pszProgramPath, m_pszProgramName); } else - RegDeleteValue(hkeyRun, m_pszAppName); + { + // key found + strValue.ReleaseBuffer(dwCount / sizeof(TCHAR)); + + if(bEnable) + { + // key exists in registry, check if the value is correct + strKey.Format(_T("%s\\%s"), m_pszProgramPath, 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 -r95e140c080a7424faba1bc9f35098e3cfd56d4e9 -r4213a6bbd2b543810d29c5a1964917903055d60c --- src/ch/AppHelper.h (.../AppHelper.h) (revision 95e140c080a7424faba1bc9f35098e3cfd56d4e9) +++ src/ch/AppHelper.h (.../AppHelper.h) (revision 4213a6bbd2b543810d29c5a1964917903055d60c) @@ -25,7 +25,7 @@ CAppHelper(); virtual ~CAppHelper(); - void SetAutorun(bool bState); // changes state of "run with system" option + bool SetAutorun(bool bState); // changes state of "run with system" option PTSTR ExpandPath(PTSTR pszString); // expands path string - ie. into c:\windows bool IsFirstInstance() const { return m_bFirstInstance; };