3 * Copyright (C) 2001, 2002 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS Userinit Logon Application
22 * FILE: subsys/system/userinit/userinit.c
23 * PROGRAMMERS: Thomas Weidenmueller (w3seek@users.sourceforge.net)
24 * Hervé Poussineau (hpoussin@reactos.org)
31 #include <undocuser.h>
33 #include <wine/debug.h>
34 #include <win32k/ntusrtyp.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(userinit
);
38 #define CMP_MAGIC 0x01234567
40 /* GLOBALS ******************************************************************/
42 /* FUNCTIONS ****************************************************************/
55 TRACE("(%p, %s, %p)\n", hKey
, debugstr_w(pszKey
), pValue
);
57 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, &dwType
, NULL
, &cbData
);
58 if (rc
!= ERROR_SUCCESS
)
60 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
65 WARN("Wrong registry data type (%u vs %u)\n", dwType
, REG_SZ
);
66 return ERROR_FILE_NOT_FOUND
;
68 Value
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0, cbData
+ sizeof(WCHAR
));
72 return ERROR_NOT_ENOUGH_MEMORY
;
74 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, NULL
, (LPBYTE
)Value
, &cbData
);
75 if (rc
!= ERROR_SUCCESS
)
77 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
78 HeapFree(GetProcessHeap(), 0, Value
);
81 /* NULL-terminate the string */
82 Value
[cbData
/ sizeof(WCHAR
)] = '\0';
89 BOOL
IsConsoleShell(VOID
)
91 HKEY ControlKey
= NULL
;
92 LPWSTR SystemStartOptions
= NULL
;
93 LPWSTR CurrentOption
, NextOption
; /* Pointers into SystemStartOptions */
101 REGSTR_PATH_CURRENT_CONTROL_SET
,
105 if (rc
!= ERROR_SUCCESS
)
107 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
111 rc
= ReadRegSzKey(ControlKey
, L
"SystemStartOptions", &SystemStartOptions
);
112 if (rc
!= ERROR_SUCCESS
)
114 WARN("ReadRegSzKey() failed with error %lu\n", rc
);
118 /* Check for CONSOLE in SystemStartOptions */
119 CurrentOption
= SystemStartOptions
;
120 while (CurrentOption
)
122 NextOption
= wcschr(CurrentOption
, L
' ');
125 if (_wcsicmp(CurrentOption
, L
"CONSOLE") == 0)
127 TRACE("Found 'CONSOLE' boot option\n");
131 CurrentOption
= NextOption
? NextOption
+ 1 : NULL
;
135 if (ControlKey
!= NULL
)
136 RegCloseKey(ControlKey
);
137 HeapFree(GetProcessHeap(), 0, SystemStartOptions
);
138 TRACE("IsConsoleShell() returning %d\n", ret
);
144 OUT WCHAR
*CommandLine
, /* must be at least MAX_PATH long */
149 WCHAR Shell
[MAX_PATH
];
151 BOOL ConsoleShell
= IsConsoleShell();
154 TRACE("(%p, %p)\n", CommandLine
, hRootKey
);
156 rc
= RegOpenKeyExW(hRootKey
, L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
157 0, KEY_QUERY_VALUE
, &hKey
);
158 if (rc
== ERROR_SUCCESS
)
160 Size
= MAX_PATH
* sizeof(WCHAR
);
161 rc
= RegQueryValueExW(hKey
,
162 ConsoleShell
? L
"ConsoleShell" : L
"Shell",
167 if (rc
== ERROR_SUCCESS
)
169 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
171 TRACE("Found command line %s\n", debugstr_w(Shell
));
172 wcscpy(CommandLine
, Shell
);
176 WARN("Wrong type %lu (expected %u or %u)\n", Type
, REG_SZ
, REG_EXPAND_SZ
);
179 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
183 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
189 StartAutoApplications(
192 WCHAR szPath
[MAX_PATH
] = {0};
195 WIN32_FIND_DATAW findData
;
196 SHELLEXECUTEINFOW ExecInfo
;
199 TRACE("(%d)\n", clsid
);
201 hResult
= SHGetFolderPathW(NULL
, clsid
, NULL
, SHGFP_TYPE_CURRENT
, szPath
);
202 len
= wcslen(szPath
);
203 if (!SUCCEEDED(hResult
) || len
== 0)
205 WARN("SHGetFolderPath() failed with error %lu\n", GetLastError());
209 wcscat(szPath
, L
"\\*");
210 hFind
= FindFirstFileW(szPath
, &findData
);
211 if (hFind
== INVALID_HANDLE_VALUE
)
213 WARN("FindFirstFile(%s) failed with error %lu\n", debugstr_w(szPath
), GetLastError());
219 if (!(findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (findData
.nFileSizeHigh
|| findData
.nFileSizeLow
))
221 memset(&ExecInfo
, 0x0, sizeof(SHELLEXECUTEINFOW
));
222 ExecInfo
.cbSize
= sizeof(ExecInfo
);
223 wcscpy(&szPath
[len
+1], findData
.cFileName
);
224 ExecInfo
.lpVerb
= L
"open";
225 ExecInfo
.lpFile
= szPath
;
226 ExecInfo
.lpDirectory
= NULL
;
227 TRACE("Executing %s in directory %s\n",
228 debugstr_w(findData
.cFileName
), debugstr_w(szPath
));
229 ShellExecuteExW(&ExecInfo
);
231 } while (FindNextFileW(hFind
, &findData
));
240 PROCESS_INFORMATION pi
;
241 WCHAR ExpandedShell
[MAX_PATH
];
243 TRACE("(%s)\n", debugstr_w(Shell
));
245 ZeroMemory(&si
, sizeof(STARTUPINFO
));
246 si
.cb
= sizeof(STARTUPINFO
);
247 ZeroMemory(&pi
, sizeof(PROCESS_INFORMATION
));
249 ExpandEnvironmentStrings(Shell
, ExpandedShell
, MAX_PATH
);
251 if (!CreateProcess(NULL
,
256 NORMAL_PRIORITY_CLASS
,
262 WARN("CreateProcess() failed with error %lu\n", GetLastError());
266 StartAutoApplications(CSIDL_STARTUP
);
267 StartAutoApplications(CSIDL_COMMON_STARTUP
);
268 CloseHandle(pi
.hProcess
);
269 CloseHandle(pi
.hThread
);
274 VOID
StartShell(VOID
)
276 WCHAR Shell
[MAX_PATH
];
277 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
285 /* Safe Mode shell run */
286 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
287 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Option",
288 0, KEY_QUERY_VALUE
, &hKey
);
289 if(rc
== ERROR_SUCCESS
)
291 Size
= sizeof(Value
);
292 rc
= RegQueryValueExW(hKey
, L
"UseAlternateShell", NULL
,
293 &Type
, (LPBYTE
)&Value
, &Size
);
294 if(rc
== ERROR_SUCCESS
)
297 if(Type
== REG_DWORD
)
301 /* Safe Mode Alternate Shell required */
302 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
303 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot",
305 if(rc
== ERROR_SUCCESS
)
307 Size
= MAX_PATH
* sizeof(WCHAR
);
308 rc
= RegQueryValueExW(hKey
, L
"AlternateShell", NULL
,
309 &Type
, (LPBYTE
)Shell
, &Size
);
310 if(rc
== ERROR_SUCCESS
)
313 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
315 TRACE("Key located - %s\n", debugstr_w(Shell
));
316 /* Try to run alternate shell */
317 if (TryToStartShell(Shell
))
319 TRACE("Alternate shell started (Safe Mode)\n");
325 WARN("Wrong type %lu (expected %u or %u)\n",
326 Type
, REG_SZ
, REG_EXPAND_SZ
);
331 WARN("Alternate shell in Safe Mode required but not specified.");
338 WARN("Wrong type %lu (expected %u)\n", Type
, REG_DWORD
);
342 /* Try to run shell in user key */
343 if (GetShell(Shell
, HKEY_CURRENT_USER
) && TryToStartShell(Shell
))
345 TRACE("Started shell from HKEY_CURRENT_USER\n");
349 /* Try to run shell in local machine key */
350 if (GetShell(Shell
, HKEY_LOCAL_MACHINE
) && TryToStartShell(Shell
))
352 TRACE("Started shell from HKEY_LOCAL_MACHINE\n");
356 /* Try default shell */
357 if (IsConsoleShell())
359 if (GetSystemDirectory(Shell
, MAX_PATH
- 8))
360 wcscat(Shell
, L
"\\cmd.exe");
362 wcscpy(Shell
, L
"cmd.exe");
366 if (GetWindowsDirectory(Shell
, MAX_PATH
- 13))
367 wcscat(Shell
, L
"\\explorer.exe");
369 wcscpy(Shell
, L
"explorer.exe");
371 if (!TryToStartShell(Shell
))
373 WARN("Failed to start default shell %s\n", debugstr_w(Shell
));
374 LoadString( GetModuleHandle(NULL
), STRING_USERINIT_FAIL
, szMsg
, sizeof(szMsg
) / sizeof(szMsg
[0]));
375 MessageBox(0, szMsg
, NULL
, 0);
379 const WCHAR g_RegColorNames
[][32] = {
380 L
"Scrollbar", /* 00 = COLOR_SCROLLBAR */
381 L
"Background", /* 01 = COLOR_DESKTOP */
382 L
"ActiveTitle", /* 02 = COLOR_ACTIVECAPTION */
383 L
"InactiveTitle", /* 03 = COLOR_INACTIVECAPTION */
384 L
"Menu", /* 04 = COLOR_MENU */
385 L
"Window", /* 05 = COLOR_WINDOW */
386 L
"WindowFrame", /* 06 = COLOR_WINDOWFRAME */
387 L
"MenuText", /* 07 = COLOR_MENUTEXT */
388 L
"WindowText", /* 08 = COLOR_WINDOWTEXT */
389 L
"TitleText", /* 09 = COLOR_CAPTIONTEXT */
390 L
"ActiveBorder", /* 10 = COLOR_ACTIVEBORDER */
391 L
"InactiveBorder", /* 11 = COLOR_INACTIVEBORDER */
392 L
"AppWorkSpace", /* 12 = COLOR_APPWORKSPACE */
393 L
"Hilight", /* 13 = COLOR_HIGHLIGHT */
394 L
"HilightText", /* 14 = COLOR_HIGHLIGHTTEXT */
395 L
"ButtonFace", /* 15 = COLOR_BTNFACE */
396 L
"ButtonShadow", /* 16 = COLOR_BTNSHADOW */
397 L
"GrayText", /* 17 = COLOR_GRAYTEXT */
398 L
"ButtonText", /* 18 = COLOR_BTNTEXT */
399 L
"InactiveTitleText", /* 19 = COLOR_INACTIVECAPTIONTEXT */
400 L
"ButtonHilight", /* 20 = COLOR_BTNHIGHLIGHT */
401 L
"ButtonDkShadow", /* 21 = COLOR_3DDKSHADOW */
402 L
"ButtonLight", /* 22 = COLOR_3DLIGHT */
403 L
"InfoText", /* 23 = COLOR_INFOTEXT */
404 L
"InfoWindow", /* 24 = COLOR_INFOBK */
405 L
"ButtonAlternateFace", /* 25 = COLOR_ALTERNATEBTNFACE */
406 L
"HotTrackingColor", /* 26 = COLOR_HOTLIGHT */
407 L
"GradientActiveTitle", /* 27 = COLOR_GRADIENTACTIVECAPTION */
408 L
"GradientInactiveTitle", /* 28 = COLOR_GRADIENTINACTIVECAPTION */
409 L
"MenuHilight", /* 29 = COLOR_MENUHILIGHT */
410 L
"MenuBar" /* 30 = COLOR_MENUBAR */
412 #define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0]))
415 COLORREF
StrToColorref(
420 TRACE("(%s)\n", debugstr_w(lpszCol
));
422 rgb
[0] = StrToIntW(lpszCol
);
423 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
424 rgb
[1] = StrToIntW(lpszCol
);
425 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
426 rgb
[2] = StrToIntW(lpszCol
);
427 return RGB(rgb
[0], rgb
[1], rgb
[2]);
431 VOID
SetUserSysColors(VOID
)
442 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_COLORS
,
443 0, KEY_QUERY_VALUE
, &hKey
);
444 if (rc
!= ERROR_SUCCESS
)
446 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
449 for(i
= 0; i
< NUM_SYSCOLORS
; i
++)
451 Size
= sizeof(szColor
);
452 rc
= RegQueryValueEx(hKey
, g_RegColorNames
[i
], NULL
, &Type
,
453 (LPBYTE
)szColor
, &Size
);
454 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
456 crColor
= StrToColorref(szColor
);
457 SetSysColors(1, &i
, &crColor
);
460 WARN("RegQueryValueEx(%s) failed with error %lu\n",
461 debugstr_w(g_RegColorNames
[i
]), rc
);
467 VOID
SetUserWallpaper(VOID
)
471 WCHAR szWallpaper
[MAX_PATH
+ 1];
476 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_DESKTOP
,
477 0, KEY_QUERY_VALUE
, &hKey
);
478 if (rc
== ERROR_SUCCESS
)
480 Size
= sizeof(szWallpaper
);
481 rc
= RegQueryValueEx(hKey
,
487 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
489 ExpandEnvironmentStrings(szWallpaper
, szWallpaper
, MAX_PATH
);
490 TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper
));
492 /* Load and change the wallpaper */
493 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, szWallpaper
, SPIF_SENDCHANGE
);
497 /* remove the wallpaper */
498 TRACE("No wallpaper set in registry (error %lu)\n", rc
);
499 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, NULL
, SPIF_SENDCHANGE
);
504 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
508 VOID
SetUserSettings(VOID
)
512 UpdatePerUserSystemParameters(1, TRUE
);
517 typedef DWORD (WINAPI
*PCMP_REPORT_LOGON
)(DWORD
, DWORD
);
523 PCMP_REPORT_LOGON CMP_Report_LogOn
;
527 hModule
= LoadLibrary(L
"setupapi.dll");
530 CMP_Report_LogOn
= (PCMP_REPORT_LOGON
)GetProcAddress(hModule
, "CMP_Report_LogOn");
531 if (CMP_Report_LogOn
)
532 CMP_Report_LogOn(CMP_MAGIC
, GetCurrentProcessId());
534 WARN("GetProcAddress() failed\n");
536 FreeLibrary(hModule
);
539 WARN("LoadLibrary() failed with error %lu\n", GetLastError());
543 #pragma warning(disable : 4100)
544 #endif /* _MSC_VER */
547 wWinMain(IN HINSTANCE hInst
,
548 IN HINSTANCE hPrevInstance
,
549 IN LPWSTR lpszCmdLine
,