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):
29 * Startup (before the user logs in)
30 * - Services (NT, ?semi-synchronous?, not implemented yet)
31 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
32 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
35 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
36 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
37 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
38 * - Startup folders (all, ?asynch?, no imp)
39 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
41 * Somewhere in there is processing the RunOnceEx entries (also no imp)
46 EXTERN_C HRESULT WINAPI
SHCreateSessionKey(REGSAM samDesired
, PHKEY phKey
);
48 #define INVALID_RUNCMD_RETURN -1
50 * This function runs the specified command in the specified dir.
51 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
52 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
53 * [in] wait - whether to wait for the run program to finish before returning.
54 * [in] minimized - Whether to ask the program to run minimized.
57 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
58 * If wait is FALSE - returns 0 if successful.
59 * If wait is TRUE - returns the program's return value.
61 static int runCmd(LPWSTR cmdline
, LPCWSTR dir
, BOOL wait
, BOOL minimized
)
64 PROCESS_INFORMATION info
;
66 WCHAR szCmdLineExp
[MAX_PATH
+1] = L
"\0";
68 ExpandEnvironmentStringsW(cmdline
, szCmdLineExp
, sizeof(szCmdLineExp
) / sizeof(WCHAR
));
70 memset(&si
, 0, sizeof(si
));
74 si
.dwFlags
= STARTF_USESHOWWINDOW
;
75 si
.wShowWindow
= SW_MINIMIZE
;
77 memset(&info
, 0, sizeof(info
));
79 if (!CreateProcessW(NULL
, szCmdLineExp
, NULL
, NULL
, FALSE
, 0, NULL
, dir
, &si
, &info
))
81 printf("Failed to run command (%ld)\n", GetLastError());
83 return INVALID_RUNCMD_RETURN
;
86 printf("Successfully ran command\n");
89 { /* wait for the process to exit */
90 WaitForSingleObject(info
.hProcess
, INFINITE
);
91 GetExitCodeProcess(info
.hProcess
, &exit_code
);
94 CloseHandle(info
.hThread
);
95 CloseHandle(info
.hProcess
);
102 * Process a "Run" type registry key.
103 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
105 * szKeyName is the key holding the actual entries.
106 * bDelete tells whether we should delete each value right before executing it.
107 * bSynchronous tells whether we should wait for the prog to complete before
108 * going on to the next prog.
110 static BOOL
ProcessRunKeys(HKEY hkRoot
, LPCWSTR szKeyName
, BOOL bDelete
,
113 static const WCHAR WINKEY_NAME
[]={'S','o','f','t','w','a','r','e','\\',
114 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
115 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
116 HKEY hkWin
= NULL
, hkRun
=NULL
;
117 LONG res
= ERROR_SUCCESS
;
118 DWORD i
, nMaxCmdLine
= 0, nMaxValue
= 0;
119 WCHAR
*szCmdLine
= NULL
;
120 WCHAR
*szValue
= NULL
;
122 if (hkRoot
== HKEY_LOCAL_MACHINE
)
123 wprintf(L
"processing %s entries under HKLM\n", szKeyName
);
125 wprintf(L
"processing %s entries under HKCU\n", szKeyName
);
127 if ((res
= RegOpenKeyExW(hkRoot
, WINKEY_NAME
, 0, KEY_READ
, &hkWin
)) != ERROR_SUCCESS
)
129 printf("RegOpenKey failed on Software\\Microsoft\\Windows\\CurrentVersion (%ld)\n",
135 if ((res
= RegOpenKeyExW(hkWin
, szKeyName
, 0, bDelete
?KEY_ALL_ACCESS
:KEY_READ
, &hkRun
))!=
138 if (res
== ERROR_FILE_NOT_FOUND
)
140 printf("Key doesn't exist - nothing to be done\n");
145 printf("RegOpenKey failed on run key (%ld)\n", res
);
150 if ((res
= RegQueryInfoKeyW(hkRun
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &i
, &nMaxValue
,
151 &nMaxCmdLine
, NULL
, NULL
)) != ERROR_SUCCESS
)
153 printf("Couldn't query key info (%ld)\n", res
);
160 printf("No commands to execute.\n");
166 if ((szCmdLine
= malloc(nMaxCmdLine
)) == NULL
)
168 printf("Couldn't allocate memory for the commands to be executed\n");
170 res
= ERROR_NOT_ENOUGH_MEMORY
;
174 if ((szValue
= malloc((++nMaxValue
)*sizeof(*szValue
))) == NULL
)
176 printf("Couldn't allocate memory for the value names\n");
179 res
= ERROR_NOT_ENOUGH_MEMORY
;
185 DWORD nValLength
=nMaxValue
, nDataLength
=nMaxCmdLine
;
190 if ((res
= RegEnumValueW(hkRun
, i
, szValue
, &nValLength
, 0, &type
,
191 (LPBYTE
)szCmdLine
, &nDataLength
)) != ERROR_SUCCESS
)
193 printf("Couldn't read in value %ld - %ld\n", i
, res
);
198 /* safe mode - force to run if prefixed with asterisk */
199 if (GetSystemMetrics(SM_CLEANBOOT
) && (szValue
[0] != L
'*')) continue;
201 if (bDelete
&& (res
= RegDeleteValueW(hkRun
, szValue
)) != ERROR_SUCCESS
)
203 printf("Couldn't delete value - %ld, %ld. Running command anyways.\n", i
, res
);
208 printf("Incorrect type of value #%ld (%ld)\n", i
, type
);
213 if ((res
= runCmd(szCmdLine
, NULL
, bSynchronous
, FALSE
)) == INVALID_RUNCMD_RETURN
)
215 printf("Error running cmd #%ld (%ld)\n", i
, GetLastError());
218 printf("Done processing cmd #%ld\n", i
);
233 return res
== ERROR_SUCCESS
? TRUE
: FALSE
;
238 ProcessStartupItems(VOID
)
240 BOOL bNormalBoot
= GetSystemMetrics(SM_CLEANBOOT
) == 0; /* Perform the operations that are performed every boot */
241 /* First, set the current directory to SystemRoot */
242 TCHAR gen_path
[MAX_PATH
];
244 HKEY hSessionKey
, hKey
;
247 res
= GetWindowsDirectory(gen_path
, sizeof(gen_path
));
251 printf("Couldn't get the windows directory - error %ld\n",
257 if (!SetCurrentDirectory(gen_path
))
259 wprintf(L
"Cannot set the dir to %s (%ld)\n", gen_path
, GetLastError());
264 hr
= SHCreateSessionKey(KEY_WRITE
, &hSessionKey
);
270 Error
= RegCreateKeyEx(hSessionKey
, L
"StartupHasBeenRun", 0, NULL
, REG_OPTION_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, &dwDisp
);
271 RegCloseKey(hSessionKey
);
272 if (Error
== ERROR_SUCCESS
)
275 if (dwDisp
== REG_OPENED_EXISTING_KEY
)
277 /* Startup programs has already been run */
283 /* Perform the operations by order checking if policy allows it, checking if this is not Safe Mode,
284 * stopping if one fails, skipping if necessary.
287 if (res
&& (SHRestricted(REST_NOLOCALMACHINERUNONCE
) == 0))
288 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"RunOnce", TRUE
, TRUE
);
290 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOLOCALMACHINERUN
) == 0))
291 res
= ProcessRunKeys(HKEY_LOCAL_MACHINE
, L
"Run", FALSE
, FALSE
);
293 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
294 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"Run", FALSE
, FALSE
);
296 if (res
&& bNormalBoot
&& (SHRestricted(REST_NOCURRENTUSERRUNONCE
) == 0))
297 res
= ProcessRunKeys(HKEY_CURRENT_USER
, L
"RunOnce", TRUE
, FALSE
);
299 printf("Operation done\n");
301 return res
? 0 : 101;