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)
32 #include <wine/debug.h>
33 #include <win32k/ntusrtyp.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(userinit
);
37 #define CMP_MAGIC 0x01234567
39 /* GLOBALS ******************************************************************/
41 /* FUNCTIONS ****************************************************************/
54 TRACE("(%p, %s, %p)\n", hKey
, debugstr_w(pszKey
), pValue
);
56 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, &dwType
, NULL
, &cbData
);
57 if (rc
!= ERROR_SUCCESS
)
59 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
64 WARN("Wrong registry data type (%u vs %u)\n", dwType
, REG_SZ
);
65 return ERROR_FILE_NOT_FOUND
;
67 Value
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0, cbData
+ sizeof(WCHAR
));
71 return ERROR_NOT_ENOUGH_MEMORY
;
73 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, NULL
, (LPBYTE
)Value
, &cbData
);
74 if (rc
!= ERROR_SUCCESS
)
76 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
77 HeapFree(GetProcessHeap(), 0, Value
);
80 /* NULL-terminate the string */
81 Value
[cbData
/ sizeof(WCHAR
)] = '\0';
88 BOOL
IsConsoleShell(VOID
)
90 HKEY ControlKey
= NULL
;
91 LPWSTR SystemStartOptions
= NULL
;
92 LPWSTR CurrentOption
, NextOption
; /* Pointers into SystemStartOptions */
100 REGSTR_PATH_CURRENT_CONTROL_SET
,
104 if (rc
!= ERROR_SUCCESS
)
106 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
110 rc
= ReadRegSzKey(ControlKey
, L
"SystemStartOptions", &SystemStartOptions
);
111 if (rc
!= ERROR_SUCCESS
)
113 WARN("ReadRegSzKey() failed with error %lu\n", rc
);
117 /* Check for CONSOLE in SystemStartOptions */
118 CurrentOption
= SystemStartOptions
;
119 while (CurrentOption
)
121 NextOption
= wcschr(CurrentOption
, L
' ');
124 if (_wcsicmp(CurrentOption
, L
"CONSOLE") == 0)
126 TRACE("Found 'CONSOLE' boot option\n");
130 CurrentOption
= NextOption
? NextOption
+ 1 : NULL
;
134 if (ControlKey
!= NULL
)
135 RegCloseKey(ControlKey
);
136 HeapFree(GetProcessHeap(), 0, SystemStartOptions
);
137 TRACE("IsConsoleShell() returning %d\n", ret
);
143 OUT WCHAR
*CommandLine
, /* must be at least MAX_PATH long */
148 WCHAR Shell
[MAX_PATH
];
150 BOOL ConsoleShell
= IsConsoleShell();
153 TRACE("(%p, %p)\n", CommandLine
, hRootKey
);
155 rc
= RegOpenKeyExW(hRootKey
, L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
156 0, KEY_QUERY_VALUE
, &hKey
);
157 if (rc
== ERROR_SUCCESS
)
159 Size
= MAX_PATH
* sizeof(WCHAR
);
160 rc
= RegQueryValueExW(hKey
,
161 ConsoleShell
? L
"ConsoleShell" : L
"Shell",
166 if (rc
== ERROR_SUCCESS
)
168 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
170 TRACE("Found command line %s\n", debugstr_w(Shell
));
171 wcscpy(CommandLine
, Shell
);
175 WARN("Wrong type %lu (expected %u or %u)\n", Type
, REG_SZ
, REG_EXPAND_SZ
);
178 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
182 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
188 StartAutoApplications(
191 WCHAR szPath
[MAX_PATH
] = {0};
194 WIN32_FIND_DATAW findData
;
195 SHELLEXECUTEINFOW ExecInfo
;
198 TRACE("(%d)\n", clsid
);
200 hResult
= SHGetFolderPathW(NULL
, clsid
, NULL
, SHGFP_TYPE_CURRENT
, szPath
);
201 len
= wcslen(szPath
);
202 if (!SUCCEEDED(hResult
) || len
== 0)
204 WARN("SHGetFolderPath() failed with error %lu\n", GetLastError());
208 wcscat(szPath
, L
"\\*");
209 hFind
= FindFirstFileW(szPath
, &findData
);
210 if (hFind
== INVALID_HANDLE_VALUE
)
212 WARN("FindFirstFile(%s) failed with error %lu\n", debugstr_w(szPath
), GetLastError());
218 if (!(findData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (findData
.nFileSizeHigh
|| findData
.nFileSizeLow
))
220 memset(&ExecInfo
, 0x0, sizeof(SHELLEXECUTEINFOW
));
221 ExecInfo
.cbSize
= sizeof(ExecInfo
);
222 wcscpy(&szPath
[len
+1], findData
.cFileName
);
223 ExecInfo
.lpVerb
= L
"open";
224 ExecInfo
.lpFile
= szPath
;
225 ExecInfo
.lpDirectory
= NULL
;
226 TRACE("Executing %s in directory %s\n",
227 debugstr_w(findData
.cFileName
), debugstr_w(szPath
));
228 ShellExecuteExW(&ExecInfo
);
230 } while (FindNextFileW(hFind
, &findData
));
239 PROCESS_INFORMATION pi
;
240 WCHAR ExpandedShell
[MAX_PATH
];
242 TRACE("(%s)\n", debugstr_w(Shell
));
244 ZeroMemory(&si
, sizeof(STARTUPINFO
));
245 si
.cb
= sizeof(STARTUPINFO
);
246 ZeroMemory(&pi
, sizeof(PROCESS_INFORMATION
));
248 ExpandEnvironmentStrings(Shell
, ExpandedShell
, MAX_PATH
);
250 if (!CreateProcess(NULL
,
255 NORMAL_PRIORITY_CLASS
,
261 WARN("CreateProcess() failed with error %lu\n", GetLastError());
265 StartAutoApplications(CSIDL_STARTUP
);
266 StartAutoApplications(CSIDL_COMMON_STARTUP
);
267 CloseHandle(pi
.hProcess
);
268 CloseHandle(pi
.hThread
);
273 VOID
StartShell(VOID
)
275 WCHAR Shell
[MAX_PATH
];
276 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
284 /* Safe Mode shell run */
285 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
286 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot\\Option",
287 0, KEY_QUERY_VALUE
, &hKey
);
288 if(rc
== ERROR_SUCCESS
)
290 Size
= sizeof(Value
);
291 rc
= RegQueryValueExW(hKey
, L
"UseAlternateShell", NULL
,
292 &Type
, (LPBYTE
)&Value
, &Size
);
293 if(rc
== ERROR_SUCCESS
)
296 if(Type
== REG_DWORD
)
300 /* Safe Mode Alternate Shell required */
301 rc
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
302 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot",
304 if(rc
== ERROR_SUCCESS
)
306 Size
= MAX_PATH
* sizeof(WCHAR
);
307 rc
= RegQueryValueExW(hKey
, L
"AlternateShell", NULL
,
308 &Type
, (LPBYTE
)Shell
, &Size
);
309 if(rc
== ERROR_SUCCESS
)
312 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
314 TRACE("Key located - %s\n", debugstr_w(Shell
));
315 /* Try to run alternate shell */
316 if (TryToStartShell(Shell
))
318 TRACE("Alternate shell started (Safe Mode)\n");
324 WARN("Wrong type %lu (expected %u or %u)\n",
325 Type
, REG_SZ
, REG_EXPAND_SZ
);
330 WARN("Alternate shell in Safe Mode required but not specified.");
337 WARN("Wrong type %lu (expected %u)\n", Type
, REG_DWORD
);
341 /* Try to run shell in user key */
342 if (GetShell(Shell
, HKEY_CURRENT_USER
) && TryToStartShell(Shell
))
344 TRACE("Started shell from HKEY_CURRENT_USER\n");
348 /* Try to run shell in local machine key */
349 if (GetShell(Shell
, HKEY_LOCAL_MACHINE
) && TryToStartShell(Shell
))
351 TRACE("Started shell from HKEY_LOCAL_MACHINE\n");
355 /* Try default shell */
356 if (IsConsoleShell())
358 if (GetSystemDirectory(Shell
, MAX_PATH
- 8))
359 wcscat(Shell
, L
"\\cmd.exe");
361 wcscpy(Shell
, L
"cmd.exe");
365 if (GetWindowsDirectory(Shell
, MAX_PATH
- 13))
366 wcscat(Shell
, L
"\\explorer.exe");
368 wcscpy(Shell
, L
"explorer.exe");
370 if (!TryToStartShell(Shell
))
372 WARN("Failed to start default shell %s\n", debugstr_w(Shell
));
373 LoadString( GetModuleHandle(NULL
), STRING_USERINIT_FAIL
, szMsg
, sizeof(szMsg
) / sizeof(szMsg
[0]));
374 MessageBox(0, szMsg
, NULL
, 0);
378 const WCHAR g_RegColorNames
[][32] = {
379 L
"Scrollbar", /* 00 = COLOR_SCROLLBAR */
380 L
"Background", /* 01 = COLOR_DESKTOP */
381 L
"ActiveTitle", /* 02 = COLOR_ACTIVECAPTION */
382 L
"InactiveTitle", /* 03 = COLOR_INACTIVECAPTION */
383 L
"Menu", /* 04 = COLOR_MENU */
384 L
"Window", /* 05 = COLOR_WINDOW */
385 L
"WindowFrame", /* 06 = COLOR_WINDOWFRAME */
386 L
"MenuText", /* 07 = COLOR_MENUTEXT */
387 L
"WindowText", /* 08 = COLOR_WINDOWTEXT */
388 L
"TitleText", /* 09 = COLOR_CAPTIONTEXT */
389 L
"ActiveBorder", /* 10 = COLOR_ACTIVEBORDER */
390 L
"InactiveBorder", /* 11 = COLOR_INACTIVEBORDER */
391 L
"AppWorkSpace", /* 12 = COLOR_APPWORKSPACE */
392 L
"Hilight", /* 13 = COLOR_HIGHLIGHT */
393 L
"HilightText", /* 14 = COLOR_HIGHLIGHTTEXT */
394 L
"ButtonFace", /* 15 = COLOR_BTNFACE */
395 L
"ButtonShadow", /* 16 = COLOR_BTNSHADOW */
396 L
"GrayText", /* 17 = COLOR_GRAYTEXT */
397 L
"ButtonText", /* 18 = COLOR_BTNTEXT */
398 L
"InactiveTitleText", /* 19 = COLOR_INACTIVECAPTIONTEXT */
399 L
"ButtonHilight", /* 20 = COLOR_BTNHIGHLIGHT */
400 L
"ButtonDkShadow", /* 21 = COLOR_3DDKSHADOW */
401 L
"ButtonLight", /* 22 = COLOR_3DLIGHT */
402 L
"InfoText", /* 23 = COLOR_INFOTEXT */
403 L
"InfoWindow", /* 24 = COLOR_INFOBK */
404 L
"ButtonAlternateFace", /* 25 = COLOR_ALTERNATEBTNFACE */
405 L
"HotTrackingColor", /* 26 = COLOR_HOTLIGHT */
406 L
"GradientActiveTitle", /* 27 = COLOR_GRADIENTACTIVECAPTION */
407 L
"GradientInactiveTitle", /* 28 = COLOR_GRADIENTINACTIVECAPTION */
408 L
"MenuHilight", /* 29 = COLOR_MENUHILIGHT */
409 L
"MenuBar" /* 30 = COLOR_MENUBAR */
411 #define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0]))
414 COLORREF
StrToColorref(
419 TRACE("(%s)\n", debugstr_w(lpszCol
));
421 rgb
[0] = StrToIntW(lpszCol
);
422 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
423 rgb
[1] = StrToIntW(lpszCol
);
424 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
425 rgb
[2] = StrToIntW(lpszCol
);
426 return RGB(rgb
[0], rgb
[1], rgb
[2]);
430 VOID
SetUserSysColors(VOID
)
441 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_COLORS
,
442 0, KEY_QUERY_VALUE
, &hKey
);
443 if (rc
!= ERROR_SUCCESS
)
445 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
448 for(i
= 0; i
< NUM_SYSCOLORS
; i
++)
450 Size
= sizeof(szColor
);
451 rc
= RegQueryValueEx(hKey
, g_RegColorNames
[i
], NULL
, &Type
,
452 (LPBYTE
)szColor
, &Size
);
453 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
455 crColor
= StrToColorref(szColor
);
456 SetSysColors(1, &i
, &crColor
);
459 WARN("RegQueryValueEx(%s) failed with error %lu\n",
460 debugstr_w(g_RegColorNames
[i
]), rc
);
467 UpdatePerUserSystemParameters(DWORD dw1
, BOOL bEnable
);
471 VOID
SetUserWallpaper(VOID
)
475 WCHAR szWallpaper
[MAX_PATH
+ 1];
480 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_DESKTOP
,
481 0, KEY_QUERY_VALUE
, &hKey
);
482 if (rc
== ERROR_SUCCESS
)
484 Size
= sizeof(szWallpaper
);
485 rc
= RegQueryValueEx(hKey
,
491 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
493 ExpandEnvironmentStrings(szWallpaper
, szWallpaper
, MAX_PATH
);
494 TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper
));
496 /* Load and change the wallpaper */
497 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, szWallpaper
, SPIF_SENDCHANGE
);
501 /* remove the wallpaper */
502 TRACE("No wallpaper set in registry (error %lu)\n", rc
);
503 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, NULL
, SPIF_SENDCHANGE
);
508 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
512 VOID
SetUserSettings(VOID
)
516 UpdatePerUserSystemParameters(1, TRUE
);
521 typedef DWORD (WINAPI
*PCMP_REPORT_LOGON
)(DWORD
, DWORD
);
527 PCMP_REPORT_LOGON CMP_Report_LogOn
;
531 hModule
= LoadLibrary(L
"setupapi.dll");
534 CMP_Report_LogOn
= (PCMP_REPORT_LOGON
)GetProcAddress(hModule
, "CMP_Report_LogOn");
535 if (CMP_Report_LogOn
)
536 CMP_Report_LogOn(CMP_MAGIC
, GetCurrentProcessId());
538 WARN("GetProcAddress() failed\n");
540 FreeLibrary(hModule
);
543 WARN("LoadLibrary() failed with error %lu\n", GetLastError());
547 #pragma warning(disable : 4100)
548 #endif /* _MSC_VER */
551 wWinMain(IN HINSTANCE hInst
,
552 IN HINSTANCE hPrevInstance
,
553 IN LPWSTR lpszCmdLine
,