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