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 EXTERN_C HRESULT WINAPI
SHCreateSessionKey(REGSAM samDesired
, PHKEY phKey
);
45 #define INVALID_RUNCMD_RETURN -1
47 * This function runs the specified command in the specified dir.
48 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
49 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
50 * [in] wait - whether to wait for the run program to finish before returning.
51 * [in] minimized - Whether to ask the program to run minimized.
54 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
55 * If wait is FALSE - returns 0 if successful.
56 * If wait is TRUE - returns the program's return value.
58 static int runCmd(LPWSTR cmdline
, LPCWSTR dir
, BOOL wait
, BOOL minimized
)
61 PROCESS_INFORMATION info
;
63 WCHAR szCmdLineExp
[MAX_PATH
+1] = L
"\0";
65 ExpandEnvironmentStringsW(cmdline
, szCmdLineExp
, sizeof(szCmdLineExp
) / sizeof(WCHAR
));
67 memset(&si
, 0, sizeof(si
));
71 si
.dwFlags
= STARTF_USESHOWWINDOW
;
72 si
.wShowWindow
= SW_MINIMIZE
;
74 memset(&info
, 0, sizeof(info
));
76 if (!CreateProcessW(NULL
, szCmdLineExp
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
))
78 TRACE("Failed to run command (%lu)\n", GetLastError());
80 return INVALID_RUNCMD_RETURN
;
83 TRACE("Successfully ran command\n");
86 { /* wait for the process to exit */
87 WaitForSingleObject(info
.hProcess
, INFINITE
);
88 GetExitCodeProcess(info
.hProcess
, &exit_code
);
91 CloseHandle(info
.hThread
);
92 CloseHandle(info
.hProcess
);
99 * Process a "Run" type registry key.
100 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
102 * szKeyName is the key holding the actual entries.
103 * bDelete tells whether we should delete each value right before executing it.
104 * bSynchronous tells whether we should wait for the prog to complete before
105 * going on to the next prog.
107 static BOOL
ProcessRunKeys(HKEY hkRoot
, LPCWSTR szKeyName
, BOOL bDelete
,
110 HKEY hkWin
= NULL
, hkRun
= NULL
;
111 LONG res
= ERROR_SUCCESS
;
112 DWORD i
, cbMaxCmdLine
= 0, cchMaxValue
= 0;
113 WCHAR
*szCmdLine
= NULL
;
114 WCHAR
*szValue
= NULL
;
116 if (hkRoot
== HKEY_LOCAL_MACHINE
)
117 TRACE("processing %ls entries under HKLM\n", szKeyName
);
119 TRACE("processing %ls entries under HKCU\n", szKeyName
);
121 res
= RegOpenKeyExW(hkRoot
,
122 L
"Software\\Microsoft\\Windows\\CurrentVersion",
126 if (res
!= ERROR_SUCCESS
)
128 TRACE("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n", res
);
133 res
= RegOpenKeyExW(hkWin
,
136 bDelete
? KEY_ALL_ACCESS
: KEY_READ
,
138 if (res
!= ERROR_SUCCESS
)
140 if (res
== ERROR_FILE_NOT_FOUND
)
142 TRACE("Key doesn't exist - nothing to be done\n");
147 TRACE("RegOpenKeyEx failed on run key (%ld)\n", res
);
152 res
= RegQueryInfoKeyW(hkRun
,
164 if (res
!= ERROR_SUCCESS
)
166 TRACE("Couldn't query key info (%ld)\n", res
);
173 TRACE("No commands to execute.\n");
179 szCmdLine
= (WCHAR
*)HeapAlloc(hProcessHeap
,
182 if (szCmdLine
== NULL
)
184 TRACE("Couldn't allocate memory for the commands to be executed\n");
186 res
= ERROR_NOT_ENOUGH_MEMORY
;
191 szValue
= (WCHAR
*)HeapAlloc(hProcessHeap
,
193 cchMaxValue
* sizeof(*szValue
));
196 TRACE("Couldn't allocate memory for the value names\n");
198 res
= ERROR_NOT_ENOUGH_MEMORY
;
204 DWORD cchValLength
= cchMaxValue
, cbDataLength
= cbMaxCmdLine
;
209 res
= RegEnumValueW(hkRun
,
217 if (res
!= ERROR_SUCCESS
)
219 TRACE("Couldn't read in value %lu - %ld\n", i
, res
);
224 /* safe mode - force to run if prefixed with asterisk */
225 if (GetSystemMetrics(SM_CLEANBOOT
) && (szValue
[0] != L
'*')) continue;
227 if (bDelete
&& (res
= RegDeleteValueW(hkRun
, szValue
)) != ERROR_SUCCESS
)
229 TRACE("Couldn't delete value - %lu, %ld. Running command anyways.\n", i
, res
);
234 TRACE("Incorrect type of value #%lu (%lu)\n", i
, type
);
239 res
= runCmd(szCmdLine
, NULL
, bSynchronous
, FALSE
);
240 if (res
== INVALID_RUNCMD_RETURN
)
242 TRACE("Error running cmd #%lu (%lu)\n", i
, GetLastError());
245 TRACE("Done processing cmd #%lu\n", i
);
251 HeapFree(hProcessHeap
, 0, szValue
);
252 if (szCmdLine
!= NULL
)
253 HeapFree(hProcessHeap
, 0, szCmdLine
);
261 return res
== ERROR_SUCCESS
? TRUE
: FALSE
;
266 ProcessStartupItems(VOID
)
268 /* TODO: ProcessRunKeys already checks SM_CLEANBOOT -- items prefixed with * should probably run even in safe mode */
269 BOOL bNormalBoot
= GetSystemMetrics(SM_CLEANBOOT
) == 0; /* Perform the operations that are performed every boot */
270 /* First, set the current directory to SystemRoot */
271 WCHAR gen_path
[MAX_PATH
];
273 HKEY hSessionKey
, hKey
;
276 res
= GetWindowsDirectoryW(gen_path
, sizeof(gen_path
) / sizeof(gen_path
[0]));
279 TRACE("Couldn't get the windows directory - error %lu\n", GetLastError());
284 if (!SetCurrentDirectoryW(gen_path
))
286 TRACE("Cannot set the dir to %ls (%lu)\n", gen_path
, GetLastError());
291 hr
= SHCreateSessionKey(KEY_WRITE
, &hSessionKey
);
297 Error
= RegCreateKeyExW(hSessionKey
, L
"StartupHasBeenRun", 0, NULL
, REG_OPTION_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, &dwDisp
);
298 RegCloseKey(hSessionKey
);
299 if (Error
== ERROR_SUCCESS
)
302 if (dwDisp
== REG_OPENED_EXISTING_KEY
)
304 /* Startup programs has already been run */
310 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
311 * stopping if one fails, skipping if necessary.
314 /* TODO: RunOnceEx */
316 if (res
&& (SHRestricted(REST_NOLOCALMACHINERUNONCE
) == 0))
317 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"RunOnce", TRUE
, TRUE
);
319 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOLOCALMACHINERUN
) == 0))
320 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"Run", FALSE
, FALSE
);
322 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
323 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"Run", FALSE
, FALSE
);
325 /* TODO: All users Startup folder */
327 /* TODO: Current user Startup folder */
329 /* TODO: HKCU\RunOnce runs even if StartupHasBeenRun exists */
330 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
331 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"RunOnce", TRUE
, FALSE
);
333 TRACE("Operation done\n");
335 return res
? 0 : 101;