2 * Copyright (C) 2002 Andreas Mohr
3 * Copyright (C) 2002 Shachar Shemesh
4 * Copyright (C) 2013 Edijs Kolesnikovics
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Based on the Wine "bootup" handler application
23 * This app handles the various "hooks" windows allows for applications to perform
24 * as part of the bootstrap process. Theses are roughly devided into three types.
25 * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
26 * Also, 119941 has some info on grpconv.exe
27 * The operations performed are (by order of execution):
30 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceEx (synch, no imp)
31 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (synch)
32 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (asynch)
33 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (asynch)
34 * - All users Startup folder "%ALLUSERSPROFILE%\Start Menu\Programs\Startup" (asynch, no imp)
35 * - Current user Startup folder "%USERPROFILE%\Start Menu\Programs\Startup" (asynch, no imp)
36 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (asynch)
38 * None is processed in Safe Mode // FIXME: Check RunOnceEx in Safe Mode
43 #define INVALID_RUNCMD_RETURN -1
45 * This function runs the specified command in the specified dir.
46 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
47 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
48 * [in] wait - whether to wait for the run program to finish before returning.
49 * [in] minimized - Whether to ask the program to run minimized.
52 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
53 * If wait is FALSE - returns 0 if successful.
54 * If wait is TRUE - returns the program's return value.
56 static int runCmd(LPWSTR cmdline
, LPCWSTR dir
, BOOL wait
, BOOL minimized
)
59 PROCESS_INFORMATION info
;
61 WCHAR szCmdLineExp
[MAX_PATH
+1] = L
"\0";
63 ExpandEnvironmentStringsW(cmdline
, szCmdLineExp
, _countof(szCmdLineExp
));
65 memset(&si
, 0, sizeof(si
));
69 si
.dwFlags
= STARTF_USESHOWWINDOW
;
70 si
.wShowWindow
= SW_MINIMIZE
;
72 memset(&info
, 0, sizeof(info
));
74 if (!CreateProcessW(NULL
, szCmdLineExp
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
))
76 TRACE("Failed to run command (%lu)\n", GetLastError());
78 return INVALID_RUNCMD_RETURN
;
81 TRACE("Successfully ran command\n");
84 { /* wait for the process to exit */
85 WaitForSingleObject(info
.hProcess
, INFINITE
);
86 GetExitCodeProcess(info
.hProcess
, &exit_code
);
89 CloseHandle(info
.hThread
);
90 CloseHandle(info
.hProcess
);
97 * Process a "Run" type registry key.
98 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
100 * szKeyName is the key holding the actual entries.
101 * bDelete tells whether we should delete each value right before executing it.
102 * bSynchronous tells whether we should wait for the prog to complete before
103 * going on to the next prog.
105 static BOOL
ProcessRunKeys(HKEY hkRoot
, LPCWSTR szKeyName
, BOOL bDelete
,
108 HKEY hkWin
= NULL
, hkRun
= NULL
;
109 LONG res
= ERROR_SUCCESS
;
110 DWORD i
, cbMaxCmdLine
= 0, cchMaxValue
= 0;
111 WCHAR
*szCmdLine
= NULL
;
112 WCHAR
*szValue
= NULL
;
114 if (hkRoot
== HKEY_LOCAL_MACHINE
)
115 TRACE("processing %ls entries under HKLM\n", szKeyName
);
117 TRACE("processing %ls entries under HKCU\n", szKeyName
);
119 res
= RegOpenKeyExW(hkRoot
,
120 L
"Software\\Microsoft\\Windows\\CurrentVersion",
124 if (res
!= ERROR_SUCCESS
)
126 TRACE("RegOpenKeyW failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n", res
);
131 res
= RegOpenKeyExW(hkWin
,
134 bDelete
? KEY_ALL_ACCESS
: KEY_READ
,
136 if (res
!= ERROR_SUCCESS
)
138 if (res
== ERROR_FILE_NOT_FOUND
)
140 TRACE("Key doesn't exist - nothing to be done\n");
145 TRACE("RegOpenKeyExW failed on run key (%ld)\n", res
);
150 res
= RegQueryInfoKeyW(hkRun
,
162 if (res
!= ERROR_SUCCESS
)
164 TRACE("Couldn't query key info (%ld)\n", res
);
171 TRACE("No commands to execute.\n");
177 szCmdLine
= (WCHAR
*)HeapAlloc(hProcessHeap
,
180 if (szCmdLine
== NULL
)
182 TRACE("Couldn't allocate memory for the commands to be executed\n");
184 res
= ERROR_NOT_ENOUGH_MEMORY
;
189 szValue
= (WCHAR
*)HeapAlloc(hProcessHeap
,
191 cchMaxValue
* sizeof(*szValue
));
194 TRACE("Couldn't allocate memory for the value names\n");
196 res
= ERROR_NOT_ENOUGH_MEMORY
;
202 DWORD cchValLength
= cchMaxValue
, cbDataLength
= cbMaxCmdLine
;
207 res
= RegEnumValueW(hkRun
,
215 if (res
!= ERROR_SUCCESS
)
217 TRACE("Couldn't read in value %lu - %ld\n", i
, res
);
222 /* safe mode - force to run if prefixed with asterisk */
223 if (GetSystemMetrics(SM_CLEANBOOT
) && (szValue
[0] != L
'*')) continue;
225 if (bDelete
&& (res
= RegDeleteValueW(hkRun
, szValue
)) != ERROR_SUCCESS
)
227 TRACE("Couldn't delete value - %lu, %ld. Running command anyways.\n", i
, res
);
232 TRACE("Incorrect type of value #%lu (%lu)\n", i
, type
);
237 res
= runCmd(szCmdLine
, NULL
, bSynchronous
, FALSE
);
238 if (res
== INVALID_RUNCMD_RETURN
)
240 TRACE("Error running cmd #%lu (%lu)\n", i
, GetLastError());
243 TRACE("Done processing cmd #%lu\n", i
);
249 HeapFree(hProcessHeap
, 0, szValue
);
250 if (szCmdLine
!= NULL
)
251 HeapFree(hProcessHeap
, 0, szCmdLine
);
259 return res
== ERROR_SUCCESS
? TRUE
: FALSE
;
264 ProcessStartupItems(VOID
)
266 /* TODO: ProcessRunKeys already checks SM_CLEANBOOT -- items prefixed with * should probably run even in safe mode */
267 BOOL bNormalBoot
= GetSystemMetrics(SM_CLEANBOOT
) == 0; /* Perform the operations that are performed every boot */
268 /* First, set the current directory to SystemRoot */
269 WCHAR gen_path
[MAX_PATH
];
271 HKEY hSessionKey
, hKey
;
274 res
= GetWindowsDirectoryW(gen_path
, _countof(gen_path
));
277 TRACE("Couldn't get the windows directory - error %lu\n", GetLastError());
282 if (!SetCurrentDirectoryW(gen_path
))
284 TRACE("Cannot set the dir to %ls (%lu)\n", gen_path
, GetLastError());
289 hr
= SHCreateSessionKey(KEY_WRITE
, &hSessionKey
);
295 Error
= RegCreateKeyExW(hSessionKey
, L
"StartupHasBeenRun", 0, NULL
, REG_OPTION_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, &dwDisp
);
296 RegCloseKey(hSessionKey
);
297 if (Error
== ERROR_SUCCESS
)
300 if (dwDisp
== REG_OPENED_EXISTING_KEY
)
302 /* Startup programs has already been run */
308 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
309 * stopping if one fails, skipping if necessary.
312 /* TODO: RunOnceEx */
314 if (res
&& (SHRestricted(REST_NOLOCALMACHINERUNONCE
) == 0))
315 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"RunOnce", TRUE
, TRUE
);
317 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOLOCALMACHINERUN
) == 0))
318 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"Run", FALSE
, FALSE
);
320 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
321 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"Run", FALSE
, FALSE
);
323 /* TODO: All users Startup folder */
325 /* TODO: Current user Startup folder */
327 /* TODO: HKCU\RunOnce runs even if StartupHasBeenRun exists */
328 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
329 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"RunOnce", TRUE
, FALSE
);
331 TRACE("Operation done\n");
333 return res
? 0 : 101;