[SYSSETUP]
[reactos.git] / reactos / dll / win32 / syssetup / install.c
index 5980dca..ce5024a 100644 (file)
 
 /* INCLUDES *****************************************************************/
 
-#define WIN32_NO_STATUS
-#include <windows.h>
-#define NTOS_MODE_USER
-#include <ndk/ntndk.h>
+#include "precomp.h"
 
-#include <commctrl.h>
-#include <stdio.h>
-#include <io.h>
-#include <tchar.h>
-#include <stdlib.h>
-
-#include <samlib/samlib.h>
-#include <syssetup/syssetup.h>
-#include <userenv.h>
-#include <setupapi.h>
-
-#include <shlobj.h>
-#include <objidl.h>
-#include <shlwapi.h>
-
-#include "globals.h"
-#include "resource.h"
+#include <rpcproxy.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -57,65 +38,70 @@ CMP_WaitNoPendingInstallEvents(DWORD dwTimeout);
 
 /* GLOBALS ******************************************************************/
 
-PSID DomainSid = NULL;
-PSID AdminSid = NULL;
-
 HINF hSysSetupInf = INVALID_HANDLE_VALUE;
+ADMIN_INFO AdminInfo;
 
 /* FUNCTIONS ****************************************************************/
 
 static VOID
-DebugPrint(char* fmt,...)
+FatalError(char *pszFmt,...)
 {
-    char buffer[512];
+    char szBuffer[512];
     va_list ap;
 
-    va_start(ap, fmt);
-    vsprintf(buffer, fmt, ap);
+    va_start(ap, pszFmt);
+    vsprintf(szBuffer, pszFmt, ap);
     va_end(ap);
 
     LogItem(SYSSETUP_SEVERITY_FATAL_ERROR, L"Failed");
 
-    strcat(buffer, "\nRebooting now!");
+    strcat(szBuffer, "\nRebooting now!");
     MessageBoxA(NULL,
-                buffer,
+                szBuffer,
                 "ReactOS Setup",
                 MB_OK);
 }
 
-
-HRESULT CreateShellLink(LPCTSTR linkPath, LPCTSTR cmd, LPCTSTR arg, LPCTSTR dir, LPCTSTR iconPath, int icon_nr, LPCTSTR comment)
+static HRESULT
+CreateShellLink(
+    LPCTSTR pszLinkPath,
+    LPCTSTR pszCmd,
+    LPCTSTR pszArg,
+    LPCTSTR pszDir,
+    LPCTSTR pszIconPath,
+    int iIconNr,
+    LPCTSTR pszComment)
 {
-    IShellLinkpsl;
-    IPersistFileppf;
+    IShellLink *psl;
+    IPersistFile *ppf;
 #ifndef _UNICODE
-    WCHAR buffer[MAX_PATH];
+    WCHAR wszBuf[MAX_PATH];
 #endif /* _UNICODE */
 
     HRESULT hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (LPVOID*)&psl);
 
     if (SUCCEEDED(hr))
     {
-        hr = psl->lpVtbl->SetPath(psl, cmd);
+        hr = psl->lpVtbl->SetPath(psl, pszCmd);
 
-        if (arg)
+        if (pszArg)
         {
-            hr = psl->lpVtbl->SetArguments(psl, arg);
+            hr = psl->lpVtbl->SetArguments(psl, pszArg);
         }
 
-        if (dir)
+        if (pszDir)
         {
-            hr = psl->lpVtbl->SetWorkingDirectory(psl, dir);
+            hr = psl->lpVtbl->SetWorkingDirectory(psl, pszDir);
         }
 
-        if (iconPath)
+        if (pszIconPath)
         {
-            hr = psl->lpVtbl->SetIconLocation(psl, iconPath, icon_nr);
+            hr = psl->lpVtbl->SetIconLocation(psl, pszIconPath, iIconNr);
         }
 
-        if (comment)
+        if (pszComment)
         {
-            hr = psl->lpVtbl->SetDescription(psl, comment);
+            hr = psl->lpVtbl->SetDescription(psl, pszComment);
         }
 
         hr = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile, (LPVOID*)&ppf);
@@ -123,11 +109,11 @@ HRESULT CreateShellLink(LPCTSTR linkPath, LPCTSTR cmd, LPCTSTR arg, LPCTSTR dir,
         if (SUCCEEDED(hr))
         {
 #ifdef _UNICODE
-            hr = ppf->lpVtbl->Save(ppf, linkPath, TRUE);
+            hr = ppf->lpVtbl->Save(ppf, pszLinkPath, TRUE);
 #else /* _UNICODE */
-            MultiByteToWideChar(CP_ACP, 0, linkPath, -1, buffer, MAX_PATH);
+            MultiByteToWideChar(CP_ACP, 0, pszLinkPath, -1, wszBuf, MAX_PATH);
 
-            hr = ppf->lpVtbl->Save(ppf, buffer, TRUE);
+            hr = ppf->lpVtbl->Save(ppf, wszBuf, TRUE);
 #endif /* _UNICODE */
 
             ppf->lpVtbl->Release(ppf);
@@ -141,164 +127,156 @@ HRESULT CreateShellLink(LPCTSTR linkPath, LPCTSTR cmd, LPCTSTR arg, LPCTSTR dir,
 
 
 static BOOL
-CreateShortcut(int csidl, LPCTSTR folder, UINT nIdName, LPCTSTR command, UINT nIdTitle, BOOL bCheckExistence)
+CreateShortcut(
+    LPCTSTR pszFolder,
+    LPCTSTR pszName,
+    LPCTSTR pszCommand,
+    LPCTSTR pszDescription,
+    INT iIconNr)
 {
-    TCHAR path[MAX_PATH];
-    TCHAR exeName[MAX_PATH];
-    TCHAR title[256];
-    TCHAR name[256];
-    LPTSTR p = path;
-    TCHAR szWorkingDir[MAX_PATH];
-    LPTSTR lpWorkingDir = NULL;
+    TCHAR szPath[MAX_PATH];
+    TCHAR szExeName[MAX_PATH];
+    LPTSTR Ptr;
+    TCHAR szWorkingDirBuf[MAX_PATH];
+    LPTSTR pszWorkingDir = NULL;
     LPTSTR lpFilePart;
     DWORD dwLen;
 
-    if (ExpandEnvironmentStrings(command,
-                                 path,
-                                 sizeof(path) / sizeof(path[0])) == 0)
+    if (ExpandEnvironmentStrings(pszCommand,
+                                 szPath,
+                                 sizeof(szPath) / sizeof(szPath[0])) == 0)
     {
-        _tcscpy(path,
-                command);
+        _tcscpy(szPath, pszCommand);
     }
 
-    if (bCheckExistence)
-    {
-        if ((_taccess(path, 0 )) == -1)
-            /* Expected error, don't return FALSE */
-            return TRUE;
-    }
+    if ((_taccess(szPath, 0 )) == -1)
+        /* Expected error, don't return FALSE */
+        return TRUE;
 
-    dwLen = GetFullPathName(path,
-                            sizeof(szWorkingDir) / sizeof(szWorkingDir[0]),
-                            szWorkingDir,
+    dwLen = GetFullPathName(szPath,
+                            sizeof(szWorkingDirBuf) / sizeof(szWorkingDirBuf[0]),
+                            szWorkingDirBuf,
                             &lpFilePart);
-    if (dwLen != 0 && dwLen <= sizeof(szWorkingDir) / sizeof(szWorkingDir[0]))
+    if (dwLen != 0 && dwLen <= sizeof(szWorkingDirBuf) / sizeof(szWorkingDirBuf[0]))
     {
-        /* Save the file name */
-        _tcscpy(exeName, lpFilePart);
+        /* Since those should only be called with (.exe) files,
+           lpFilePart has not to be NULL */
+        ASSERT(lpFilePart != NULL);
 
-        if (lpFilePart != NULL)
+        /* Save the file name */
+        _tcscpy(szExeName, lpFilePart);
+
+        /* We're only interested in the path. Cut the file name off.
+           Also remove the trailing backslash unless the working directory
+           is only going to be a drive, ie. C:\ */
+        *(lpFilePart--) = _T('\0');
+        if (!(lpFilePart - szWorkingDirBuf == 2 && szWorkingDirBuf[1] == _T(':') &&
+              szWorkingDirBuf[2] == _T('\\')))
         {
-            /* We're only interested in the path. Cut the file name off.
-               Also remove the trailing backslash unless the working directory
-               is only going to be a drive, ie. C:\ */
-            *(lpFilePart--) = _T('\0');
-            if (!(lpFilePart - szWorkingDir == 2 && szWorkingDir[1] == _T(':') &&
-                  szWorkingDir[2] == _T('\\')))
-            {
-                *lpFilePart = _T('\0');
-            }
+            *lpFilePart = _T('\0');
         }
 
-        lpWorkingDir = szWorkingDir;
+        pszWorkingDir = szWorkingDirBuf;
     }
 
+    _tcscpy(szPath, pszFolder);
 
-    if (!SHGetSpecialFolderPath(0, path, csidl, TRUE))
+    Ptr = PathAddBackslash(szPath);
+
+    _tcscpy(Ptr, pszName);
+
+    // FIXME: we should pass 'command' straight in here, but shell32 doesn't expand it
+    return SUCCEEDED(CreateShellLink(szPath, szExeName, _T(""), pszWorkingDir, szExeName, iIconNr, pszDescription));
+}
+
+
+static BOOL CreateShortcutsFromSection(HINF hinf, LPWSTR  pszSection, LPCWSTR pszFolder)
+{
+    INFCONTEXT Context;
+    WCHAR szCommand[MAX_PATH];
+    WCHAR szName[MAX_PATH];
+    WCHAR szDescription[MAX_PATH];
+    INT iIconNr;
+
+    if (!SetupFindFirstLine(hinf, pszSection, NULL, &Context))
         return FALSE;
 
-    if (folder)
+    do
     {
-        p = PathAddBackslash(p);
-        _tcscpy(p, folder);
-    }
+        if (SetupGetFieldCount(&Context) < 4)
+            continue;
 
-    p = PathAddBackslash(p);
+        if (!SetupGetStringFieldW(&Context, 1, szCommand, MAX_PATH, NULL))
+            continue;
 
-    if (!LoadString(hDllInstance, nIdName, name, sizeof(name)/sizeof(name[0])))
-        return FALSE;
-    _tcscpy(p, name);
+        if (!SetupGetStringFieldW(&Context, 2, szName, MAX_PATH, NULL))
+            continue;
 
-    if (!LoadString(hDllInstance, nIdTitle, title, sizeof(title)/sizeof(title[0])))
-        return FALSE;
+        if (!SetupGetStringFieldW(&Context, 3, szDescription, MAX_PATH, NULL))
+            continue;
 
-    // FIXME: we should pass 'command' straight in here, but shell32 doesn't expand it
-    return SUCCEEDED(CreateShellLink(path, exeName, _T(""), lpWorkingDir, NULL, 0, title));
-}
+        if (!SetupGetIntField(&Context, 4, &iIconNr))
+            continue;
 
+        _tcscat(szName, L".lnk");
 
-static BOOL
-CreateShortcutFolder(int csidl, UINT nID, LPTSTR name, int nameLen)
+        CreateShortcut(pszFolder, szName, szCommand, szDescription, iIconNr);
+
+    }while (SetupFindNextLine(&Context, &Context));
+
+    return TRUE;
+}
+
+static BOOL CreateShortcuts(HINF hinf, LPCWSTR szSection)
 {
-    TCHAR path[MAX_PATH];
-    LPTSTR p;
+    INFCONTEXT Context;
+    WCHAR szPath[MAX_PATH];
+    WCHAR szFolder[MAX_PATH];
+    WCHAR szFolderSection[MAX_PATH];
+    INT csidl;
+    LPWSTR p;
 
-    if (!SHGetSpecialFolderPath(0, path, csidl, TRUE))
-        return FALSE;
+    CoInitialize(NULL);
 
-    if (!LoadString(hDllInstance, nID, name, nameLen))
+    if (!SetupFindFirstLine(hinf, szSection, NULL, &Context))
         return FALSE;
 
-    p = PathAddBackslash(path);
-    _tcscpy(p, name);
+    do
+    {
+        if (SetupGetFieldCount(&Context) < 2)
+            continue;
 
-    return CreateDirectory(path, NULL) || GetLastError()==ERROR_ALREADY_EXISTS;
-}
+        if (!SetupGetStringFieldW(&Context, 0, szFolderSection, MAX_PATH, NULL))
+            continue;
 
+        if (!SetupGetIntField(&Context, 1, &csidl))
+            continue;
 
-static BOOL
-CreateRandomSid(
-    OUT PSID *Sid)
-{
-    SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
-    LARGE_INTEGER SystemTime;
-    PULONG Seed;
-    NTSTATUS Status;
-
-    NtQuerySystemTime(&SystemTime);
-    Seed = &SystemTime.u.LowPart;
-
-    Status = RtlAllocateAndInitializeSid(
-        &SystemAuthority,
-        4,
-        SECURITY_NT_NON_UNIQUE,
-        RtlUniform(Seed),
-        RtlUniform(Seed),
-        RtlUniform(Seed),
-        SECURITY_NULL_RID,
-        SECURITY_NULL_RID,
-        SECURITY_NULL_RID,
-        SECURITY_NULL_RID,
-        Sid);
-    return NT_SUCCESS(Status);
-}
+        if (!SetupGetStringFieldW(&Context, 2, szFolder, MAX_PATH, NULL))
+            continue;
 
+        if (!SHGetSpecialFolderPathW(0, szPath, csidl, TRUE))
+            continue;
 
-static VOID
-AppendRidToSid(
-    OUT PSID *Dst,
-    IN PSID Src,
-    IN ULONG NewRid)
-{
-    ULONG Rid[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-    UCHAR RidCount;
-    ULONG i;
+        p = PathAddBackslash(szPath);
+        _tcscpy(p, szFolder);
 
-    RidCount = *RtlSubAuthorityCountSid (Src);
+        if (!CreateDirectory(szPath, NULL))
+        {
+            if (GetLastError() != ERROR_ALREADY_EXISTS) 
+            {
+                continue;
+            }
+        }
 
-    for (i = 0; i < RidCount; i++)
-        Rid[i] = *RtlSubAuthoritySid (Src, i);
+        CreateShortcutsFromSection(hinf, szFolderSection, szPath);
 
-    if (RidCount < 8)
-    {
-        Rid[RidCount] = NewRid;
-        RidCount++;
-    }
+    }while (SetupFindNextLine(&Context, &Context));
 
-    RtlAllocateAndInitializeSid(
-        RtlIdentifierAuthoritySid(Src),
-        RidCount,
-        Rid[0],
-        Rid[1],
-        Rid[2],
-        Rid[3],
-        Rid[4],
-        Rid[5],
-        Rid[6],
-        Rid[7],
-        Dst);
-}
+    CoUninitialize();
 
+    return TRUE;
+}
 
 static VOID
 CreateTempDir(
@@ -313,9 +291,9 @@ CreateTempDir(
                      L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
                      0,
                      KEY_QUERY_VALUE,
-                     &hKey))
+                     &hKey) != ERROR_SUCCESS)
     {
-        DebugPrint("Error: %lu\n", GetLastError());
+        FatalError("Error: %lu\n", GetLastError());
         return;
     }
 
@@ -326,11 +304,10 @@ CreateTempDir(
                         NULL,
                         NULL,
                         (LPBYTE)szBuffer,
-                        &dwLength))
+                        &dwLength) != ERROR_SUCCESS)
     {
-        DebugPrint("Error: %lu\n", GetLastError());
-        RegCloseKey(hKey);
-        return;
+        FatalError("Error: %lu\n", GetLastError());
+        goto cleanup;
     }
 
     /* Expand it */
@@ -338,9 +315,8 @@ CreateTempDir(
                                   szTempDir,
                                   MAX_PATH))
     {
-        DebugPrint("Error: %lu\n", GetLastError());
-        RegCloseKey(hKey);
-        return;
+        FatalError("Error: %lu\n", GetLastError());
+        goto cleanup;
     }
 
     /* Create profiles directory */
@@ -348,22 +324,21 @@ CreateTempDir(
     {
         if (GetLastError() != ERROR_ALREADY_EXISTS)
         {
-            DebugPrint("Error: %lu\n", GetLastError());
-            RegCloseKey(hKey);
-            return;
+            FatalError("Error: %lu\n", GetLastError());
+            goto cleanup;
         }
     }
 
+cleanup:
     RegCloseKey(hKey);
 }
 
-
-BOOL
+static BOOL
 InstallSysSetupInfDevices(VOID)
 {
     INFCONTEXT InfContext;
-    WCHAR LineBuffer[256];
-    DWORD LineLength;
+    WCHAR szLineBuffer[256];
+    DWORD dwLineLength;
 
     if (!SetupFindFirstLineW(hSysSetupInf,
                             L"DeviceInfsToInstall",
@@ -377,14 +352,14 @@ InstallSysSetupInfDevices(VOID)
     {
         if (!SetupGetStringFieldW(&InfContext,
                                  0,
-                                 LineBuffer,
-                                 sizeof(LineBuffer)/sizeof(LineBuffer[0]),
-                                 &LineLength))
+                                 szLineBuffer,
+                                 sizeof(szLineBuffer)/sizeof(szLineBuffer[0]),
+                                 &dwLineLength))
         {
             return FALSE;
         }
 
-        if (!SetupDiInstallClassW(NULL, LineBuffer, DI_QUIETINSTALL, NULL))
+        if (!SetupDiInstallClassW(NULL, szLineBuffer, DI_QUIETINSTALL, NULL))
         {
             return FALSE;
         }
@@ -393,12 +368,13 @@ InstallSysSetupInfDevices(VOID)
 
     return TRUE;
 }
-BOOL
+
+static BOOL
 InstallSysSetupInfComponents(VOID)
 {
     INFCONTEXT InfContext;
-    WCHAR NameBuffer[256];
-    WCHAR SectionBuffer[256];
+    WCHAR szNameBuffer[256];
+    WCHAR szSectionBuffer[256];
     HINF hComponentInf = INVALID_HANDLE_VALUE;
 
     if (!SetupFindFirstLineW(hSysSetupInf,
@@ -414,40 +390,40 @@ InstallSysSetupInfComponents(VOID)
         {
             if (!SetupGetStringFieldW(&InfContext,
                                      1, // Get the component name
-                                     NameBuffer,
-                                     sizeof(NameBuffer)/sizeof(NameBuffer[0]),
+                                     szNameBuffer,
+                                     sizeof(szNameBuffer)/sizeof(szNameBuffer[0]),
                                      NULL))
             {
-                DebugPrint("Error while trying to get component name \n");
+                FatalError("Error while trying to get component name \n");
                 return FALSE;
             }
 
             if (!SetupGetStringFieldW(&InfContext,
                                      2, // Get the component install section
-                                     SectionBuffer,
-                                     sizeof(SectionBuffer)/sizeof(SectionBuffer[0]),
+                                     szSectionBuffer,
+                                     sizeof(szSectionBuffer)/sizeof(szSectionBuffer[0]),
                                      NULL))
             {
-                DebugPrint("Error while trying to get component install section \n");
+                FatalError("Error while trying to get component install section \n");
                 return FALSE;
             }
 
-            DPRINT("Trying to execute install section '%S' from '%S' \n", SectionBuffer , NameBuffer);
+            DPRINT("Trying to execute install section '%S' from '%S' \n", szSectionBuffer, szNameBuffer);
 
-            hComponentInf = SetupOpenInfFileW(NameBuffer,
+            hComponentInf = SetupOpenInfFileW(szNameBuffer,
                                               NULL,
                                               INF_STYLE_WIN4,
                                               NULL);
 
             if (hComponentInf == INVALID_HANDLE_VALUE)
             {
-                DebugPrint("SetupOpenInfFileW() failed to open '%S' (Error: %lu)\n", NameBuffer ,GetLastError());
+                FatalError("SetupOpenInfFileW() failed to open '%S' (Error: %lu)\n", szNameBuffer, GetLastError());
                 return FALSE;
             }
 
             if (!SetupInstallFromInfSectionW(NULL,
                                             hComponentInf,
-                                            SectionBuffer,
+                                            szSectionBuffer,
                                             SPINST_ALL,
                                             NULL,
                                             NULL,
@@ -457,7 +433,7 @@ InstallSysSetupInfComponents(VOID)
                                             NULL,
                                             NULL))
            {
-                DebugPrint("Error while trying to install : %S (Error: %lu)\n", NameBuffer, GetLastError());
+                FatalError("Error while trying to install : %S (Error: %lu)\n", szNameBuffer, GetLastError());
                 SetupCloseInfFile(hComponentInf);
                 return FALSE;
            }
@@ -470,17 +446,73 @@ InstallSysSetupInfComponents(VOID)
     return TRUE;
 }
 
+
+
+
+BOOL
+RegisterTypeLibraries (HINF hinf, LPCWSTR szSection)
+{
+    INFCONTEXT InfContext;
+    BOOL res;
+    WCHAR szName[MAX_PATH];
+    WCHAR szPath[MAX_PATH];
+    INT csidl;
+    LPWSTR p;
+    HMODULE hmod;
+    HRESULT hret;
+
+    /* Begin iterating the entries in the inf section */
+    res = SetupFindFirstLine(hinf, szSection, NULL, &InfContext);
+    if (!res) return FALSE;
+
+    do
+    {
+        /* Get the name of the current type library */
+        if (!SetupGetStringFieldW(&InfContext, 1, szName, MAX_PATH, NULL))
+        {
+            FatalError("SetupGetStringFieldW failed\n");
+            continue;
+        }
+
+        if (!SetupGetIntField(&InfContext, 2, &csidl))
+            csidl = CSIDL_SYSTEM;
+
+        hret = SHGetFolderPathW(NULL, csidl, NULL, 0, szPath);
+        if (FAILED(hret))
+        {
+            FatalError("SHGetSpecialFolderPathW failed hret=0x%d\n", hret);
+            continue;
+        }
+
+        p = PathAddBackslash(szPath);
+        _tcscpy(p, szName);
+
+        hmod = LoadLibraryW(szName);
+        if (hmod == NULL)
+        {
+            FatalError("LoadLibraryW failed\n");
+            continue;
+        }
+
+        __wine_register_resources(hmod);
+
+    }while (SetupFindNextLine(&InfContext, &InfContext));
+
+    return TRUE;
+}
+
 static BOOL
 EnableUserModePnpManager(VOID)
 {
     SC_HANDLE hSCManager = NULL;
     SC_HANDLE hService = NULL;
-    BOOL ret = FALSE;
+    BOOL bRet = FALSE;
 
     hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
     if (hSCManager == NULL)
     {
         DPRINT1("Unable to open the service control manager.\n");
+        DPRINT1("Last Error %d\n", GetLastError());
         goto cleanup;
     }
 
@@ -493,36 +525,35 @@ EnableUserModePnpManager(VOID)
         goto cleanup;
     }
 
-    ret = ChangeServiceConfigW(hService,
+    bRet = ChangeServiceConfigW(hService,
                                SERVICE_NO_CHANGE,
                                SERVICE_AUTO_START,
                                SERVICE_NO_CHANGE,
                                NULL, NULL, NULL,
                                NULL, NULL, NULL, NULL);
-    if (!ret)
+    if (!bRet)
     {
         DPRINT1("Unable to change the service configuration\n");
         goto cleanup;
     }
 
-    ret = StartServiceW(hService, 0, NULL);
-    if ((!ret) && (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING))
+    bRet = StartServiceW(hService, 0, NULL);
+    if (!bRet && (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING))
     {
         DPRINT1("Unable to start service\n");
         goto cleanup;
     }
 
-    ret = TRUE;
+    bRet = TRUE;
 
 cleanup:
-    if (hSCManager != NULL)
-        CloseServiceHandle(hSCManager);
     if (hService != NULL)
         CloseServiceHandle(hService);
-    return ret;
+    if (hSCManager != NULL)
+        CloseServiceHandle(hSCManager);
+    return bRet;
 }
 
-
 static INT_PTR CALLBACK
 StatusMessageWindowProc(
     IN HWND hwndDlg,
@@ -547,7 +578,6 @@ StatusMessageWindowProc(
     return FALSE;
 }
 
-
 static DWORD WINAPI
 ShowStatusMessageThread(
     IN LPVOID lpParameter)
@@ -587,7 +617,7 @@ ReadRegSzKey(
     LONG rc;
     DWORD dwType;
     DWORD cbData = 0;
-    LPWSTR Value;
+    LPWSTR pwszValue;
 
     if (!pValue)
         return ERROR_INVALID_PARAMETER;
@@ -598,29 +628,29 @@ ReadRegSzKey(
         return rc;
     if (dwType != REG_SZ)
         return ERROR_FILE_NOT_FOUND;
-    Value = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
-    if (!Value)
+    pwszValue = HeapAlloc(GetProcessHeap(), 0, cbData + sizeof(WCHAR));
+    if (!pwszValue)
         return ERROR_NOT_ENOUGH_MEMORY;
-    rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)Value, &cbData);
+    rc = RegQueryValueExW(hKey, pszKey, NULL, NULL, (LPBYTE)pwszValue, &cbData);
     if (rc != ERROR_SUCCESS)
     {
-        HeapFree(GetProcessHeap(), 0, Value);
+        HeapFree(GetProcessHeap(), 0, pwszValue);
         return rc;
     }
     /* NULL-terminate the string */
-    Value[cbData / sizeof(WCHAR)] = '\0';
+    pwszValue[cbData / sizeof(WCHAR)] = '\0';
 
-    *pValue = Value;
+    *pValue = pwszValue;
     return ERROR_SUCCESS;
 }
 
 static BOOL
 IsConsoleBoot(VOID)
 {
-    HKEY ControlKey = NULL;
-    LPWSTR SystemStartOptions = NULL;
-    LPWSTR CurrentOption, NextOption; /* Pointers into SystemStartOptions */
-    BOOL ConsoleBoot = FALSE;
+    HKEY hControlKey = NULL;
+    LPWSTR pwszSystemStartOptions = NULL;
+    LPWSTR pwszCurrentOption, pwszNextOption; /* Pointers into SystemStartOptions */
+    BOOL bConsoleBoot = FALSE;
     LONG rc;
 
     rc = RegOpenKeyExW(
@@ -628,33 +658,36 @@ IsConsoleBoot(VOID)
         L"SYSTEM\\CurrentControlSet\\Control",
         0,
         KEY_QUERY_VALUE,
-        &ControlKey);
+        &hControlKey);
+    if (rc != ERROR_SUCCESS)
+        goto cleanup;
 
-    rc = ReadRegSzKey(ControlKey, L"SystemStartOptions", &SystemStartOptions);
+    rc = ReadRegSzKey(hControlKey, L"SystemStartOptions", &pwszSystemStartOptions);
     if (rc != ERROR_SUCCESS)
         goto cleanup;
 
-    /* Check for CMDCONS in SystemStartOptions */
-    CurrentOption = SystemStartOptions;
-    while (CurrentOption)
+    /* Check for CONSOLE switch in SystemStartOptions */
+    pwszCurrentOption = pwszSystemStartOptions;
+    while (pwszCurrentOption)
     {
-        NextOption = wcschr(CurrentOption, L' ');
-        if (NextOption)
-            *NextOption = L'\0';
-        if (wcsicmp(CurrentOption, L"CONSOLE") == 0)
+        pwszNextOption = wcschr(pwszCurrentOption, L' ');
+        if (pwszNextOption)
+            *pwszNextOption = L'\0';
+        if (wcsicmp(pwszCurrentOption, L"CONSOLE") == 0)
         {
-            DPRINT("Found %S. Switching to console boot\n", CurrentOption);
-            ConsoleBoot = TRUE;
+            DPRINT("Found %S. Switching to console boot\n", pwszCurrentOption);
+            bConsoleBoot = TRUE;
             goto cleanup;
         }
-        CurrentOption = NextOption ? NextOption + 1 : NULL;
+        pwszCurrentOption = pwszNextOption ? pwszNextOption + 1 : NULL;
     }
 
 cleanup:
-    if (ControlKey != NULL)
-        RegCloseKey(ControlKey);
-    HeapFree(GetProcessHeap(), 0, SystemStartOptions);
-    return ConsoleBoot;
+    if (hControlKey != NULL)
+        RegCloseKey(hControlKey);
+    if (pwszSystemStartOptions)
+        HeapFree(GetProcessHeap(), 0, pwszSystemStartOptions);
+    return bConsoleBoot;
 }
 
 static BOOL
@@ -669,53 +702,58 @@ CommonInstall(VOID)
         NULL);
     if (hSysSetupInf == INVALID_HANDLE_VALUE)
     {
-        DebugPrint("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
+        FatalError("SetupOpenInfFileW() failed to open 'syssetup.inf' (Error: %lu)\n", GetLastError());
         return FALSE;
     }
 
     if (!InstallSysSetupInfDevices())
     {
-        DebugPrint("InstallSysSetupInfDevices() failed!\n");
-        SetupCloseInfFile(hSysSetupInf);
-        return FALSE;
+        FatalError("InstallSysSetupInfDevices() failed!\n");
+        goto error;
     }
 
     if(!InstallSysSetupInfComponents())
     {
-        DebugPrint("InstallSysSetupInfComponents() failed!\n");
-        SetupCloseInfFile(hSysSetupInf);
-        return FALSE;
+        FatalError("InstallSysSetupInfComponents() failed!\n");
+        goto error;
     }
 
     if (!IsConsoleBoot())
     {
-        CreateThread(
+        HANDLE hThread;
+
+        hThread = CreateThread(
             NULL,
             0,
             ShowStatusMessageThread,
             (LPVOID)&hWnd,
             0,
             NULL);
+
+        if (hThread)
+            CloseHandle(hThread);
     }
 
     if (!EnableUserModePnpManager())
     {
-       DebugPrint("EnableUserModePnpManager() failed!\n");
-       SetupCloseInfFile(hSysSetupInf);
-       EndDialog(hWnd, 0);
-       return FALSE;
+        FatalError("EnableUserModePnpManager() failed!\n");
+        goto error;
     }
 
     if (CMP_WaitNoPendingInstallEvents(INFINITE) != WAIT_OBJECT_0)
     {
-      DebugPrint("CMP_WaitNoPendingInstallEvents() failed!\n");
-      SetupCloseInfFile(hSysSetupInf);
-      EndDialog(hWnd, 0);
-      return FALSE;
+        FatalError("CMP_WaitNoPendingInstallEvents() failed!\n");
+        goto error;
     }
 
     EndDialog(hWnd, 0);
     return TRUE;
+
+error:
+    if (hWnd)
+        EndDialog(hWnd, 0);
+    SetupCloseInfFile(hSysSetupInf);
+    return FALSE;
 }
 
 DWORD WINAPI
@@ -723,21 +761,36 @@ InstallLiveCD(IN HINSTANCE hInstance)
 {
     STARTUPINFOW StartupInfo;
     PROCESS_INFORMATION ProcessInformation;
-    BOOL res;
+    BOOL bRes;
 
     if (!CommonInstall())
-        goto cleanup;
+        goto error;
+    
+    /* Register components */
+    _SEH2_TRY
+    {
+        if (!SetupInstallFromInfSectionW(NULL,
+            hSysSetupInf, L"RegistrationPhase2",
+            SPINST_ALL,
+            0, NULL, 0, NULL, NULL, NULL, NULL))
+        {
+            DPRINT1("SetupInstallFromInfSectionW failed!\n");
+        }
+
+        RegisterTypeLibraries(hSysSetupInf, L"TypeLibraries");
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        DPRINT1("Catching exception\n");
+    }
+    _SEH2_END;
+    
     SetupCloseInfFile(hSysSetupInf);
 
     /* Run the shell */
-    StartupInfo.cb = sizeof(STARTUPINFOW);
-    StartupInfo.lpReserved = NULL;
-    StartupInfo.lpDesktop = NULL;
-    StartupInfo.lpTitle = NULL;
-    StartupInfo.dwFlags = 0;
-    StartupInfo.cbReserved2 = 0;
-    StartupInfo.lpReserved2 = 0;
-    res = CreateProcessW(
+    ZeroMemory(&StartupInfo, sizeof(StartupInfo));
+    StartupInfo.cb = sizeof(StartupInfo);
+    bRes = CreateProcessW(
         L"userinit.exe",
         NULL,
         NULL,
@@ -748,90 +801,24 @@ InstallLiveCD(IN HINSTANCE hInstance)
         NULL,
         &StartupInfo,
         &ProcessInformation);
-    if (!res)
-        goto cleanup;
+    if (!bRes)
+        goto error;
+
+    CloseHandle(ProcessInformation.hThread);
+    CloseHandle(ProcessInformation.hProcess);
 
     return 0;
 
-cleanup:
+error:
     MessageBoxW(
         NULL,
-        L"You can shutdown your computer, or press ENTER to reboot",
+        L"Failed to load LiveCD! You can shutdown your computer, or press ENTER to reboot.",
         L"ReactOS LiveCD",
         MB_OK);
     return 0;
 }
 
 
-static BOOL
-CreateShortcuts(VOID)
-{
-    TCHAR szFolder[256];
-
-    CoInitialize(NULL);
-
-    /* Create desktop shortcuts */
-    CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
-    CreateShortcut(CSIDL_DESKTOP, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\rapps.exe"), IDS_CMT_DOWNLOADER, TRUE);
-
-    /* Create program startmenu shortcuts */
-    CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_EXPLORER, _T("%SystemRoot%\\explorer.exe"), IDS_CMT_EXPLORER, TRUE);
-    CreateShortcut(CSIDL_PROGRAMS, NULL, IDS_SHORT_DOWNLOADER, _T("%SystemRoot%\\system32\\rapps.exe"), IDS_CMT_DOWNLOADER, TRUE);
-
-    /* Create administrative tools startmenu shortcuts */
-    CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_SERVICE, _T("%SystemRoot%\\system32\\servman.exe"), IDS_CMT_SERVMAN, TRUE);
-    CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_DEVICE, _T("%SystemRoot%\\system32\\devmgmt.exe"), IDS_CMT_DEVMGMT, TRUE);
-    CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_EVENTVIEW, _T("%SystemRoot%\\system32\\eventvwr.exe"), IDS_CMT_EVENTVIEW, TRUE);
-    CreateShortcut(CSIDL_COMMON_ADMINTOOLS, NULL, IDS_SHORT_MSCONFIG, _T("%SystemRoot%\\system32\\msconfig.exe"), IDS_CMT_MSCONFIG, TRUE);
-
-    /* Create and fill Accessories subfolder */
-    if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_ACCESSORIES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
-    {
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CALC, _T("%SystemRoot%\\system32\\calc.exe"), IDS_CMT_CALC, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CMD, _T("%SystemRoot%\\system32\\cmd.exe"), IDS_CMT_CMD, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_NOTEPAD, _T("%SystemRoot%\\system32\\notepad.exe"), IDS_CMT_NOTEPAD, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_RDESKTOP, _T("%SystemRoot%\\system32\\mstsc.exe"), IDS_CMT_RDESKTOP, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNAP, _T("%SystemRoot%\\system32\\screenshot.exe"), IDS_CMT_SCREENSHOT, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WORDPAD, _T("%SystemRoot%\\system32\\wordpad.exe"), IDS_CMT_WORDPAD, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_PAINT, _T("%SystemRoot%\\system32\\paint.exe"), IDS_CMT_PAINT, TRUE);
-    }
-
-    /* Create System Tools subfolder and fill if the exe is available */
-    if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_TOOLS, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
-    {
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_CHARMAP, _T("%SystemRoot%\\system32\\charmap.exe"), IDS_CMT_CHARMAP, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_KBSWITCH, _T("%SystemRoot%\\system32\\kbswitch.exe"), IDS_CMT_KBSWITCH, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_REGEDIT, _T("%SystemRoot%\\regedit.exe"), IDS_CMT_REGEDIT, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_DXDIAG, _T("%SystemRoot%\\system32\\dxdiag.exe"), IDS_CMT_DXDIAG, TRUE);
-    }
-
-    /* Create Accessibility subfolder and fill if the exe is available */
-    if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ACCESSIBILITY, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
-    {
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MAGNIFY, _T("%SystemRoot%\\system32\\magnify.exe"), IDS_CMT_MAGNIFY, TRUE);
-    }
-
-    /* Create Entertainment subfolder and fill if the exe is available */
-    if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_SYS_ENTERTAINMENT, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
-    {
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_MPLAY32, _T("%SystemRoot%\\system32\\mplay32.exe"), IDS_CMT_MPLAY32, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDVOL32, _T("%SystemRoot%\\system32\\sndvol32.exe"), IDS_CMT_SNDVOL32, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SNDREC32, _T("%SystemRoot%\\system32\\sndrec32.exe"), IDS_CMT_SNDREC32, TRUE);
-    }
-
-    /* Create Games subfolder and fill if the exe is available */
-    if (CreateShortcutFolder(CSIDL_PROGRAMS, IDS_GAMES, szFolder, sizeof(szFolder)/sizeof(szFolder[0])))
-    {
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SOLITAIRE, _T("%SystemRoot%\\system32\\sol.exe"), IDS_CMT_SOLITAIRE, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_WINEMINE, _T("%SystemRoot%\\system32\\winmine.exe"), IDS_CMT_WINEMINE, TRUE);
-        CreateShortcut(CSIDL_PROGRAMS, szFolder, IDS_SHORT_SPIDER, _T("%SystemRoot%\\system32\\spider.exe"), IDS_CMT_SPIDER, TRUE);
-    }
-
-    CoUninitialize();
-
-    return TRUE;
-}
-
 static BOOL
 SetSetupType(DWORD dwSetupType)
 {
@@ -865,51 +852,20 @@ DWORD WINAPI
 InstallReactOS(HINSTANCE hInstance)
 {
     TCHAR szBuffer[MAX_PATH];
-    DWORD LastError;
     HANDLE token;
     TOKEN_PRIVILEGES privs;
     HKEY hKey;
+    HINF hShortcutsInf;
 
     InitializeSetupActionLog(FALSE);
     LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS");
 
     if (!InitializeProfiles())
     {
-        DebugPrint("InitializeProfiles() failed");
-        return 0;
-    }
-
-    if (!CreateShortcuts())
-    {
-        DebugPrint("InitializeProfiles() failed");
-        return 0;
-    }
-
-    /* Initialize the Security Account Manager (SAM) */
-    if (!SamInitializeSAM())
-    {
-        DebugPrint("SamInitializeSAM() failed!");
+        FatalError("InitializeProfiles() failed");
         return 0;
     }
 
-    /* Create the semi-random Domain-SID */
-    if (!CreateRandomSid(&DomainSid))
-    {
-        DebugPrint("Domain-SID creation failed!");
-        return 0;
-    }
-
-    /* Set the Domain SID (aka Computer SID) */
-    if (!SamSetDomainSid(DomainSid))
-    {
-        DebugPrint("SamSetDomainSid() failed!");
-        RtlFreeSid(DomainSid);
-        return 0;
-    }
-
-    /* Append the Admin-RID */
-    AppendRidToSid(&AdminSid, DomainSid, DOMAIN_USER_RID_ADMIN);
-
     CreateTempDir(L"TEMP");
     CreateTempDir(L"TMP");
 
@@ -948,26 +904,27 @@ InstallReactOS(HINSTANCE hInstance)
 
     InstallWizard();
 
-    /* Create the Administrator account */
-    if (!SamCreateUser(L"Administrator", L"", AdminSid))
+    InstallSecurity();
+
+    SetAutoAdminLogon();
+
+    hShortcutsInf = SetupOpenInfFileW(L"shortcuts.inf",
+                                      NULL,
+                                      INF_STYLE_WIN4,
+                                      NULL);
+    if (hShortcutsInf == INVALID_HANDLE_VALUE) 
     {
-        /* Check what the error was.
-         * If the Admin Account already exists, then it means Setup
-         * wasn't allowed to finish properly. Instead of rebooting
-         * and not completing it, let it restart instead
-         */
-        LastError = GetLastError();
-        if (LastError != ERROR_USER_EXISTS)
-        {
-            DebugPrint("SamCreateUser() failed!");
-            RtlFreeSid(AdminSid);
-            RtlFreeSid(DomainSid);
-            return 0;
-        }
+        FatalError("Failed to open shortcuts.inf");
+        return 0;
     }
 
-    RtlFreeSid(AdminSid);
-    RtlFreeSid(DomainSid);
+    if (!CreateShortcuts(hShortcutsInf, L"ShortcutFolders"))
+    {
+        FatalError("CreateShortcuts() failed");
+        return 0;
+    }
+
+    SetupCloseInfFile(hShortcutsInf);
 
     /* ROS HACK, as long as NtUnloadKey is not implemented */
     {
@@ -978,11 +935,16 @@ InstallReactOS(HINSTANCE hInstance)
             PROFILEINFOW ProfileInfo;
             HANDLE hToken;
             BOOL ret;
-#define LOGON32_LOGON_NETWORK 3
-            ret = LogonUserW(L"Administrator", L"", L"", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken);
+
+            ret = LogonUserW(AdminInfo.Name,
+                             AdminInfo.Domain,
+                             AdminInfo.Password,
+                             LOGON32_LOGON_INTERACTIVE,
+                             LOGON32_PROVIDER_DEFAULT,
+                             &hToken);
             if (!ret)
             {
-                DebugPrint("LogonUserW() failed!");
+                FatalError("LogonUserW() failed!");
                 return 0;
             }
             ZeroMemory(&ProfileInfo, sizeof(PROFILEINFOW));
@@ -1005,10 +967,19 @@ InstallReactOS(HINSTANCE hInstance)
     LogItem(SYSSETUP_SEVERITY_INFORMATION, L"Installing ReactOS done");
     TerminateSetupActionLog();
 
+    if (AdminInfo.Name != NULL)
+        RtlFreeHeap(RtlGetProcessHeap(), 0, AdminInfo.Name);
+
+    if (AdminInfo.Domain != NULL)
+        RtlFreeHeap(RtlGetProcessHeap(), 0, AdminInfo.Domain);
+
+    if (AdminInfo.Password != NULL)
+        RtlFreeHeap(RtlGetProcessHeap(), 0, AdminInfo.Password);
+
     /* Get shutdown privilege */
-    if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
+    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
     {
-        DebugPrint("OpenProcessToken() failed!");
+        FatalError("OpenProcessToken() failed!");
         return 0;
     }
     if (!LookupPrivilegeValue(
@@ -1016,7 +987,7 @@ InstallReactOS(HINSTANCE hInstance)
         SE_SHUTDOWN_NAME,
         &privs.Privileges[0].Luid))
     {
-        DebugPrint("LookupPrivilegeValue() failed!");
+        FatalError("LookupPrivilegeValue() failed!");
         return 0;
     }
     privs.PrivilegeCount = 1;
@@ -1029,7 +1000,7 @@ InstallReactOS(HINSTANCE hInstance)
         (PTOKEN_PRIVILEGES)NULL,
         NULL) == 0)
     {
-        DebugPrint("AdjustTokenPrivileges() failed!");
+        FatalError("AdjustTokenPrivileges() failed!");
         return 0;
     }