From 7b9b222148c3c9f4bffdfda90f853c353d88d90f Mon Sep 17 00:00:00 2001 From: eliboa Date: Tue, 24 Jul 2018 20:38:30 +0200 Subject: [PATCH] Added : option to run app at Windows startup --- TegraRcmGUI/DialogTab03.cpp | 80 +++++++++++++++++++++++++++++++++ TegraRcmGUI/DialogTab03.h | 2 + TegraRcmGUI/TegraRcmGUI.cpp | 14 +++--- TegraRcmGUI/TegraRcmGUI.h | 20 +++++++++ TegraRcmGUI/TegraRcmGUI.rc | Bin 21738 -> 21972 bytes TegraRcmGUI/TegraRcmGUIDlg.cpp | 7 +++ TegraRcmGUI/TegraRcmGUIDlg.h | 1 + 7 files changed, 119 insertions(+), 5 deletions(-) diff --git a/TegraRcmGUI/DialogTab03.cpp b/TegraRcmGUI/DialogTab03.cpp index 801082d..f7c71ce 100644 --- a/TegraRcmGUI/DialogTab03.cpp +++ b/TegraRcmGUI/DialogTab03.cpp @@ -72,6 +72,25 @@ BOOL DialogTab03::OnInitDialog() checkbox->SetCheck(BST_CHECKED); } + HKEY hKey; + const std::string key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; + const std::string subkey = "TegraRcmGUI"; + + // Open Run Registry location + LONG lnRes = RegOpenKeyExA(HKEY_CURRENT_USER, + key.c_str(), 0, KEY_READ, &hKey); + + if (ERROR_SUCCESS == lnRes) + { + lnRes = RegQueryValueExA(hKey, subkey.c_str(), NULL, NULL, NULL, NULL); + if (lnRes != ERROR_FILE_NOT_FOUND) + { + CMFCButton*checkbox = (CMFCButton*)GetDlgItem(RUN_WINSTART); + checkbox->SetCheck(BST_CHECKED); + } + } + + return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } @@ -80,6 +99,7 @@ BEGIN_MESSAGE_MAP(DialogTab03, CDialogEx) ON_BN_CLICKED(AUTO_INJECT, &DialogTab03::OnClickedAutoInject) ON_BN_CLICKED(MIN_TO_TRAY, &DialogTab03::OnClickedMinToTray) ON_BN_CLICKED(ID_INSTALL_DRIVER, &DialogTab03::OnBnClickedInstallDriver) + ON_BN_CLICKED(RUN_WINSTART, &DialogTab03::OnBnClickedWinstart) END_MESSAGE_MAP() @@ -131,3 +151,63 @@ void DialogTab03::OnBnClickedInstallDriver() m_TegraRcm->InstallDriver(); } + + +void DialogTab03::OnBnClickedWinstart() +{ + // Init + HKEY hKey; + const std::string key = "TegraRcmGUI"; + std::vector buffer; + + // Get checkbox value (checked, unchecked) + CButton *m_ctlCheck = (CButton*)GetDlgItem(RUN_WINSTART); + BOOL IsCheckChecked = (m_ctlCheck->GetCheck() == 1) ? true : false; + + // Get application absolute path + TCHAR szPath[_MAX_PATH]; + VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH)); + // Convert path to ANSI string + int size = WideCharToMultiByte(CP_UTF8, 0, szPath, -1, NULL, 0, NULL, NULL); + if (size > 0) { + buffer.resize(size); + WideCharToMultiByte(CP_UTF8, 0, szPath, -1, (LPSTR)(&buffer[0]), buffer.size(), NULL, NULL); + } + std::string appPath(&buffer[0]); + std::string keyValue; + keyValue.append("\""); + keyValue.append(appPath); + keyValue.append("\" /autostart"); + + // Open Run Registry location + LONG lnRes = RegOpenKeyEx(HKEY_CURRENT_USER, + _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), + 0L, KEY_WRITE, + &hKey); + + if (ERROR_SUCCESS == lnRes) + { + if (IsCheckChecked) + { + // Set full application path with a keyname to registry + lnRes = RegSetValueExA(hKey, + key.c_str(), + 0, + REG_SZ, + (LPBYTE)(keyValue.c_str()), + keyValue.size() + 1); + } + else + { + lnRes = RegDeleteValueA(hKey, key.c_str()); + } + if (ERROR_SUCCESS != lnRes) + { + AfxMessageBox(_T("Failed to set/unset at startup")); + } + } + else + { + AfxMessageBox(_T("Failed to access registry")); + } +} diff --git a/TegraRcmGUI/DialogTab03.h b/TegraRcmGUI/DialogTab03.h index 067efec..67390cb 100644 --- a/TegraRcmGUI/DialogTab03.h +++ b/TegraRcmGUI/DialogTab03.h @@ -30,4 +30,6 @@ public: afx_msg void OnClickedAutoInject(); afx_msg void OnClickedMinToTray(); afx_msg void OnBnClickedInstallDriver(); + afx_msg void OnBnClickedWinstart(); + }; diff --git a/TegraRcmGUI/TegraRcmGUI.cpp b/TegraRcmGUI/TegraRcmGUI.cpp index ba7c014..ddcd3b3 100644 --- a/TegraRcmGUI/TegraRcmGUI.cpp +++ b/TegraRcmGUI/TegraRcmGUI.cpp @@ -78,7 +78,10 @@ BOOL CTegraRcmGUIApp::InitInstance() InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); - + + // Get command line args + CCustomCommandLineInfo oInfo; + ParseCommandLine(oInfo); AfxEnableControlContainer(); @@ -93,12 +96,14 @@ BOOL CTegraRcmGUIApp::InitInstance() SetRegistryKey(_T("Local AppWizard-Generated Applications")); CTegraRcmGUIDlg dlg; + if (oInfo.IsAutostart()) { + dlg.AUTOSTART = TRUE; + } m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { - // TODO: Place code here to handle when the dialog is - // dismissed with OK + // TODO } else if (nResponse == IDCANCEL) { @@ -121,5 +126,4 @@ int CTegraRcmGUIApp::Run() { int rc = 0; return 0; -} - +} \ No newline at end of file diff --git a/TegraRcmGUI/TegraRcmGUI.h b/TegraRcmGUI/TegraRcmGUI.h index 36411c3..81cdf33 100644 --- a/TegraRcmGUI/TegraRcmGUI.h +++ b/TegraRcmGUI/TegraRcmGUI.h @@ -30,4 +30,24 @@ public: DECLARE_MESSAGE_MAP() }; +class CCustomCommandLineInfo : public CCommandLineInfo +{ +public: + CCustomCommandLineInfo() + { + m_bAutostart = FALSE; + } + BOOL m_bAutostart; +public: + BOOL IsAutostart() { return m_bAutostart; }; + + virtual void ParseParam(LPCTSTR pszParam, BOOL bFlag, BOOL bLast) + { + if (0 == wcscmp(pszParam, L"autostart")) + { + m_bAutostart = TRUE; + } + } +}; + extern CTegraRcmGUIApp theApp; \ No newline at end of file diff --git a/TegraRcmGUI/TegraRcmGUI.rc b/TegraRcmGUI/TegraRcmGUI.rc index d9e0589ffa1d71d84db0ef1065e78a975f7b0b3e..b44164ef32440cba6414bfd4afb65d1ad9848da0 100644 GIT binary patch delta 188 zcmaF0lJUxF#tlh2j2e>@brcyjCpYTkGaE7(PyVcPp4X5;hryh|07!yF0@d{=|I_v2 zhlrRln1aR4Co}3A`UWwSGUPERFeEY*03nDj0g~YinLyDLhJ1!{hGHPU7$};^Py{4P zfvS{%`hyrk8T=UHfhs+LbTC5*gCj%GVDk;3_M1$eXnJ(Af%u%sJVp|m JFX&6D0ss&%DSiL| delta 58 zcmV-A0LA~*s{!h%0kC2$lVmI)lZGsA12OLookUp(); } + + // After first initDialog, hide window if min2tray option is enabled + if (AUTOSTART) + { + if (m_TegraRcm->MIN_TO_TRAY_CURR) HideWindowCommand(); + AUTOSTART = FALSE; + } } void CTegraRcmGUIDlg::OnEnChangePath() diff --git a/TegraRcmGUI/TegraRcmGUIDlg.h b/TegraRcmGUI/TegraRcmGUIDlg.h index eb6b1db..b4c35e5 100644 --- a/TegraRcmGUI/TegraRcmGUIDlg.h +++ b/TegraRcmGUI/TegraRcmGUIDlg.h @@ -57,6 +57,7 @@ public: BOOL DELAY_AUTOINJECT = FALSE; BOOL ASK_FOR_DRIVER = FALSE; BOOL PAUSE_LKP_DEVICE = FALSE; + BOOL AUTOSTART = FALSE; BOOL CmdShow; COLORREF LabelColor = RGB(0, 0, 0); COLORREF WhiteRGB = RGB(255, 255, 255);