Skip to content

Commit 7b9b222

Browse files
committed
Added : option to run app at Windows startup
1 parent 2516e81 commit 7b9b222

File tree

7 files changed

+119
-5
lines changed

7 files changed

+119
-5
lines changed

TegraRcmGUI/DialogTab03.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ BOOL DialogTab03::OnInitDialog()
7272
checkbox->SetCheck(BST_CHECKED);
7373
}
7474

75+
HKEY hKey;
76+
const std::string key = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
77+
const std::string subkey = "TegraRcmGUI";
78+
79+
// Open Run Registry location
80+
LONG lnRes = RegOpenKeyExA(HKEY_CURRENT_USER,
81+
key.c_str(), 0, KEY_READ, &hKey);
82+
83+
if (ERROR_SUCCESS == lnRes)
84+
{
85+
lnRes = RegQueryValueExA(hKey, subkey.c_str(), NULL, NULL, NULL, NULL);
86+
if (lnRes != ERROR_FILE_NOT_FOUND)
87+
{
88+
CMFCButton*checkbox = (CMFCButton*)GetDlgItem(RUN_WINSTART);
89+
checkbox->SetCheck(BST_CHECKED);
90+
}
91+
}
92+
93+
7594
return TRUE; // return TRUE unless you set the focus to a control
7695
// EXCEPTION: OCX Property Pages should return FALSE
7796
}
@@ -80,6 +99,7 @@ BEGIN_MESSAGE_MAP(DialogTab03, CDialogEx)
8099
ON_BN_CLICKED(AUTO_INJECT, &DialogTab03::OnClickedAutoInject)
81100
ON_BN_CLICKED(MIN_TO_TRAY, &DialogTab03::OnClickedMinToTray)
82101
ON_BN_CLICKED(ID_INSTALL_DRIVER, &DialogTab03::OnBnClickedInstallDriver)
102+
ON_BN_CLICKED(RUN_WINSTART, &DialogTab03::OnBnClickedWinstart)
83103
END_MESSAGE_MAP()
84104

85105

@@ -131,3 +151,63 @@ void DialogTab03::OnBnClickedInstallDriver()
131151
m_TegraRcm->InstallDriver();
132152
}
133153

154+
155+
156+
void DialogTab03::OnBnClickedWinstart()
157+
{
158+
// Init
159+
HKEY hKey;
160+
const std::string key = "TegraRcmGUI";
161+
std::vector<char> buffer;
162+
163+
// Get checkbox value (checked, unchecked)
164+
CButton *m_ctlCheck = (CButton*)GetDlgItem(RUN_WINSTART);
165+
BOOL IsCheckChecked = (m_ctlCheck->GetCheck() == 1) ? true : false;
166+
167+
// Get application absolute path
168+
TCHAR szPath[_MAX_PATH];
169+
VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH));
170+
// Convert path to ANSI string
171+
int size = WideCharToMultiByte(CP_UTF8, 0, szPath, -1, NULL, 0, NULL, NULL);
172+
if (size > 0) {
173+
buffer.resize(size);
174+
WideCharToMultiByte(CP_UTF8, 0, szPath, -1, (LPSTR)(&buffer[0]), buffer.size(), NULL, NULL);
175+
}
176+
std::string appPath(&buffer[0]);
177+
std::string keyValue;
178+
keyValue.append("\"");
179+
keyValue.append(appPath);
180+
keyValue.append("\" /autostart");
181+
182+
// Open Run Registry location
183+
LONG lnRes = RegOpenKeyEx(HKEY_CURRENT_USER,
184+
_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),
185+
0L, KEY_WRITE,
186+
&hKey);
187+
188+
if (ERROR_SUCCESS == lnRes)
189+
{
190+
if (IsCheckChecked)
191+
{
192+
// Set full application path with a keyname to registry
193+
lnRes = RegSetValueExA(hKey,
194+
key.c_str(),
195+
0,
196+
REG_SZ,
197+
(LPBYTE)(keyValue.c_str()),
198+
keyValue.size() + 1);
199+
}
200+
else
201+
{
202+
lnRes = RegDeleteValueA(hKey, key.c_str());
203+
}
204+
if (ERROR_SUCCESS != lnRes)
205+
{
206+
AfxMessageBox(_T("Failed to set/unset at startup"));
207+
}
208+
}
209+
else
210+
{
211+
AfxMessageBox(_T("Failed to access registry"));
212+
}
213+
}

TegraRcmGUI/DialogTab03.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ class DialogTab03 : public CDialogEx
3030
afx_msg void OnClickedAutoInject();
3131
afx_msg void OnClickedMinToTray();
3232
afx_msg void OnBnClickedInstallDriver();
33+
afx_msg void OnBnClickedWinstart();
34+
3335
};

TegraRcmGUI/TegraRcmGUI.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ BOOL CTegraRcmGUIApp::InitInstance()
7878
InitCommonControlsEx(&InitCtrls);
7979

8080
CWinApp::InitInstance();
81-
81+
82+
// Get command line args
83+
CCustomCommandLineInfo oInfo;
84+
ParseCommandLine(oInfo);
8285

8386
AfxEnableControlContainer();
8487

@@ -93,12 +96,14 @@ BOOL CTegraRcmGUIApp::InitInstance()
9396
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
9497

9598
CTegraRcmGUIDlg dlg;
99+
if (oInfo.IsAutostart()) {
100+
dlg.AUTOSTART = TRUE;
101+
}
96102
m_pMainWnd = &dlg;
97103
INT_PTR nResponse = dlg.DoModal();
98104
if (nResponse == IDOK)
99105
{
100-
// TODO: Place code here to handle when the dialog is
101-
// dismissed with OK
106+
// TODO
102107
}
103108
else if (nResponse == IDCANCEL)
104109
{
@@ -121,5 +126,4 @@ int CTegraRcmGUIApp::Run()
121126
{
122127
int rc = 0;
123128
return 0;
124-
}
125-
129+
}

TegraRcmGUI/TegraRcmGUI.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,24 @@ class CTegraRcmGUIApp : public CWinApp
3030
DECLARE_MESSAGE_MAP()
3131
};
3232

33+
class CCustomCommandLineInfo : public CCommandLineInfo
34+
{
35+
public:
36+
CCustomCommandLineInfo()
37+
{
38+
m_bAutostart = FALSE;
39+
}
40+
BOOL m_bAutostart;
41+
public:
42+
BOOL IsAutostart() { return m_bAutostart; };
43+
44+
virtual void ParseParam(LPCTSTR pszParam, BOOL bFlag, BOOL bLast)
45+
{
46+
if (0 == wcscmp(pszParam, L"autostart"))
47+
{
48+
m_bAutostart = TRUE;
49+
}
50+
}
51+
};
52+
3353
extern CTegraRcmGUIApp theApp;

TegraRcmGUI/TegraRcmGUI.rc

234 Bytes
Binary file not shown.

TegraRcmGUI/TegraRcmGUIDlg.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ void CTegraRcmGUIDlg::OnTimer(UINT nIDEvent)
386386
{
387387
m_TegraRcm->LookUp();
388388
}
389+
390+
// After first initDialog, hide window if min2tray option is enabled
391+
if (AUTOSTART)
392+
{
393+
if (m_TegraRcm->MIN_TO_TRAY_CURR) HideWindowCommand();
394+
AUTOSTART = FALSE;
395+
}
389396
}
390397

391398
void CTegraRcmGUIDlg::OnEnChangePath()

TegraRcmGUI/TegraRcmGUIDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class CTegraRcmGUIDlg :
5757
BOOL DELAY_AUTOINJECT = FALSE;
5858
BOOL ASK_FOR_DRIVER = FALSE;
5959
BOOL PAUSE_LKP_DEVICE = FALSE;
60+
BOOL AUTOSTART = FALSE;
6061
BOOL CmdShow;
6162
COLORREF LabelColor = RGB(0, 0, 0);
6263
COLORREF WhiteRGB = RGB(255, 255, 255);

0 commit comments

Comments
 (0)