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)
27 #define WIN32_NO_STATUS
29 #define COM_NO_WINDOWS_H
40 #include <undocuser.h>
41 #include <wine/debug.h>
45 WINE_DEFAULT_DEBUG_CHANNEL(userinit
);
47 #define CMP_MAGIC 0x01234567
49 /* GLOBALS ******************************************************************/
51 /* FUNCTIONS ****************************************************************/
64 TRACE("(%p, %s, %p)\n", hKey
, debugstr_w(pszKey
), pValue
);
66 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, &dwType
, NULL
, &cbData
);
67 if (rc
!= ERROR_SUCCESS
)
69 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
74 WARN("Wrong registry data type (%u vs %u)\n", dwType
, REG_SZ
);
75 return ERROR_FILE_NOT_FOUND
;
77 Value
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0, cbData
+ sizeof(WCHAR
));
81 return ERROR_NOT_ENOUGH_MEMORY
;
83 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, NULL
, (LPBYTE
)Value
, &cbData
);
84 if (rc
!= ERROR_SUCCESS
)
86 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
87 HeapFree(GetProcessHeap(), 0, Value
);
90 /* NULL-terminate the string */
91 Value
[cbData
/ sizeof(WCHAR
)] = '\0';
98 BOOL
IsConsoleShell(VOID
)
100 HKEY ControlKey
= NULL
;
101 LPWSTR SystemStartOptions
= NULL
;
102 LPWSTR CurrentOption
, NextOption
; /* Pointers into SystemStartOptions */
110 REGSTR_PATH_CURRENT_CONTROL_SET
,
114 if (rc
!= ERROR_SUCCESS
)
116 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
120 rc
= ReadRegSzKey(ControlKey
, L
"SystemStartOptions", &SystemStartOptions
);
121 if (rc
!= ERROR_SUCCESS
)
123 WARN("ReadRegSzKey() failed with error %lu\n", rc
);
127 /* Check for CONSOLE switch in SystemStartOptions */
128 CurrentOption
= SystemStartOptions
;
129 while (CurrentOption
)
131 NextOption
= wcschr(CurrentOption
, L
' ');
134 if (_wcsicmp(CurrentOption
, L
"CONSOLE") == 0)
136 TRACE("Found 'CONSOLE' boot option\n");
140 CurrentOption
= NextOption
? NextOption
+ 1 : NULL
;
144 if (ControlKey
!= NULL
)
145 RegCloseKey(ControlKey
);
146 HeapFree(GetProcessHeap(), 0, SystemStartOptions
);
147 TRACE("IsConsoleShell() returning %d\n", ret
);
153 OUT WCHAR
*CommandLine
, /* must be at least MAX_PATH long */
158 WCHAR Shell
[MAX_PATH
];
160 BOOL ConsoleShell
= IsConsoleShell();
163 TRACE("(%p, %p)\n", CommandLine
, hRootKey
);
165 rc
= RegOpenKeyExW(hRootKey
, L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
166 0, KEY_QUERY_VALUE
, &hKey
);
167 if (rc
== ERROR_SUCCESS
)
169 Size
= MAX_PATH
* sizeof(WCHAR
);
170 rc
= RegQueryValueExW(hKey
,
171 ConsoleShell
? L
"ConsoleShell" : L
"Shell",
176 if (rc
== ERROR_SUCCESS
)
178 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
180 TRACE("Found command line %s\n", debugstr_w(Shell
));
181 wcscpy(CommandLine
, Shell
);
185 WARN("Wrong type %lu (expected %u or %u)\n", Type
, REG_SZ
, REG_EXPAND_SZ
);
188 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
192 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
198 StartAutoApplications(
201 WCHAR szPath
[MAX_PATH
] = {0};
204 WIN32_FIND_DATAW findData
;
205 SHELLEXECUTEINFOW ExecInfo
;
208 TRACE("(%d)\n", clsid
);
210 hResult
= SHGetFolderPathW(NULL
, clsid
, NULL
, SHGFP_TYPE_CURRENT
, szPath
);
211 len
= wcslen(szPath
);
212 if (!SUCCEEDED(hResult
) || len
== 0)
214 WARN("SHGetFolderPath() failed with error %lu\n", GetLastError());
218 wcscat(szPath
, L
"\\*");
219 hFind
= FindFirstFileW(szPath
, &findData
);
220 if (hFind
== INVALID_HANDLE_VALUE
)
222 WARN("FindFirstFile(%s) failed with error %lu\n", debugstr_w(szPath
), GetLastError());
228 if (!(findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (findData
.nFileSizeHigh
|| findData
.nFileSizeLow
))
230 memset(&ExecInfo
, 0x0, sizeof(SHELLEXECUTEINFOW
));
231 ExecInfo
.cbSize
= sizeof(ExecInfo
);
232 wcscpy(&szPath
[len
+1], findData
.cFileName
);
233 ExecInfo
.lpVerb
= L
"open";
234 ExecInfo
.lpFile
= szPath
;
235 ExecInfo
.lpDirectory
= NULL
;
236 TRACE("Executing %s in directory %s\n",
237 debugstr_w(findData
.cFileName
), debugstr_w(szPath
));
238 ShellExecuteExW(&ExecInfo
);
240 } while (FindNextFileW(hFind
, &findData
));
249 PROCESS_INFORMATION pi
;
250 WCHAR ExpandedShell
[MAX_PATH
];
252 TRACE("(%s)\n", debugstr_w(Shell
));
254 ZeroMemory(&si
, sizeof(si
));
256 si
.dwFlags
= STARTF_USESHOWWINDOW
;
257 si
.wShowWindow
= SW_SHOWNORMAL
;
258 ZeroMemory(&pi
, sizeof(pi
));
260 ExpandEnvironmentStrings(Shell
, ExpandedShell
, MAX_PATH
);
262 if (!CreateProcess(NULL
,
267 NORMAL_PRIORITY_CLASS
,
273 WARN("CreateProcess() failed with error %lu\n", GetLastError());
277 StartAutoApplications(CSIDL_STARTUP
);
278 StartAutoApplications(CSIDL_COMMON_STARTUP
);
279 CloseHandle(pi
.hProcess
);
280 CloseHandle(pi
.hThread
);
285 VOID
StartShell(VOID
)
287 WCHAR Shell
[MAX_PATH
];
288 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
296 /* Safe Mode shell run */
297 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
298 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Option",
299 0, KEY_QUERY_VALUE
, &hKey
);
300 if(rc
== ERROR_SUCCESS
)
302 Size
= sizeof(Value
);
303 rc
= RegQueryValueExW(hKey
, L
"UseAlternateShell", NULL
,
304 &Type
, (LPBYTE
)&Value
, &Size
);
305 if(rc
== ERROR_SUCCESS
)
308 if(Type
== REG_DWORD
)
312 /* Safe Mode Alternate Shell required */
313 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
314 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot",
316 if(rc
== ERROR_SUCCESS
)
318 Size
= MAX_PATH
* sizeof(WCHAR
);
319 rc
= RegQueryValueExW(hKey
, L
"AlternateShell", NULL
,
320 &Type
, (LPBYTE
)Shell
, &Size
);
321 if(rc
== ERROR_SUCCESS
)
324 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
326 TRACE("Key located - %s\n", debugstr_w(Shell
));
327 /* Try to run alternate shell */
328 if (TryToStartShell(Shell
))
330 TRACE("Alternate shell started (Safe Mode)\n");
336 WARN("Wrong type %lu (expected %u or %u)\n",
337 Type
, REG_SZ
, REG_EXPAND_SZ
);
342 WARN("Alternate shell in Safe Mode required but not specified.");
349 WARN("Wrong type %lu (expected %u)\n", Type
, REG_DWORD
);
353 /* Try to run shell in user key */
354 if (GetShell(Shell
, HKEY_CURRENT_USER
) && TryToStartShell(Shell
))
356 TRACE("Started shell from HKEY_CURRENT_USER\n");
360 /* Try to run shell in local machine key */
361 if (GetShell(Shell
, HKEY_LOCAL_MACHINE
) && TryToStartShell(Shell
))
363 TRACE("Started shell from HKEY_LOCAL_MACHINE\n");
367 /* Try default shell */
368 if (IsConsoleShell())
370 if (GetSystemDirectory(Shell
, MAX_PATH
- 8))
371 wcscat(Shell
, L
"\\cmd.exe");
373 wcscpy(Shell
, L
"cmd.exe");
377 if (GetWindowsDirectory(Shell
, MAX_PATH
- 13))
378 wcscat(Shell
, L
"\\explorer.exe");
380 wcscpy(Shell
, L
"explorer.exe");
382 if (!TryToStartShell(Shell
))
384 WARN("Failed to start default shell %s\n", debugstr_w(Shell
));
385 LoadString( GetModuleHandle(NULL
), STRING_USERINIT_FAIL
, szMsg
, sizeof(szMsg
) / sizeof(szMsg
[0]));
386 MessageBox(0, szMsg
, NULL
, 0);
390 const WCHAR g_RegColorNames
[][32] = {
391 L
"Scrollbar", /* 00 = COLOR_SCROLLBAR */
392 L
"Background", /* 01 = COLOR_DESKTOP */
393 L
"ActiveTitle", /* 02 = COLOR_ACTIVECAPTION */
394 L
"InactiveTitle", /* 03 = COLOR_INACTIVECAPTION */
395 L
"Menu", /* 04 = COLOR_MENU */
396 L
"Window", /* 05 = COLOR_WINDOW */
397 L
"WindowFrame", /* 06 = COLOR_WINDOWFRAME */
398 L
"MenuText", /* 07 = COLOR_MENUTEXT */
399 L
"WindowText", /* 08 = COLOR_WINDOWTEXT */
400 L
"TitleText", /* 09 = COLOR_CAPTIONTEXT */
401 L
"ActiveBorder", /* 10 = COLOR_ACTIVEBORDER */
402 L
"InactiveBorder", /* 11 = COLOR_INACTIVEBORDER */
403 L
"AppWorkSpace", /* 12 = COLOR_APPWORKSPACE */
404 L
"Hilight", /* 13 = COLOR_HIGHLIGHT */
405 L
"HilightText", /* 14 = COLOR_HIGHLIGHTTEXT */
406 L
"ButtonFace", /* 15 = COLOR_BTNFACE */
407 L
"ButtonShadow", /* 16 = COLOR_BTNSHADOW */
408 L
"GrayText", /* 17 = COLOR_GRAYTEXT */
409 L
"ButtonText", /* 18 = COLOR_BTNTEXT */
410 L
"InactiveTitleText", /* 19 = COLOR_INACTIVECAPTIONTEXT */
411 L
"ButtonHilight", /* 20 = COLOR_BTNHIGHLIGHT */
412 L
"ButtonDkShadow", /* 21 = COLOR_3DDKSHADOW */
413 L
"ButtonLight", /* 22 = COLOR_3DLIGHT */
414 L
"InfoText", /* 23 = COLOR_INFOTEXT */
415 L
"InfoWindow", /* 24 = COLOR_INFOBK */
416 L
"ButtonAlternateFace", /* 25 = COLOR_ALTERNATEBTNFACE */
417 L
"HotTrackingColor", /* 26 = COLOR_HOTLIGHT */
418 L
"GradientActiveTitle", /* 27 = COLOR_GRADIENTACTIVECAPTION */
419 L
"GradientInactiveTitle", /* 28 = COLOR_GRADIENTINACTIVECAPTION */
420 L
"MenuHilight", /* 29 = COLOR_MENUHILIGHT */
421 L
"MenuBar" /* 30 = COLOR_MENUBAR */
423 #define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0]))
426 COLORREF
StrToColorref(
431 TRACE("(%s)\n", debugstr_w(lpszCol
));
433 rgb
[0] = StrToIntW(lpszCol
);
434 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
435 rgb
[1] = StrToIntW(lpszCol
);
436 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
437 rgb
[2] = StrToIntW(lpszCol
);
438 return RGB(rgb
[0], rgb
[1], rgb
[2]);
442 VOID
SetUserSysColors(VOID
)
453 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_COLORS
,
454 0, KEY_QUERY_VALUE
, &hKey
);
455 if (rc
!= ERROR_SUCCESS
)
457 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
460 for(i
= 0; i
< NUM_SYSCOLORS
; i
++)
462 Size
= sizeof(szColor
);
463 rc
= RegQueryValueEx(hKey
, g_RegColorNames
[i
], NULL
, &Type
,
464 (LPBYTE
)szColor
, &Size
);
465 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
467 crColor
= StrToColorref(szColor
);
468 SetSysColors(1, &i
, &crColor
);
471 WARN("RegQueryValueEx(%s) failed with error %lu\n",
472 debugstr_w(g_RegColorNames
[i
]), rc
);
478 VOID
SetUserWallpaper(VOID
)
482 WCHAR szWallpaper
[MAX_PATH
+ 1];
487 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_DESKTOP
,
488 0, KEY_QUERY_VALUE
, &hKey
);
489 if (rc
== ERROR_SUCCESS
)
491 Size
= sizeof(szWallpaper
);
492 rc
= RegQueryValueEx(hKey
,
498 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
500 ExpandEnvironmentStrings(szWallpaper
, szWallpaper
, MAX_PATH
);
501 TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper
));
503 /* Load and change the wallpaper */
504 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, szWallpaper
, SPIF_SENDCHANGE
);
508 /* remove the wallpaper */
509 TRACE("No wallpaper set in registry (error %lu)\n", rc
);
510 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, NULL
, SPIF_SENDCHANGE
);
515 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
519 VOID
SetUserSettings(VOID
)
523 UpdatePerUserSystemParameters(1, TRUE
);
528 typedef DWORD (WINAPI
*PCMP_REPORT_LOGON
)(DWORD
, DWORD
);
534 PCMP_REPORT_LOGON CMP_Report_LogOn
;
538 hModule
= LoadLibrary(L
"setupapi.dll");
541 CMP_Report_LogOn
= (PCMP_REPORT_LOGON
)GetProcAddress(hModule
, "CMP_Report_LogOn");
542 if (CMP_Report_LogOn
)
543 CMP_Report_LogOn(CMP_MAGIC
, GetCurrentProcessId());
545 WARN("GetProcAddress() failed\n");
547 FreeLibrary(hModule
);
550 WARN("LoadLibrary() failed with error %lu\n", GetLastError());
554 #pragma warning(disable : 4100)
555 #endif /* _MSC_VER */
558 wWinMain(IN HINSTANCE hInst
,
559 IN HINSTANCE hPrevInstance
,
560 IN LPWSTR lpszCmdLine
,