xref: /reactos/base/system/runonce/runonce.c (revision 84ccccab)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS runonce.exe
4  * FILE:            base/system/runonce/runonce.c
5  * PROGRAMMER:      Dmitry Chapyshev (dmitry@reactos.org)
6  */
7 
8 #include "runonce.h"
9 
10 static
11 DWORD
12 WINAPI
13 StartApplication(LPVOID lpDlg)
14 {
15     HWND hList = GetDlgItem((HWND)lpDlg, IDC_COMP_LIST);
16     INT Index, Count = SendMessage(hList, LB_GETCOUNT, 0, 0);
17     PROCESS_INFORMATION pi;
18     STARTUPINFOW si;
19     TCHAR szData[MAX_PATH];
20 
21     for (Index = 0; Index < Count; Index++)
22     {
23         SendMessage(hList, LB_GETTEXT, Index, (LPARAM)szData);
24 
25         ZeroMemory(&si, sizeof(si));
26         si.cb = sizeof(si);
27         si.dwFlags = STARTF_USESHOWWINDOW;
28         si.wShowWindow = SW_SHOW;
29 
30         if (!CreateProcess(NULL, szData, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
31             continue;
32 
33         WaitForSingleObjectEx(pi.hProcess, INFINITE, TRUE);
34 
35         CloseHandle(pi.hProcess);
36         CloseHandle(pi.hThread);
37     }
38 
39     RegDeleteKey(HKEY_LOCAL_MACHINE,
40                  _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce\\Setup"));
41 
42     PostMessage((HWND)lpDlg, WM_CLOSE, 0, 0);
43 
44     return 0;
45 }
46 
47 static
48 VOID
49 InitDialog(HWND hDlg)
50 {
51     TCHAR szAppPath[MAX_PATH], szData[MAX_PATH];
52     DWORD dwIndex, dwSize, dwType, dwData, dwThreadId;
53     HKEY hKey;
54 
55     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
56                      _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce\\Setup"),
57                      0,
58                      KEY_QUERY_VALUE,
59                      &hKey) == ERROR_SUCCESS)
60     {
61         for (dwIndex = 0; ; dwIndex++)
62         {
63             dwSize = sizeof(szAppPath);
64             dwData = sizeof(szData) / sizeof(TCHAR);
65 
66             if (RegEnumValue(hKey,
67                              dwIndex,
68                              szAppPath,
69                              &dwSize,
70                              NULL,
71                              &dwType,
72                              (LPBYTE)szData,
73                              &dwData) == ERROR_SUCCESS)
74             {
75                 if (dwType != REG_SZ) continue;
76 
77                 SendMessage(GetDlgItem(hDlg, IDC_COMP_LIST), LB_ADDSTRING, 0, (LPARAM)szData);
78             }
79         }
80 
81         RegCloseKey(hKey);
82     }
83 
84     CloseHandle(CreateThread(NULL,
85                              0,
86                              StartApplication,
87                              (LPVOID)hDlg,
88                              0,
89                              &dwThreadId));
90 }
91 
92 static
93 INT_PTR
94 CALLBACK
95 RunOnceDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
96 {
97     switch (Msg)
98     {
99         case WM_INITDIALOG:
100             InitDialog(hDlg);
101             break;
102 
103         case WM_CLOSE:
104             EndDialog(hDlg, 0);
105             break;
106     }
107 
108     return 0;
109 }
110 
111 INT
112 WINAPI
113 _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, INT nCmdShow)
114 {
115     LPCTSTR lpCmd = GetCommandLine();
116     TCHAR szAppPath[MAX_PATH], szData[MAX_PATH];
117     DWORD dwIndex, dwSize, dwType, dwData;
118     PROCESS_INFORMATION pi;
119     STARTUPINFOW si;
120     BOOL bRunApps = FALSE;
121     HKEY hKey;
122 
123     while (*lpCmd)
124     {
125         while (*lpCmd && *lpCmd != _T('/') && *lpCmd != _T('-')) lpCmd++;
126         if (!*lpCmd) break;
127         if (*++lpCmd == _T('r')) bRunApps = TRUE;
128         lpCmd++;
129     }
130 
131     if (bRunApps)
132     {
133         if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
134                          _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"),
135                          0,
136                          KEY_QUERY_VALUE,
137                          &hKey) == ERROR_SUCCESS)
138         {
139             for (dwIndex = 0; ; dwIndex++)
140             {
141                 dwSize = sizeof(szAppPath);
142                 dwData = sizeof(szData) / sizeof(TCHAR);
143 
144                 if (RegEnumValue(hKey,
145                                  dwIndex,
146                                  szAppPath,
147                                  &dwSize,
148                                  NULL,
149                                  &dwType,
150                                  (LPBYTE)szData,
151                                  &dwData) == ERROR_SUCCESS)
152                 {
153                     RegDeleteValue(hKey, szAppPath);
154 
155                     if (dwType != REG_SZ) continue;
156 
157                     ZeroMemory(&si, sizeof(si));
158                     si.cb = sizeof(si);
159                     si.dwFlags = STARTF_USESHOWWINDOW;
160                     si.wShowWindow = SW_SHOW;
161 
162                     if (!CreateProcess(NULL, szData, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
163                         continue;
164 
165                     WaitForSingleObjectEx(pi.hProcess, INFINITE, TRUE);
166 
167                     CloseHandle(pi.hProcess);
168                     CloseHandle(pi.hThread);
169                 }
170             }
171 
172             RegCloseKey(hKey);
173         }
174 
175         return 1;
176     }
177 
178     DialogBox(hInst, MAKEINTRESOURCE(IDD_RUNONCE_DLG), NULL, RunOnceDlgProc);
179 
180     return 0;
181 }
182