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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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>
34 WINE_DEFAULT_DEBUG_CHANNEL(userinit
);
36 #define CMP_MAGIC 0x01234567
38 /* GLOBALS ******************************************************************/
40 /* FUNCTIONS ****************************************************************/
53 TRACE("(%p, %s, %p)\n", hKey
, debugstr_w(pszKey
), pValue
);
55 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, &dwType
, NULL
, &cbData
);
56 if (rc
!= ERROR_SUCCESS
)
58 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
63 WARN("Wrong registry data type (%u vs %u)\n", dwType
, REG_SZ
);
64 return ERROR_FILE_NOT_FOUND
;
66 Value
= (WCHAR
*) HeapAlloc(GetProcessHeap(), 0, cbData
+ sizeof(WCHAR
));
70 return ERROR_NOT_ENOUGH_MEMORY
;
72 rc
= RegQueryValueExW(hKey
, pszKey
, NULL
, NULL
, (LPBYTE
)Value
, &cbData
);
73 if (rc
!= ERROR_SUCCESS
)
75 WARN("RegQueryValueEx(%s) failed with error %lu\n", debugstr_w(pszKey
), rc
);
76 HeapFree(GetProcessHeap(), 0, Value
);
79 /* NULL-terminate the string */
80 Value
[cbData
/ sizeof(WCHAR
)] = '\0';
87 BOOL
IsConsoleShell(VOID
)
89 HKEY ControlKey
= NULL
;
90 LPWSTR SystemStartOptions
= NULL
;
91 LPWSTR CurrentOption
, NextOption
; /* Pointers into SystemStartOptions */
99 REGSTR_PATH_CURRENT_CONTROL_SET
,
103 if (rc
!= ERROR_SUCCESS
)
105 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
109 rc
= ReadRegSzKey(ControlKey
, L
"SystemStartOptions", &SystemStartOptions
);
110 if (rc
!= ERROR_SUCCESS
)
112 WARN("ReadRegSzKey() failed with error %lu\n", rc
);
116 /* Check for CONSOLE in SystemStartOptions */
117 CurrentOption
= SystemStartOptions
;
118 while (CurrentOption
)
120 NextOption
= wcschr(CurrentOption
, L
' ');
123 if (wcsicmp(CurrentOption
, L
"CONSOLE") == 0)
125 TRACE("Found 'CONSOLE' boot option\n");
129 CurrentOption
= NextOption
? NextOption
+ 1 : NULL
;
133 if (ControlKey
!= NULL
)
134 RegCloseKey(ControlKey
);
135 HeapFree(GetProcessHeap(), 0, SystemStartOptions
);
136 TRACE("IsConsoleShell() returning %d\n", ret
);
142 OUT WCHAR
*CommandLine
, /* must be at least MAX_PATH long */
147 WCHAR Shell
[MAX_PATH
];
149 BOOL ConsoleShell
= IsConsoleShell();
152 TRACE("(%p, %p)\n", CommandLine
, hRootKey
);
154 rc
= RegOpenKeyExW(hRootKey
, L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon",
155 0, KEY_QUERY_VALUE
, &hKey
);
156 if (rc
== ERROR_SUCCESS
)
158 Size
= MAX_PATH
* sizeof(WCHAR
);
159 rc
= RegQueryValueExW(hKey
,
160 ConsoleShell
? L
"ConsoleShell" : L
"Shell",
165 if (rc
== ERROR_SUCCESS
)
167 if ((Type
== REG_SZ
) || (Type
== REG_EXPAND_SZ
))
169 TRACE("Found command line %s\n", debugstr_w(Shell
));
170 wcscpy(CommandLine
, Shell
);
174 WARN("Wrong type %lu (expected %u or %u)\n", Type
, REG_SZ
, REG_EXPAND_SZ
);
177 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
181 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
187 StartAutoApplications(
190 WCHAR szPath
[MAX_PATH
] = {0};
193 WIN32_FIND_DATAW findData
;
194 SHELLEXECUTEINFOW ExecInfo
;
197 TRACE("(%d)\n", clsid
);
199 hResult
= SHGetFolderPathW(NULL
, clsid
, NULL
, SHGFP_TYPE_CURRENT
, szPath
);
200 len
= wcslen(szPath
);
201 if (!SUCCEEDED(hResult
) || len
== 0)
203 WARN("SHGetFolderPath() failed with error %lu\n", GetLastError());
207 wcscat(szPath
, L
"\\*");
208 hFind
= FindFirstFileW(szPath
, &findData
);
209 if (hFind
== INVALID_HANDLE_VALUE
)
211 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 ExecInfo
.lpVerb
= L
"open";
223 ExecInfo
.lpFile
= findData
.cFileName
;
224 ExecInfo
.lpDirectory
= szPath
;
225 TRACE("Executing %s in directory %s\n",
226 debugstr_w(findData
.cFileName
), debugstr_w(szPath
));
227 ShellExecuteExW(&ExecInfo
);
229 } while (FindNextFileW(hFind
, &findData
));
238 PROCESS_INFORMATION pi
;
239 WCHAR ExpandedShell
[MAX_PATH
];
241 TRACE("(%s)\n", debugstr_w(Shell
));
243 ZeroMemory(&si
, sizeof(STARTUPINFO
));
244 si
.cb
= sizeof(STARTUPINFO
);
245 ZeroMemory(&pi
, sizeof(PROCESS_INFORMATION
));
247 ExpandEnvironmentStrings(Shell
, ExpandedShell
, MAX_PATH
);
249 if (!CreateProcess(NULL
,
254 NORMAL_PRIORITY_CLASS
,
260 WARN("CreateProcess() failed with error %lu\n", GetLastError());
264 StartAutoApplications(CSIDL_STARTUP
);
265 StartAutoApplications(CSIDL_COMMON_STARTUP
);
266 CloseHandle(pi
.hProcess
);
267 CloseHandle(pi
.hThread
);
272 VOID
StartShell(VOID
)
274 WCHAR Shell
[MAX_PATH
];
275 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
279 /* Try to run shell in user key */
280 if (GetShell(Shell
, HKEY_CURRENT_USER
) && TryToStartShell(Shell
))
282 TRACE("Started shell from HKEY_CURRENT_USER\n");
286 /* Try to run shell in local machine key */
287 if (GetShell(Shell
, HKEY_LOCAL_MACHINE
) && TryToStartShell(Shell
))
289 TRACE("Started shell from HKEY_LOCAL_MACHINE\n");
293 /* Try default shell */
294 if (IsConsoleShell())
296 if (GetSystemDirectory(Shell
, MAX_PATH
- 8))
297 wcscat(Shell
, L
"\\cmd.exe");
299 wcscpy(Shell
, L
"cmd.exe");
303 if (GetWindowsDirectory(Shell
, MAX_PATH
- 13))
304 wcscat(Shell
, L
"\\explorer.exe");
306 wcscpy(Shell
, L
"explorer.exe");
308 if (!TryToStartShell(Shell
))
310 WARN("Failed to start default shell %s\n", debugstr_w(Shell
));
311 LoadString( GetModuleHandle(NULL
), STRING_USERINIT_FAIL
, szMsg
, sizeof(szMsg
) / sizeof(szMsg
[0]));
312 MessageBox(0, szMsg
, NULL
, 0);
316 const WCHAR g_RegColorNames
[][32] = {
317 L
"Scrollbar", /* 00 = COLOR_SCROLLBAR */
318 L
"Background", /* 01 = COLOR_DESKTOP */
319 L
"ActiveTitle", /* 02 = COLOR_ACTIVECAPTION */
320 L
"InactiveTitle", /* 03 = COLOR_INACTIVECAPTION */
321 L
"Menu", /* 04 = COLOR_MENU */
322 L
"Window", /* 05 = COLOR_WINDOW */
323 L
"WindowFrame", /* 06 = COLOR_WINDOWFRAME */
324 L
"MenuText", /* 07 = COLOR_MENUTEXT */
325 L
"WindowText", /* 08 = COLOR_WINDOWTEXT */
326 L
"TitleText", /* 09 = COLOR_CAPTIONTEXT */
327 L
"ActiveBorder", /* 10 = COLOR_ACTIVEBORDER */
328 L
"InactiveBorder", /* 11 = COLOR_INACTIVEBORDER */
329 L
"AppWorkSpace", /* 12 = COLOR_APPWORKSPACE */
330 L
"Hilight", /* 13 = COLOR_HIGHLIGHT */
331 L
"HilightText", /* 14 = COLOR_HIGHLIGHTTEXT */
332 L
"ButtonFace", /* 15 = COLOR_BTNFACE */
333 L
"ButtonShadow", /* 16 = COLOR_BTNSHADOW */
334 L
"GrayText", /* 17 = COLOR_GRAYTEXT */
335 L
"ButtonText", /* 18 = COLOR_BTNTEXT */
336 L
"InactiveTitleText", /* 19 = COLOR_INACTIVECAPTIONTEXT */
337 L
"ButtonHilight", /* 20 = COLOR_BTNHIGHLIGHT */
338 L
"ButtonDkShadow", /* 21 = COLOR_3DDKSHADOW */
339 L
"ButtonLight", /* 22 = COLOR_3DLIGHT */
340 L
"InfoText", /* 23 = COLOR_INFOTEXT */
341 L
"InfoWindow", /* 24 = COLOR_INFOBK */
342 L
"ButtonAlternateFace", /* 25 = COLOR_ALTERNATEBTNFACE */
343 L
"HotTrackingColor", /* 26 = COLOR_HOTLIGHT */
344 L
"GradientActiveTitle", /* 27 = COLOR_GRADIENTACTIVECAPTION */
345 L
"GradientInactiveTitle", /* 28 = COLOR_GRADIENTINACTIVECAPTION */
346 L
"MenuHilight", /* 29 = COLOR_MENUHILIGHT */
347 L
"MenuBar" /* 30 = COLOR_MENUBAR */
349 #define NUM_SYSCOLORS (sizeof(g_RegColorNames) / sizeof(g_RegColorNames[0]))
352 COLORREF
StrToColorref(
357 TRACE("(%s)\n", debugstr_w(lpszCol
));
359 rgb
[0] = StrToIntW(lpszCol
);
360 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
361 rgb
[1] = StrToIntW(lpszCol
);
362 lpszCol
= StrChrW(lpszCol
, L
' ') + 1;
363 rgb
[2] = StrToIntW(lpszCol
);
364 return RGB(rgb
[0], rgb
[1], rgb
[2]);
368 VOID
SetUserSysColors(VOID
)
379 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_COLORS
,
380 0, KEY_QUERY_VALUE
, &hKey
);
381 if (rc
!= ERROR_SUCCESS
)
383 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
386 for(i
= 0; i
< NUM_SYSCOLORS
; i
++)
388 Size
= sizeof(szColor
);
389 rc
= RegQueryValueEx(hKey
, g_RegColorNames
[i
], NULL
, &Type
,
390 (LPBYTE
)szColor
, &Size
);
391 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
393 crColor
= StrToColorref(szColor
);
394 SetSysColors(1, &i
, &crColor
);
397 WARN("RegQueryValueEx(%s) failed with error %lu\n",
398 debugstr_w(g_RegColorNames
[i
]), rc
);
404 VOID
LoadUserFontSetting(
405 IN LPWSTR lpValueName
,
413 TRACE("(%s, %p)\n", debugstr_w(lpValueName
), pFont
);
415 Size
= sizeof(LOGFONTW
);
416 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_METRICS
,
417 0, KEY_QUERY_VALUE
, &hKey
);
418 if (rc
!= ERROR_SUCCESS
)
420 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
423 rc
= RegQueryValueEx(hKey
, lpValueName
, NULL
, &Type
, (LPBYTE
)&lfTemp
, &Size
);
424 if (rc
!= ERROR_SUCCESS
|| Type
!= REG_BINARY
)
426 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
430 /* FIXME: Check if lfTemp is a valid font */
435 VOID
LoadUserMetricSetting(
436 IN LPWSTR lpValueName
,
444 TRACE("(%s, %p)\n", debugstr_w(lpValueName
), pValue
);
446 Size
= sizeof(strValue
);
447 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_METRICS
,
448 0, KEY_QUERY_VALUE
, &hKey
);
449 if (rc
!= ERROR_SUCCESS
)
451 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
454 rc
= RegQueryValueEx(hKey
, lpValueName
, NULL
, &Type
, (LPBYTE
)&strValue
, &Size
);
455 if (rc
!= ERROR_SUCCESS
|| Type
!= REG_SZ
)
457 WARN("RegQueryValueEx() failed with error %lu\n", rc
);
461 *pValue
= StrToInt(strValue
);
465 VOID
SetUserMetrics(VOID
)
467 NONCLIENTMETRICSW ncmetrics
;
468 MINIMIZEDMETRICS mmmetrics
;
472 ncmetrics
.cbSize
= sizeof(NONCLIENTMETRICSW
);
473 mmmetrics
.cbSize
= sizeof(MINIMIZEDMETRICS
);
474 SystemParametersInfoW(SPI_GETNONCLIENTMETRICS
, sizeof(NONCLIENTMETRICSW
), &ncmetrics
, 0);
475 SystemParametersInfoW(SPI_GETMINIMIZEDMETRICS
, sizeof(MINIMIZEDMETRICS
), &mmmetrics
, 0);
477 LoadUserFontSetting(L
"CaptionFont", &ncmetrics
.lfCaptionFont
);
478 LoadUserFontSetting(L
"SmCaptionFont", &ncmetrics
.lfSmCaptionFont
);
479 LoadUserFontSetting(L
"MenuFont", &ncmetrics
.lfMenuFont
);
480 LoadUserFontSetting(L
"StatusFont", &ncmetrics
.lfStatusFont
);
481 LoadUserFontSetting(L
"MessageFont", &ncmetrics
.lfMessageFont
);
482 /* FIXME: load icon font ? */
484 LoadUserMetricSetting(L
"BorderWidth", &ncmetrics
.iBorderWidth
);
485 LoadUserMetricSetting(L
"ScrollWidth", &ncmetrics
.iScrollWidth
);
486 LoadUserMetricSetting(L
"ScrollHeight", &ncmetrics
.iScrollHeight
);
487 LoadUserMetricSetting(L
"CaptionWidth", &ncmetrics
.iCaptionWidth
);
488 LoadUserMetricSetting(L
"CaptionHeight", &ncmetrics
.iCaptionHeight
);
489 LoadUserMetricSetting(L
"SmCaptionWidth", &ncmetrics
.iSmCaptionWidth
);
490 LoadUserMetricSetting(L
"SmCaptionHeight", &ncmetrics
.iSmCaptionHeight
);
491 LoadUserMetricSetting(L
"Menuwidth", &ncmetrics
.iMenuWidth
);
492 LoadUserMetricSetting(L
"MenuHeight", &ncmetrics
.iMenuHeight
);
494 SystemParametersInfoW(SPI_SETNONCLIENTMETRICS
, sizeof(NONCLIENTMETRICSW
), &ncmetrics
, 0);
498 VOID
SetUserWallpaper(VOID
)
502 WCHAR szWallpaper
[MAX_PATH
+ 1];
507 rc
= RegOpenKeyEx(HKEY_CURRENT_USER
, REGSTR_PATH_DESKTOP
,
508 0, KEY_QUERY_VALUE
, &hKey
);
509 if (rc
== ERROR_SUCCESS
)
511 Size
= sizeof(szWallpaper
);
512 rc
= RegQueryValueEx(hKey
,
518 if (rc
== ERROR_SUCCESS
&& Type
== REG_SZ
)
520 ExpandEnvironmentStrings(szWallpaper
, szWallpaper
, MAX_PATH
);
521 TRACE("Using wallpaper %s\n", debugstr_w(szWallpaper
));
523 /* Load and change the wallpaper */
524 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, szWallpaper
, SPIF_SENDCHANGE
);
528 /* remove the wallpaper */
529 TRACE("No wallpaper set in registry (error %lu)\n", rc
);
530 SystemParametersInfo(SPI_SETDESKWALLPAPER
, 0, NULL
, SPIF_SENDCHANGE
);
535 WARN("RegOpenKeyEx() failed with error %lu\n", rc
);
539 VOID
SetUserSettings(VOID
)
548 typedef DWORD (WINAPI
*PCMP_REPORT_LOGON
)(DWORD
, DWORD
);
554 PCMP_REPORT_LOGON CMP_Report_LogOn
;
558 hModule
= LoadLibrary(L
"setupapi.dll");
561 CMP_Report_LogOn
= (PCMP_REPORT_LOGON
)GetProcAddress(hModule
, "CMP_Report_LogOn");
562 if (CMP_Report_LogOn
)
563 CMP_Report_LogOn(CMP_MAGIC
, GetCurrentProcessId());
565 WARN("GetProcAddress() failed\n");
567 FreeLibrary(hModule
);
570 WARN("LoadLibrary() failed with error %lu\n", GetLastError());
574 #pragma warning(disable : 4100)
575 #endif /* _MSC_VER */
578 wWinMain(IN HINSTANCE hInst
,
579 IN HINSTANCE hPrevInstance
,
580 IN LPWSTR lpszCmdLine
,