diff --git a/nsis/installer.nsi b/nsis/installer.nsi index 6f12f2d9e..4c8b49c70 100644 --- a/nsis/installer.nsi +++ b/nsis/installer.nsi @@ -83,6 +83,10 @@ functionEnd section "install" # Files for the install directory - to build the installer, these should be in the same directory as the install script (this file) setOutPath $INSTDIR + + # Close parity if running + !insertmacro TerminateApp + # Files added here should be removed by the uninstaller (see section "uninstall") file /oname=parity.exe ..\target\release\parity.exe file /oname=ptray.exe ..\windows\ptray\x64\Release\ptray.exe @@ -99,6 +103,7 @@ section "install" # Start Menu createDirectory "$SMPROGRAMS\${COMPANYNAME}" + delete "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" createShortCut "$SMPROGRAMS\${COMPANYNAME}\${APPNAME} Ethereum.lnk" "$INSTDIR\ptray.exe" "ui" "$INSTDIR\logo.ico" createShortCut "$DESKTOP\${APPNAME} Ethereum.lnk" "$INSTDIR\ptray.exe" "ui" "$INSTDIR\logo.ico" @@ -115,7 +120,7 @@ section "install" SimpleFC::AdvAddRule "Parity UDP discovery (UDP:30303)" "" 17 2 1 2147483647 1 "$INSTDIR\parity.exe" "" "" "Parity" "" 30303 "" "" # Registry information for add/remove programs - WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" "${COMPANYNAME} - ${APPNAME} - ${DESCRIPTION}" + WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" "${APPNAME} - ${DESCRIPTION}" WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\"" WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S" WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation" "$\"$INSTDIR$\"" @@ -133,7 +138,8 @@ section "install" # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "EstimatedSize" ${INSTALLSIZE} - WriteRegStr HKEY_LOCAL_MACHINE "Software\Microsoft\Windows\CurrentVersion\Run" ${APPNAME} "$INSTDIR\ptray.exe ${ARGS}" + WriteRegStr HKEY_CURRENT_USER "Software\Microsoft\Windows\CurrentVersion\Run" ${APPNAME} "$INSTDIR\ptray.exe ${ARGS}" + DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "${APPNAME}" ExecShell "" "$INSTDIR\ptray.exe" "${FIRST_START_ARGS}" sectionEnd @@ -177,5 +183,6 @@ section "uninstall" # Remove uninstaller information from the registry DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "${APPNAME}" + DeleteRegValue HKCU "Software\Microsoft\Windows\CurrentVersion\Run" "${APPNAME}" sectionEnd diff --git a/windows/ptray/ptray.cpp b/windows/ptray/ptray.cpp index 81c9b8a16..981b7a1a2 100644 --- a/windows/ptray/ptray.cpp +++ b/windows/ptray/ptray.cpp @@ -33,6 +33,7 @@ #define MAX_LOADSTRING 100 #define IDM_EXIT 100 #define IDM_OPEN 101 +#define IDM_AUTOSTART 102 #define WM_USER_SHELLICON WM_USER + 1 HANDLE parityHandle = INVALID_HANDLE_VALUE; @@ -49,12 +50,14 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void KillParity(); void OpenUI(); bool ParityIsRunning(); +bool AutostartEnabled(); +void EnableAutostart(bool enable); bool GetParityExePath(TCHAR* dest, size_t destSize) { if (!dest || MAX_PATH > destSize) return false; - GetModuleFileName(NULL, dest, destSize); + GetModuleFileName(NULL, dest, (DWORD)destSize); if (!PathRemoveFileSpec(dest)) return false; return PathAppend(dest, _T("parity.exe")) == TRUE; @@ -201,7 +204,11 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) HMENU hPopMenu = CreatePopupMenu(); InsertMenu(hPopMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING, IDM_OPEN, _T("Open")); InsertMenu(hPopMenu, 0xFFFFFFFF, MF_SEPARATOR | MF_BYPOSITION, 0, nullptr); + InsertMenu(hPopMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING, IDM_AUTOSTART, _T("Start at Login")); + InsertMenu(hPopMenu, 0xFFFFFFFF, MF_SEPARATOR | MF_BYPOSITION, 0, nullptr); InsertMenu(hPopMenu, 0xFFFFFFFF, MF_BYPOSITION | MF_STRING, IDM_EXIT, _T("Exit")); + bool autoStart = AutostartEnabled(); + CheckMenuItem(hPopMenu, IDM_AUTOSTART, autoStart ? MF_CHECKED : autoStart); SetForegroundWindow(hWnd); TrackPopupMenu(hPopMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN, lpClickPoint.x, lpClickPoint.y, 0, hWnd, NULL); @@ -221,6 +228,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) case IDM_OPEN: OpenUI(); break; + case IDM_AUTOSTART: + { + bool autoStart = AutostartEnabled(); + EnableAutostart(!autoStart); + } + break; default: return DefWindowProc(hWnd, message, wParam, lParam); } @@ -276,3 +289,37 @@ void OpenUI() LPWSTR cmd = _T("parity.exe ui"); CreateProcess(path, cmd, nullptr, nullptr, false, CREATE_NO_WINDOW, nullptr, nullptr, &startupInfo, &procInfo); } + +bool AutostartEnabled() { + HKEY hKey; + LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ, &hKey); + if (lRes != ERROR_SUCCESS) + return false; + + WCHAR szBuffer[512]; + DWORD dwBufferSize = sizeof(szBuffer); + ULONG nError; + nError = RegQueryValueExW(hKey, L"Parity", 0, nullptr, (LPBYTE)szBuffer, &dwBufferSize); + if (ERROR_SUCCESS != nError) + return false; + return true; +} + +void EnableAutostart(bool enable) { + HKEY hKey; + LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey); + if (lRes != ERROR_SUCCESS) + return; + + if (enable) { + TCHAR path[MAX_PATH] = { 0 }; + if (!GetParityExePath(path, MAX_PATH)) + return; + RegSetValueEx(hKey, L"Parity", 0, REG_SZ, (LPBYTE)path, MAX_PATH); + } + else + { + RegDeleteValue(hKey, L"Parity"); + } +} + diff --git a/windows/ptray/ptray.vcxproj b/windows/ptray/ptray.vcxproj index 2d2fe87a8..e015d55c1 100644 --- a/windows/ptray/ptray.vcxproj +++ b/windows/ptray/ptray.vcxproj @@ -95,7 +95,7 @@ - Use + NotUsing Level3 Disabled _DEBUG;_WINDOWS;%(PreprocessorDefinitions)