3 * Copyright (C) 2003 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 system libraries
22 * PURPOSE: System setup
23 * FILE: lib/syssetup/install.c
24 * PROGRAMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
29 #define WIN32_NO_STATUS
31 #define NTOS_MODE_USER
32 #include <ndk/ntndk.h>
40 #include <samlib/samlib.h>
41 #include <syssetup/syssetup.h>
55 CMP_WaitNoPendingInstallEvents(DWORD dwTimeout
);
57 /* GLOBALS ******************************************************************/
59 PSID DomainSid
= NULL
;
62 HINF hSysSetupInf
= INVALID_HANDLE_VALUE
;
64 /* FUNCTIONS ****************************************************************/
67 DebugPrint(char* fmt
,...)
73 vsprintf(buffer
, fmt
, ap
);
76 LogItem(SYSSETUP_SEVERITY_FATAL_ERROR
, L
"Failed");
78 strcat(buffer
, "\nRebooting now!");
86 HRESULT
CreateShellLink(LPCTSTR linkPath
, LPCTSTR cmd
, LPCTSTR arg
, LPCTSTR dir
, LPCTSTR iconPath
, int icon_nr
, LPCTSTR comment
)
91 WCHAR buffer
[MAX_PATH
];
94 HRESULT hr
= CoCreateInstance(&CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLink
, (LPVOID
*)&psl
);
98 hr
= psl
->lpVtbl
->SetPath(psl
, cmd
);
102 hr
= psl
->lpVtbl
->SetArguments(psl
, arg
);
107 hr
= psl
->lpVtbl
->SetWorkingDirectory(psl
, dir
);
112 hr
= psl
->lpVtbl
->SetIconLocation(psl
, iconPath
, icon_nr
);
117 hr
= psl
->lpVtbl
->SetDescription(psl
, comment
);
120 hr
= psl
->lpVtbl
->QueryInterface(psl
, &IID_IPersistFile
, (LPVOID
*)&ppf
);
125 hr
= ppf
->lpVtbl
->Save(ppf
, linkPath
, TRUE
);
127 MultiByteToWideChar(CP_ACP
, 0, linkPath
, -1, buffer
, MAX_PATH
);
129 hr
= ppf
->lpVtbl
->Save(ppf
, buffer
, TRUE
);
130 #endif /* _UNICODE */
132 ppf
->lpVtbl
->Release(ppf
);
135 psl
->lpVtbl
->Release(psl
);
143 CreateShortcut(int csidl
, LPCTSTR folder
, UINT nIdName
, LPCTSTR command
, UINT nIdTitle
, BOOL bCheckExistence
)
145 TCHAR path
[MAX_PATH
];
146 TCHAR exeName
[MAX_PATH
];
150 TCHAR szWorkingDir
[MAX_PATH
];
151 LPTSTR lpWorkingDir
= NULL
;
155 if (ExpandEnvironmentStrings(command
,
157 sizeof(path
) / sizeof(path
[0])) == 0)
165 if ((_taccess(path
, 0 )) == -1)
166 /* Expected error, don't return FALSE */
170 dwLen
= GetFullPathName(path
,
171 sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]),
174 if (dwLen
!= 0 && dwLen
<= sizeof(szWorkingDir
) / sizeof(szWorkingDir
[0]))
176 /* Save the file name */
177 _tcscpy(exeName
, lpFilePart
);
179 if (lpFilePart
!= NULL
)
181 /* We're only interested in the path. Cut the file name off.
182 Also remove the trailing backslash unless the working directory
183 is only going to be a drive, ie. C:\ */
184 *(lpFilePart
--) = _T('\0');
185 if (!(lpFilePart
- szWorkingDir
== 2 && szWorkingDir
[1] == _T(':') &&
186 szWorkingDir
[2] == _T('\\')))
188 *lpFilePart
= _T('\0');
192 lpWorkingDir
= szWorkingDir
;
196 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
201 p
= PathAddBackslash(p
);
205 p
= PathAddBackslash(p
);
207 if (!LoadString(hDllInstance
, nIdName
, name
, sizeof(name
)/sizeof(name
[0])))
211 if (!LoadString(hDllInstance
, nIdTitle
, title
, sizeof(title
)/sizeof(title
[0])))
214 // FIXME: we should pass 'command' straight in here, but shell32 doesn't
215 // expand it, and explorer uses ShellExecuteEx anyway instead of calling
216 // shell32's STDMETHODCALLTYPE Resolve / IShellLinkW_fnResolve
217 return SUCCEEDED(CreateShellLink(path
, exeName
, _T(""), lpWorkingDir
, NULL
, 0, title
));
222 CreateShortcutFolder(int csidl
, UINT nID
, LPTSTR name
, int nameLen
)
224 TCHAR path
[MAX_PATH
];
227 if (!SHGetSpecialFolderPath(0, path
, csidl
, TRUE
))
230 if (!LoadString(hDllInstance
, nID
, name
, nameLen
))
233 p
= PathAddBackslash(path
);
236 return CreateDirectory(path
, NULL
) || GetLastError()==ERROR_ALREADY_EXISTS
;
244 SID_IDENTIFIER_AUTHORITY SystemAuthority
= {SECURITY_NT_AUTHORITY
};
245 LARGE_INTEGER SystemTime
;
249 NtQuerySystemTime(&SystemTime
);
250 Seed
= &SystemTime
.u
.LowPart
;
252 Status
= RtlAllocateAndInitializeSid(
255 SECURITY_NT_NON_UNIQUE
,
264 return NT_SUCCESS(Status
);
274 ULONG Rid
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
278 RidCount
= *RtlSubAuthorityCountSid (Src
);
280 for (i
= 0; i
< RidCount
; i
++)
281 Rid
[i
] = *RtlSubAuthoritySid (Src
, i
);
285 Rid
[RidCount
] = NewRid
;
289 RtlAllocateAndInitializeSid(
290 RtlIdentifierAuthoritySid(Src
),
308 TCHAR szTempDir
[MAX_PATH
];
309 TCHAR szBuffer
[MAX_PATH
];
313 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
314 _T("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
319 DebugPrint("Error: %lu\n", GetLastError());
324 dwLength
= MAX_PATH
* sizeof(TCHAR
);
325 if (RegQueryValueEx(hKey
,
332 DebugPrint("Error: %lu\n", GetLastError());
338 if (!ExpandEnvironmentStrings(szBuffer
,
342 DebugPrint("Error: %lu\n", GetLastError());
347 /* Create profiles directory */
348 if (!CreateDirectory(szTempDir
, NULL
))
350 if (GetLastError() != ERROR_ALREADY_EXISTS
)
352 DebugPrint("Error: %lu\n", GetLastError());
363 ProcessSysSetupInf(VOID
)
365 INFCONTEXT InfContext
;
366 TCHAR LineBuffer
[256];
369 if (!SetupFindFirstLine(hSysSetupInf
,
370 _T("DeviceInfsToInstall"),
379 if (!SetupGetStringField(&InfContext
,
382 sizeof(LineBuffer
)/sizeof(LineBuffer
[0]),
388 if (!SetupDiInstallClass(NULL
, LineBuffer
, DI_QUIETINSTALL
, NULL
))
393 while (SetupFindNextLine(&InfContext
, &InfContext
));
400 EnableUserModePnpManager(VOID
)
402 SC_HANDLE hSCManager
= NULL
;
403 SC_HANDLE hService
= NULL
;
406 hSCManager
= OpenSCManager(NULL
, NULL
, 0);
407 if (hSCManager
== NULL
)
410 hService
= OpenService(hSCManager
, _T("PlugPlay"), SERVICE_CHANGE_CONFIG
| SERVICE_START
);
411 if (hService
== NULL
)
414 ret
= ChangeServiceConfig(
416 SERVICE_NO_CHANGE
, SERVICE_AUTO_START
, SERVICE_NO_CHANGE
,
417 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
421 ret
= StartService(hService
, 0, NULL
);
428 if (hSCManager
!= NULL
)
429 CloseServiceHandle(hSCManager
);
430 if (hService
!= NULL
)
431 CloseServiceHandle(hService
);
437 StatusMessageWindowProc(
443 UNREFERENCED_PARAMETER(wParam
);
451 if (!LoadString(hDllInstance
, IDS_STATUS_INSTALL_DEV
, szMsg
, sizeof(szMsg
)/sizeof(szMsg
[0])))
453 SetDlgItemText(hwndDlg
, IDC_STATUSLABEL
, szMsg
);
462 ShowStatusMessageThread(
463 IN LPVOID lpParameter
)
465 HWND
*phWnd
= (HWND
*)lpParameter
;
469 hWnd
= CreateDialogParam(
471 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG
),
473 StatusMessageWindowProc
,
479 ShowWindow(hWnd
, SW_SHOW
);
481 /* Message loop for the Status window */
482 while (GetMessage(&Msg
, NULL
, 0, 0))
484 TranslateMessage(&Msg
);
485 DispatchMessage(&Msg
);
496 hSysSetupInf
= SetupOpenInfFileW(
501 if (hSysSetupInf
== INVALID_HANDLE_VALUE
)
503 DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
507 if (!ProcessSysSetupInf())
509 DebugPrint("ProcessSysSetupInf() failed!\n");
510 SetupCloseInfFile(hSysSetupInf
);
517 ShowStatusMessageThread
,
522 if (!EnableUserModePnpManager())
524 DebugPrint("EnableUserModePnpManager() failed!\n");
525 SetupCloseInfFile(hSysSetupInf
);
530 if (CMP_WaitNoPendingInstallEvents(INFINITE
) != WAIT_OBJECT_0
)
532 DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
533 SetupCloseInfFile(hSysSetupInf
);
543 InstallLiveCD(IN HINSTANCE hInstance
)
545 STARTUPINFO StartupInfo
;
546 PROCESS_INFORMATION ProcessInformation
;
549 if (!CommonInstall())
551 SetupCloseInfFile(hSysSetupInf
);
554 StartupInfo
.cb
= sizeof(StartupInfo
);
555 StartupInfo
.lpReserved
= NULL
;
556 StartupInfo
.lpDesktop
= NULL
;
557 StartupInfo
.lpTitle
= NULL
;
558 StartupInfo
.dwFlags
= 0;
559 StartupInfo
.cbReserved2
= 0;
560 StartupInfo
.lpReserved2
= 0;
571 &ProcessInformation
);
575 /* Wait for process termination */
576 WaitForSingleObject(ProcessInformation
.hProcess
, INFINITE
);
581 "You can shutdown your computer, or press ENTER to reboot",
589 CreateShortcuts(VOID
)
595 /* Create desktop shortcuts */
596 CreateShortcut(CSIDL_DESKTOP
, NULL
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
598 /* Create program startmenu shortcuts */
599 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_EXPLORER
, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER
, FALSE
);
600 CreateShortcut(CSIDL_PROGRAMS
, NULL
, IDS_SHORT_DOWNLOADER
, _T("%SystemRoot%\\system32\\downloader.exe"), IDS_CMT_DOWNLOADER
, TRUE
);
602 /* Create administrative tools startmenu shortcuts */
603 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_SERVICE
, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN
, FALSE
);
604 CreateShortcut(CSIDL_COMMON_ADMINTOOLS
, NULL
, IDS_SHORT_DEVICE
, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT
, FALSE
);
606 /* Create and fill Accessories subfolder */
607 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_ACCESSORIES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
609 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CALC
, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC
, FALSE
);
610 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_CMD
, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD
, FALSE
);
611 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_NOTEPAD
, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD
, FALSE
);
612 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_REGEDIT
, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT
, FALSE
);
613 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WORDPAD
, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD
, FALSE
);
614 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SNAP
, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT
, TRUE
);
617 /* Create Games subfolder and fill if the exe is available */
618 if (CreateShortcutFolder(CSIDL_PROGRAMS
, IDS_GAMES
, szFolder
, sizeof(szFolder
)/sizeof(szFolder
[0])))
620 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_SOLITAIRE
, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE
, FALSE
);
621 CreateShortcut(CSIDL_PROGRAMS
, szFolder
, IDS_SHORT_WINEMINE
, _T("%SystemRoot%\\system32\\winemine.exe"), IDS_CMT_WINEMINE
, FALSE
);
630 InstallReactOS(HINSTANCE hInstance
)
632 TCHAR szBuffer
[MAX_PATH
];
635 InitializeSetupActionLog(FALSE
);
636 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS");
638 /* Set user langage to the system language */
639 SetUserDefaultLCID(GetSystemDefaultLCID());
640 SetThreadLocale(GetSystemDefaultLCID());
642 if (!InitializeProfiles())
644 DebugPrint("InitializeProfiles() failed");
648 if (!CreateShortcuts())
650 DebugPrint("InitializeProfiles() failed");
654 /* Initialize the Security Account Manager (SAM) */
655 if (!SamInitializeSAM())
657 DebugPrint("SamInitializeSAM() failed!");
661 /* Create the semi-random Domain-SID */
662 if (!CreateRandomSid(&DomainSid
))
664 DebugPrint("Domain-SID creation failed!");
668 /* Set the Domain SID (aka Computer SID) */
669 if (!SamSetDomainSid(DomainSid
))
671 DebugPrint("SamSetDomainSid() failed!");
672 RtlFreeSid(DomainSid
);
676 /* Append the Admin-RID */
677 AppendRidToSid(&AdminSid
, DomainSid
, DOMAIN_USER_RID_ADMIN
);
679 /* Create the Administrator account */
680 if (!SamCreateUser(L
"Administrator", L
"", AdminSid
))
682 /* Check what the error was.
683 * If the Admin Account already exists, then it means Setup
684 * wasn't allowed to finish properly. Instead of rebooting
685 * and not completing it, let it restart instead
687 LastError
= GetLastError();
688 if (LastError
!= ERROR_USER_EXISTS
)
690 DebugPrint("SamCreateUser() failed!");
691 RtlFreeSid(AdminSid
);
692 RtlFreeSid(DomainSid
);
697 /* Create the Administrator profile */
698 if (!CreateUserProfileW(AdminSid
, L
"Administrator"))
700 DebugPrint("CreateUserProfileW() failed!");
701 RtlFreeSid(AdminSid
);
702 RtlFreeSid(DomainSid
);
706 RtlFreeSid(AdminSid
);
707 RtlFreeSid(DomainSid
);
709 CreateTempDir(L
"TEMP");
710 CreateTempDir(L
"TMP");
712 if (GetWindowsDirectory(szBuffer
, sizeof(szBuffer
) / sizeof(TCHAR
)))
714 PathAddBackslash(szBuffer
);
715 _tcscat(szBuffer
, _T("system"));
716 CreateDirectory(szBuffer
, NULL
);
719 if (!CommonInstall())
724 SetupCloseInfFile(hSysSetupInf
);
726 LogItem(SYSSETUP_SEVERITY_INFORMATION
, L
"Installing ReactOS done");
727 TerminateSetupActionLog();
729 /// THE FOLLOWING DPRINT IS FOR THE SYSTEM REGRESSION TOOL
731 DbgPrint("SYSREG_CHECKPOINT:SYSSETUP_COMPLETE\n");
743 IN LPCWSTR lpszFontSize
)
745 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);