Synchronize with trunk r58528.
[reactos.git] / dll / win32 / syssetup / install.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: System setup
23 * FILE: dll/win32/syssetup/install.c
24 * PROGRAMER: Eric Kohl
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #include "precomp.h"
30
31 #define NDEBUG
32 #include <debug.h>
33
34 DWORD WINAPI
35 CMP_WaitNoPendingInstallEvents(DWORD dwTimeout);
36
37 /* GLOBALS ******************************************************************/
38
39 HINF hSysSetupInf = INVALID_HANDLE_VALUE;
40
41 /* FUNCTIONS ****************************************************************/
42
43 static VOID
44 FatalError(char *pszFmt,...)
45 {
46 char szBuffer[512];
47 va_list ap;
48
49 va_start(ap, pszFmt);
50 vsprintf(szBuffer, pszFmt, ap);
51 va_end(ap);
52
53 LogItem(SYSSETUP_SEVERITY_FATAL_ERROR, L"Failed");
54
55 strcat(szBuffer, "\nRebooting now!");
56 MessageBoxA(NULL,
57 szBuffer,
58 "ReactOS Setup",
59 MB_OK);
60 }
61
62 static HRESULT
63 CreateShellLink(
64 LPCTSTR pszLinkPath,
65 LPCTSTR pszCmd,
66 LPCTSTR pszArg,
67 LPCTSTR pszDir,
68 LPCTSTR pszIconPath,
69 int iIconNr,
70 LPCTSTR pszComment)
71 {
72 IShellLink *psl;
73 IPersistFile *ppf;
74 #ifndef _UNICODE
75 WCHAR wszBuf[MAX_PATH];
76 #endif /* _UNICODE */
77
78 HRESULT hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&psl);
79
80 if (SUCCEEDED(hr))
81 {
82 hr = psl->lpVtbl->SetPath(psl, pszCmd);
83
84 if (pszArg)
85 {
86 hr = psl->lpVtbl->SetArguments(psl, pszArg);
87 }
88
89 if (pszDir)
90 {
91 hr = psl->lpVtbl->SetWorkingDirectory(psl, pszDir);
92 }
93
94 if (pszIconPath)
95 {
96 hr = psl->lpVtbl->SetIconLocation(psl, pszIconPath, iIconNr);
97 }
98
99 if (pszComment)
100 {
101 hr = psl->lpVtbl->SetDescription(psl, pszComment);
102 }
103
104 hr = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
105
106 if (SUCCEEDED(hr))
107 {
108 #ifdef _UNICODE
109 hr = ppf->lpVtbl->Save(ppf, pszLinkPath, TRUE);
110 #else /* _UNICODE */
111 MultiByteToWideChar(CP_ACP, 0, pszLinkPath, -1, wszBuf, MAX_PATH);
112
113 hr = ppf->lpVtbl->Save(ppf, wszBuf, TRUE);
114 #endif /* _UNICODE */
115
116 ppf->lpVtbl->Release(ppf);
117 }
118
119 psl->lpVtbl->Release(psl);
120 }
121
122 return hr;
123 }
124
125
126 static BOOL
127 CreateShortcut(
128 int csidl,
129 LPCTSTR pszFolder,
130 UINT nIdName,
131 LPCTSTR pszCommand,
132 UINT nIdTitle,
133 BOOL bCheckExistence,
134 INT iIconNr)
135 {
136 TCHAR szPath[MAX_PATH];
137 TCHAR szExeName[MAX_PATH];
138 TCHAR szTitle[256];
139 TCHAR szName[256];
140 LPTSTR Ptr = szPath;
141 TCHAR szWorkingDirBuf[MAX_PATH];
142 LPTSTR pszWorkingDir = NULL;
143 LPTSTR lpFilePart;
144 DWORD dwLen;
145
146 if (ExpandEnvironmentStrings(pszCommand,
147 szPath,
148 sizeof(szPath) / sizeof(szPath[0])) == 0)
149 {
150 _tcscpy(szPath, pszCommand);
151 }
152
153 if (bCheckExistence)
154 {
155 if ((_taccess(szPath, 0 )) == -1)
156 /* Expected error, don't return FALSE */
157 return TRUE;
158 }
159
160 dwLen = GetFullPathName(szPath,
161 sizeof(szWorkingDirBuf) / sizeof(szWorkingDirBuf[0]),
162 szWorkingDirBuf,
163 &lpFilePart);
164 if (dwLen != 0 && dwLen <= sizeof(szWorkingDirBuf) / sizeof(szWorkingDirBuf[0]))
165 {
166 /* Since those should only be called with (.exe) files,
167 lpFilePart has not to be NULL */
168 ASSERT(lpFilePart != NULL);
169
170 /* Save the file name */
171 _tcscpy(szExeName, lpFilePart);
172
173 /* We're only interested in the path. Cut the file name off.
174 Also remove the trailing backslash unless the working directory
175 is only going to be a drive, ie. C:\ */
176 *(lpFilePart--) = _T('\0');
177 if (!(lpFilePart - szWorkingDirBuf == 2 && szWorkingDirBuf[1] == _T(':') &&
178 szWorkingDirBuf[2] == _T('\\')))
179 {
180 *lpFilePart = _T('\0');
181 }
182
183 pszWorkingDir = szWorkingDirBuf;
184 }
185
186
187 if (!SHGetSpecialFolderPath(0, szPath, csidl, TRUE))
188 return FALSE;
189
190 if (pszFolder)
191 {
192 Ptr = PathAddBackslash(Ptr);
193 _tcscpy(Ptr, pszFolder);
194 }
195
196 Ptr = PathAddBackslash(Ptr);
197
198 if (!LoadString(hDllInstance, nIdName, szName, sizeof(szName)/sizeof(szName[0])))
199 return FALSE;
200 _tcscpy(Ptr, szName);
201
202 if (!LoadString(hDllInstance, nIdTitle, szTitle, sizeof(szTitle)/sizeof(szTitle[0])))
203 return FALSE;
204
205 // FIXME: we should pass 'command' straight in here, but shell32 doesn't expand it
206 return SUCCEEDED(CreateShellLink(szPath, szExeName, _T(""), pszWorkingDir, szExeName, iIconNr, szTitle));
207 }
208
209 static BOOL
210 CreateShortcutFolder(int csidl, UINT nID, LPTSTR pszName, int cchNameLen)
211 {
212 TCHAR szPath[MAX_PATH];
213 LPTSTR p;
214
215 if (!SHGetSpecialFolderPath(0, szPath, csidl, TRUE))
216 return FALSE;
217
218 if (!LoadString(hDllInstance, nID, pszName, cchNameLen))
219 return FALSE;
220
221 p = PathAddBackslash(szPath);
222 _tcscpy(p, pszName);
223
224 return CreateDirectory(szPath, NULL) || GetLastError()==ERROR_ALREADY_EXISTS;
225 }
226
227
228 static VOID
229 CreateTempDir(
230 IN LPCWSTR VarName)
231 {
232 WCHAR szTempDir[MAX_PATH];
233 WCHAR szBuffer[MAX_PATH];
234 DWORD dwLength;
235 HKEY hKey;
236
237 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
238 L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
239 0,
240 KEY_QUERY_VALUE,
241 &hKey) != ERROR_SUCCESS)
242 {
243 FatalError("Error: %lu\n", GetLastError());
244 return;
245 }
246
247 /* Get temp dir */
248 dwLength = MAX_PATH * sizeof(WCHAR);
249 if (RegQueryValueExW(hKey,
250 VarName,
251 NULL,
252 NULL,
253 (LPBYTE)szBuffer,
254 &dwLength) != ERROR_SUCCESS)
255 {
256 FatalError("Error: %lu\n", GetLastError());
257 goto cleanup;
258 }
259
260 /* Expand it */
261 if (!ExpandEnvironmentStringsW(szBuffer,
262 szTempDir,
263 MAX_PATH))
264 {
265 FatalError("Error: %lu\n", GetLastError());
266 goto cleanup;
267 }
268
269 /* Create profiles directory */
270 if (!CreateDirectoryW(szTempDir, NULL))
271 {
272 if (GetLastError() != ERROR_ALREADY_EXISTS)
273 {
274 FatalError("Error: %lu\n", GetLastError());
275 goto cleanup;
276 }
277 }
278
279 cleanup:
280 RegCloseKey(hKey);
281 }
282
283 static BOOL
284 InstallSysSetupInfDevices(VOID)
285 {
286 INFCONTEXT InfContext;
287 WCHAR szLineBuffer[256];
288 DWORD dwLineLength;
289
290 if (!SetupFindFirstLineW(hSysSetupInf,
291 L"DeviceInfsToInstall",
292 NULL,
293 &InfContext))
294 {
295 return FALSE;
296 }
297
298 do
299 {
300 if (!SetupGetStringFieldW(&InfContext,
301 0,
302 szLineBuffer,
303 sizeof(szLineBuffer)/sizeof(szLineBuffer[0]),
304 &dwLineLength))
305 {
306 return FALSE;
307 }
308
309 if (!SetupDiInstallClassW(NULL, szLineBuffer, DI_QUIETINSTALL, NULL))
310 {
311 return FALSE;
312 }
313 }
314 while (SetupFindNextLine(&InfContext, &InfContext));
315
316 return TRUE;
317 }
318
319 static BOOL
320 InstallSysSetupInfComponents(VOID)
321 {
322 INFCONTEXT InfContext;
323 WCHAR szNameBuffer[256];
324 WCHAR szSectionBuffer[256];
325 HINF hComponentInf = INVALID_HANDLE_VALUE;
326
327 if (!SetupFindFirstLineW(hSysSetupInf,
328 L"Infs.Always",
329 NULL,
330 &InfContext))
331 {
332 DPRINT("No Inf.Always section found\n");
333 }
334 else
335 {
336 do
337 {
338 if (!SetupGetStringFieldW(&InfContext,
339 1, // Get the component name
340 szNameBuffer,
341 sizeof(szNameBuffer)/sizeof(szNameBuffer[0]),
342 NULL))
343 {
344 FatalError("Error while trying to get component name \n");
345 return FALSE;
346 }
347
348 if (!SetupGetStringFieldW(&InfContext,
349 2, // Get the component install section
350 szSectionBuffer,
351 sizeof(szSectionBuffer)/sizeof(szSectionBuffer[0]),
352 NULL))
353 {
354 FatalError("Error while trying to get component install section \n");
355 return FALSE;
356 }
357
358 DPRINT("Trying to execute install section '%S' from '%S' \n", szSectionBuffer, szNameBuffer);
359
360 hComponentInf = SetupOpenInfFileW(szNameBuffer,
361 NULL,
362 INF_STYLE_WIN4,
363 NULL);
364
365 if (hComponentInf == INVALID_HANDLE_VALUE)
366 {
367 FatalError("SetupOpenInfFileW() failed to open '%S' (Error: %lu)\n", szNameBuffer, GetLastError());
368 return FALSE;
369 }
370
371 if (!SetupInstallFromInfSectionW(NULL,
372 hComponentInf,
373 szSectionBuffer,
374 SPINST_ALL,
375 NULL,
376 NULL,
377 SP_COPY_NEWER,
378 SetupDefaultQueueCallbackW,
379 NULL,
380 NULL,
381 NULL))
382 {
383 FatalError("Error while trying to install : %S (Error: %lu)\n", szNameBuffer, GetLastError());
384 SetupCloseInfFile(hComponentInf);
385 return FALSE;
386 }
387
388 SetupCloseInfFile(hComponentInf);
389 }
390 while (SetupFindNextLine(&InfContext, &InfContext));
391 }
392
393 return TRUE;
394 }
395
396 static BOOL
397 EnableUserModePnpManager(VOID)
398 {
399 SC_HANDLE hSCManager = NULL;
400 SC_HANDLE hService = NULL;
401 BOOL bRet = FALSE;
402
403 hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
404 if (hSCManager == NULL)
405 {
406 DPRINT1("Unable to open the service control manager.\n");
407 DPRINT1("Last Error %d\n", GetLastError());
408 goto cleanup;
409 }
410
411 hService = OpenServiceW(hSCManager,
412 L"PlugPlay",
413 SERVICE_CHANGE_CONFIG | SERVICE_START);
414 if (hService == NULL)
415 {
416 DPRINT1("Unable to open PlugPlay service\n");
417 goto cleanup;
418 }
419
420 bRet = ChangeServiceConfigW(hService,
421 SERVICE_NO_CHANGE,
422 SERVICE_AUTO_START,
423 SERVICE_NO_CHANGE,
424 NULL, NULL, NULL,
425 NULL, NULL, NULL, NULL);
426 if (!bRet)
427 {
428 DPRINT1("Unable to change the service configuration\n");
429 goto cleanup;
430 }
431
432 bRet = StartServiceW(hService, 0, NULL);
433 if (!bRet && (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING))
434 {
435 DPRINT1("Unable to start service\n");
436 goto cleanup;
437 }
438
439 bRet = TRUE;
440
441 cleanup:
442 if (hService != NULL)
443 CloseServiceHandle(hService);
444 if (hSCManager != NULL)
445 CloseServiceHandle(hSCManager);
446 return bRet;
447 }
448
449 static INT_PTR CALLBACK
450 StatusMessageWindowProc(
451 IN HWND hwndDlg,
452 IN UINT uMsg,
453 IN WPARAM wParam,
454 IN LPARAM lParam)
455 {
456 UNREFERENCED_PARAMETER(wParam);
457
458 switch (uMsg)
459 {
460 case WM_INITDIALOG:
461 {
462 WCHAR szMsg[256];
463
464 if (!LoadStringW(hDllInstance, IDS_STATUS_INSTALL_DEV, szMsg, sizeof(szMsg)/sizeof(szMsg[0])))
465 return FALSE;
466 SetDlgItemTextW(hwndDlg, IDC_STATUSLABEL, szMsg);
467 return TRUE;
468 }
469 }
470 return FALSE;
471 }
472
473 static DWORD WINAPI
474 ShowStatusMessageThread(
475 IN LPVOID lpParameter)
476 {
477 HWND *phWnd = (HWND *)lpParameter;
478 HWND hWnd;
479 MSG Msg;
480
481 hWnd = CreateDialogParam(
482 hDllInstance,
483 MAKEINTRESOURCE(IDD_STATUSWINDOW_DLG),
484 GetDesktopWindow(),
485 StatusMessageWindowProc,
486 (LPARAM)NULL);
487 if (!hWnd)
488 return 0;
489 *phWnd = hWnd;
490
491 ShowWindow(hWnd, SW_SHOW);
492
493 /* Message loop for the Status window */
494 while (GetMessage(&Msg, NULL, 0, 0))
495 {
496 TranslateMessage(&Msg);
497 DispatchMessage(&Msg);
498 }
499
500 return 0;
501 }
502
503 static LONG
504 ReadRegSzKey(
505 IN HKEY hKey,
506 IN LPCWSTR pszKey,
507 OUT LPWSTR* pValue)
508 {
509 LONG rc;
510 DWORD dwType;
511 DWORD cbData = 0;
512 LPWSTR pwszValue;
513
514 if (!pValue)
515 return ERROR_INVALID_PARAMETER;
516
517 *pValue = NULL;
518 rc = RegQueryValueExW(hKey, pszKey, NULL, &dwType, NULL, &cbData);
519 if (rc != ERROR_SUCCESS)
520 return rc;
521 if (dwType != REG_SZ)
522 return ERROR_FILE_NOT_FOUND;
523 pwszValue = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
524 if (!pwszValue)
525 return ERROR_NOT_ENOUGH_MEMORY;
526 rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)pwszValue, &cbData);
527 if (rc != ERROR_SUCCESS)
528 {
529 HeapFree(GetProcessHeap(), 0, pwszValue);
530 return rc;
531 }
532 /* NULL-terminate the string */
533 pwszValue[cbData / sizeof(WCHAR)] = '\0';
534
535 *pValue = pwszValue;
536 return ERROR_SUCCESS;
537 }
538
539 static BOOL
540 IsConsoleBoot(VOID)
541 {
542 HKEY hControlKey = NULL;
543 LPWSTR pwszSystemStartOptions = NULL;
544 LPWSTR pwszCurrentOption, pwszNextOption; /* Pointers into SystemStartOptions */
545 BOOL bConsoleBoot = FALSE;
546 LONG rc;
547
548 rc = RegOpenKeyExW(
549 HKEY_LOCAL_MACHINE,
550 L"SYSTEM\\CurrentControlSet\\Control",
551 0,
552 KEY_QUERY_VALUE,
553 &hControlKey);
554 if (rc != ERROR_SUCCESS)
555 goto cleanup;
556
557 rc = ReadRegSzKey(hControlKey, L"SystemStartOptions", &pwszSystemStartOptions);
558 if (rc != ERROR_SUCCESS)
559 goto cleanup;
560
561 /* Check for CONSOLE switch in SystemStartOptions */
562 pwszCurrentOption = pwszSystemStartOptions;
563 while (pwszCurrentOption)
564 {
565 pwszNextOption = wcschr(pwszCurrentOption, L' ');
566 if (pwszNextOption)
567 *pwszNextOption = L'\0';
568 if (wcsicmp(pwszCurrentOption, L"CONSOLE") == 0)
569 {
570 DPRINT("Found %S. Switching to console boot\n", pwszCurrentOption);
571 bConsoleBoot = TRUE;
572 goto cleanup;
573 }
574 pwszCurrentOption = pwszNextOption ? pwszNextOption + 1 : NULL;
575 }
576
577 cleanup:
578 if (hControlKey != NULL)
579 RegCloseKey(hControlKey);
580 if (pwszSystemStartOptions)
581 HeapFree(GetProcessHeap(), 0, pwszSystemStartOptions);
582 return bConsoleBoot;
583 }
584
585 static BOOL
586 CommonInstall(VOID)
587 {
588 HWND hWnd = NULL;
589
590 hSysSetupInf = SetupOpenInfFileW(
591 L"syssetup.inf",
592 NULL,
593 INF_STYLE_WIN4,
594 NULL);
595 if (hSysSetupInf == INVALID_HANDLE_VALUE)
596 {
597 FatalError("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
598 return FALSE;
599 }
600
601 if (!InstallSysSetupInfDevices())
602 {
603 FatalError("InstallSysSetupInfDevices() failed!\n");
604 goto error;
605 }
606
607 if(!InstallSysSetupInfComponents())
608 {
609 FatalError("InstallSysSetupInfComponents() failed!\n");
610 goto error;
611 }
612
613 if (!IsConsoleBoot())
614 {
615 HANDLE hThread;
616
617 hThread = CreateThread(
618 NULL,
619 0,
620 ShowStatusMessageThread,
621 (LPVOID)&hWnd,
622 0,
623 NULL);
624
625 if (hThread)
626 CloseHandle(hThread);
627 }
628
629 if (!EnableUserModePnpManager())
630 {
631 FatalError("EnableUserModePnpManager() failed!\n");
632 goto error;
633 }
634
635 if (CMP_WaitNoPendingInstallEvents(INFINITE) != WAIT_OBJECT_0)
636 {
637 FatalError("CMP_WaitNoPendingInstallEvents() failed!\n");
638 goto error;
639 }
640
641 EndDialog(hWnd, 0);
642 return TRUE;
643
644 error:
645 if (hWnd)
646 EndDialog(hWnd, 0);
647 SetupCloseInfFile(hSysSetupInf);
648 return FALSE;
649 }
650
651 DWORD WINAPI
652 InstallLiveCD(IN HINSTANCE hInstance)
653 {
654 STARTUPINFOW StartupInfo;
655 PROCESS_INFORMATION ProcessInformation;
656 BOOL bRes;
657
658 if (!CommonInstall())
659 goto error;
660
661 /* Register components */
662 _SEH2_TRY
663 {
664 if (!SetupInstallFromInfSectionW(NULL,
665 hSysSetupInf, L"RegistrationPhase2",
666 SPINST_ALL,
667 0, NULL, 0, NULL, NULL, NULL, NULL))
668 {
669 DPRINT1("SetupInstallFromInfSectionW failed!\n");
670 }
671 }
672 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
673 {
674 DPRINT1("Catching exception\n");
675 }
676 _SEH2_END;
677
678 SetupCloseInfFile(hSysSetupInf);
679
680 /* Run the shell */
681 ZeroMemory(&StartupInfo, sizeof(StartupInfo));
682 StartupInfo.cb = sizeof(StartupInfo);
683 bRes = CreateProcessW(
684 L"userinit.exe",
685 NULL,
686 NULL,
687 NULL,
688 FALSE,
689 0,
690 NULL,
691 NULL,
692 &StartupInfo,
693 &ProcessInformation);
694 if (!bRes)
695 goto error;
696
697 CloseHandle(ProcessInformation.hThread);
698 CloseHandle(ProcessInformation.hProcess);
699
700 return 0;
701
702 error:
703 MessageBoxW(
704 NULL,
705 L"Failed to load LiveCD! You can shutdown your computer, or press ENTER to reboot.",
706 L"ReactOS LiveCD",
707 MB_OK);
708 return 0;
709 }
710
711
712 static BOOL
713 CreateShortcuts(VOID)
714 {
715 TCHAR szFolder[256];
716
717 CoInitialize(NULL);
718
719 /* Create desktop shortcuts */
720 CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE, 0);
721
722 /* Create program startmenu shortcuts */
723 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_EXPLORER, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER, TRUE, 1);
724 CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\rapps.exe"), IDS_CMT_DOWNLOADER, TRUE, 0);
725
726 /* Create administrative tools startmenu shortcuts */
727 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_SERVICE, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN, TRUE, 0);
728 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_DEVICE, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT, TRUE, 0);
729 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_EVENTVIEW, _T("%SystemRoot%\\system32\\eventvwr.exe"), IDS_CMT_EVENTVIEW, TRUE, 0);
730 CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_MSCONFIG, _T("%SystemRoot%\\system32\\msconfig.exe"), IDS_CMT_MSCONFIG, TRUE, 0);
731
732 /* Create and fill Accessories subfolder */
733 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_ACCESSORIES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
734 {
735 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CALC, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC, TRUE, 0);
736 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE, 0);
737 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_NOTEPAD, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD, TRUE, 0);
738 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_RDESKTOP, _T("%SystemRoot%\\system32\\mstsc.exe"), IDS_CMT_RDESKTOP, TRUE, 0);
739 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNAP, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT, TRUE, 0);
740 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WORDPAD, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD, TRUE, 0);
741 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_PAINT, _T("%SystemRoot%\\system32\\mspaint.exe"), IDS_CMT_PAINT, TRUE, 0);
742 }
743
744 /* Create System Tools subfolder and fill if the exe is available */
745 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_TOOLS, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
746 {
747 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CHARMAP, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP, TRUE, 0);
748 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_KBSWITCH, _T("%SystemRoot%\\system32\\kbswitch.exe"), IDS_CMT_KBSWITCH, TRUE, 0);
749 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_REGEDIT, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT, TRUE, 0);
750 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_DXDIAG, _T("%SystemRoot%\\system32\\dxdiag.exe"), IDS_CMT_DXDIAG, TRUE, 0);
751 }
752
753 /* Create Accessibility subfolder and fill if the exe is available */
754 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ACCESSIBILITY, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
755 {
756 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MAGNIFY, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY, TRUE, 0);
757 }
758
759 /* Create Entertainment subfolder and fill if the exe is available */
760 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ENTERTAINMENT, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
761 {
762 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MPLAY32, _T("%SystemRoot%\\system32\\mplay32.exe"), IDS_CMT_MPLAY32, TRUE, 0);
763 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDVOL32, _T("%SystemRoot%\\system32\\sndvol32.exe"), IDS_CMT_SNDVOL32, TRUE, 0);
764 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDREC32, _T("%SystemRoot%\\system32\\sndrec32.exe"), IDS_CMT_SNDREC32, TRUE, 0);
765 }
766
767 /* Create Games subfolder and fill if the exe is available */
768 if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_GAMES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
769 {
770 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SOLITAIRE, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE, TRUE, 0);
771 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WINEMINE, _T("%SystemRoot%\\system32\\winmine.exe"), IDS_CMT_WINEMINE, TRUE, 0);
772 CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SPIDER, _T("%SystemRoot%\\system32\\spider.exe"), IDS_CMT_SPIDER, TRUE, 0);
773 }
774
775 CoUninitialize();
776
777 return TRUE;
778 }
779
780 static BOOL
781 SetSetupType(DWORD dwSetupType)
782 {
783 DWORD dwError;
784 HKEY hKey;
785
786 dwError = RegOpenKeyExW(
787 HKEY_LOCAL_MACHINE,
788 L"SYSTEM\\Setup",
789 0,
790 KEY_SET_VALUE,
791 &hKey);
792 if (dwError != ERROR_SUCCESS)
793 return FALSE;
794
795 dwError = RegSetValueExW(
796 hKey,
797 L"SetupType",
798 0,
799 REG_DWORD,
800 (LPBYTE)&dwSetupType,
801 sizeof(DWORD));
802 RegCloseKey(hKey);
803 if (dwError != ERROR_SUCCESS)
804 return FALSE;
805
806 return TRUE;
807 }
808
809 DWORD WINAPI
810 InstallReactOS(HINSTANCE hInstance)
811 {
812 TCHAR szBuffer[MAX_PATH];
813 HANDLE token;
814 TOKEN_PRIVILEGES privs;
815 HKEY hKey;
816
817 InitializeSetupActionLog(FALSE);
818 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS");
819
820 if (!InitializeProfiles())
821 {
822 FatalError("InitializeProfiles() failed");
823 return 0;
824 }
825
826 CreateTempDir(L"TEMP");
827 CreateTempDir(L"TMP");
828
829 if (GetWindowsDirectory(szBuffer, sizeof(szBuffer) / sizeof(TCHAR)))
830 {
831 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
832 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
833 0,
834 KEY_WRITE,
835 &hKey) == ERROR_SUCCESS)
836 {
837 RegSetValueExW(hKey,
838 L"PathName",
839 0,
840 REG_SZ,
841 (LPBYTE)szBuffer,
842 (wcslen(szBuffer) + 1) * sizeof(WCHAR));
843
844 RegSetValueExW(hKey,
845 L"SystemRoot",
846 0,
847 REG_SZ,
848 (LPBYTE)szBuffer,
849 (wcslen(szBuffer) + 1) * sizeof(WCHAR));
850
851 RegCloseKey(hKey);
852 }
853
854 PathAddBackslash(szBuffer);
855 _tcscat(szBuffer, _T("system"));
856 CreateDirectory(szBuffer, NULL);
857 }
858
859 if (!CommonInstall())
860 return 0;
861
862 InstallWizard();
863
864 InstallSecurity();
865
866 if (!CreateShortcuts())
867 {
868 FatalError("CreateShortcuts() failed");
869 return 0;
870 }
871
872 /* ROS HACK, as long as NtUnloadKey is not implemented */
873 {
874 NTSTATUS Status = NtUnloadKey(NULL);
875 if (Status == STATUS_NOT_IMPLEMENTED)
876 {
877 /* Create the Administrator profile */
878 PROFILEINFOW ProfileInfo;
879 HANDLE hToken;
880 BOOL ret;
881
882 ret = LogonUserW(L"Administrator", L"", L"", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken);
883 if (!ret)
884 {
885 FatalError("LogonUserW() failed!");
886 return 0;
887 }
888 ZeroMemory(&ProfileInfo, sizeof(PROFILEINFOW));
889 ProfileInfo.dwSize = sizeof(PROFILEINFOW);
890 ProfileInfo.lpUserName = L"Administrator";
891 ProfileInfo.dwFlags = PI_NOUI;
892 LoadUserProfileW(hToken, &ProfileInfo);
893 CloseHandle(hToken);
894 }
895 else
896 {
897 DPRINT1("ROS HACK not needed anymore. Please remove it\n");
898 }
899 }
900 /* END OF ROS HACK */
901
902 SetupCloseInfFile(hSysSetupInf);
903 SetSetupType(0);
904
905 LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS done");
906 TerminateSetupActionLog();
907
908 /* Get shutdown privilege */
909 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
910 {
911 FatalError("OpenProcessToken() failed!");
912 return 0;
913 }
914 if (!LookupPrivilegeValue(
915 NULL,
916 SE_SHUTDOWN_NAME,
917 &privs.Privileges[0].Luid))
918 {
919 FatalError("LookupPrivilegeValue() failed!");
920 return 0;
921 }
922 privs.PrivilegeCount = 1;
923 privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
924 if (AdjustTokenPrivileges(
925 token,
926 FALSE,
927 &privs,
928 0,
929 (PTOKEN_PRIVILEGES)NULL,
930 NULL) == 0)
931 {
932 FatalError("AdjustTokenPrivileges() failed!");
933 return 0;
934 }
935
936 ExitWindowsEx(EWX_REBOOT, 0);
937 return 0;
938 }
939
940
941 /*
942 * @unimplemented
943 */
944 DWORD WINAPI
945 SetupChangeFontSize(
946 IN HANDLE hWnd,
947 IN LPCWSTR lpszFontSize)
948 {
949 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
950 return FALSE;
951 }
952
953 /*
954 * @unimplemented
955 */
956 DWORD WINAPI
957 SetupChangeLocaleEx(HWND hWnd,
958 LCID Lcid,
959 LPCWSTR lpSrcRootPath,
960 char Unknown,
961 DWORD dwUnused1,
962 DWORD dwUnused2)
963 {
964 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
965 return FALSE;
966 }
967
968 /*
969 * @implemented
970 */
971 DWORD WINAPI
972 SetupChangeLocale(HWND hWnd, LCID Lcid)
973 {
974 return SetupChangeLocaleEx(hWnd, Lcid, NULL, 0, 0, 0);
975 }