xref: /reactos/base/system/winlogon/setup.c (revision 58588b76)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Winlogon
4  * FILE:            base/system/winlogon/setup.c
5  * PURPOSE:         Setup support functions
6  * PROGRAMMERS:     Eric Kohl
7  */
8 
9 /* INCLUDES *****************************************************************/
10 
11 #include "winlogon.h"
12 
13 /* FUNCTIONS ****************************************************************/
14 
15 DWORD
16 GetSetupType(VOID)
17 {
18     DWORD dwError;
19     HKEY hKey;
20     DWORD dwType;
21     DWORD dwSize;
22     DWORD dwSetupType;
23 
24     TRACE("GetSetupType()\n");
25 
26     /* Open key */
27     dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
28                             L"SYSTEM\\Setup",
29                             0,
30                             KEY_QUERY_VALUE,
31                             &hKey);
32     if (dwError != ERROR_SUCCESS)
33         return 0;
34 
35     /* Read key */
36     dwSize = sizeof(DWORD);
37     dwError = RegQueryValueExW(hKey,
38                                L"SetupType",
39                                NULL,
40                                &dwType,
41                                (LPBYTE)&dwSetupType,
42                                &dwSize);
43 
44     /* Close key, and check if returned values are correct */
45     RegCloseKey(hKey);
46     if (dwError != ERROR_SUCCESS || dwType != REG_DWORD || dwSize != sizeof(DWORD))
47         return 0;
48 
49     TRACE("GetSetupType() returns %lu\n", dwSetupType);
50     return dwSetupType;
51 }
52 
53 
54 static
55 DWORD
56 WINAPI
57 RunSetupThreadProc(
58     IN LPVOID lpParameter)
59 {
60     PROCESS_INFORMATION ProcessInformation;
61     STARTUPINFOW StartupInfo;
62     WCHAR Shell[MAX_PATH];
63     WCHAR CommandLine[MAX_PATH];
64     BOOL Result;
65     DWORD dwError;
66     HKEY hKey;
67     DWORD dwType;
68     DWORD dwSize;
69     DWORD dwExitCode;
70 
71     TRACE("RunSetup() called\n");
72 
73     /* Open key */
74     dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
75                             L"SYSTEM\\Setup",
76                             0,
77                             KEY_QUERY_VALUE,
78                             &hKey);
79     if (dwError != ERROR_SUCCESS)
80         return FALSE;
81 
82     /* Read key */
83     dwSize = sizeof(Shell);
84     dwError = RegQueryValueExW(hKey,
85                                L"CmdLine",
86                                NULL,
87                                &dwType,
88                                (LPBYTE)Shell,
89                                &dwSize);
90     RegCloseKey(hKey);
91     if (dwError != ERROR_SUCCESS)
92         return FALSE;
93 
94     /* Finish string */
95     Shell[dwSize / sizeof(WCHAR)] = UNICODE_NULL;
96 
97     /* Expand string (if applicable) */
98     if (dwType == REG_EXPAND_SZ)
99         ExpandEnvironmentStringsW(Shell, CommandLine, ARRAYSIZE(CommandLine));
100     else if (dwType == REG_SZ)
101         wcscpy(CommandLine, Shell);
102     else
103         return FALSE;
104 
105     TRACE("Should run '%s' now\n", debugstr_w(CommandLine));
106 
107     SwitchDesktop(WLSession->ApplicationDesktop);
108 
109     /* Start process */
110     StartupInfo.cb = sizeof(StartupInfo);
111     StartupInfo.lpReserved = NULL;
112     StartupInfo.lpDesktop = L"WinSta0\\Default";
113     StartupInfo.lpTitle = NULL;
114     StartupInfo.dwFlags = 0;
115     StartupInfo.cbReserved2 = 0;
116     StartupInfo.lpReserved2 = 0;
117 
118     Result = CreateProcessW(NULL,
119                             CommandLine,
120                             NULL,
121                             NULL,
122                             FALSE,
123                             DETACHED_PROCESS,
124                             NULL,
125                             NULL,
126                             &StartupInfo,
127                             &ProcessInformation);
128     if (!Result)
129     {
130         TRACE("Failed to run setup process\n");
131         SwitchDesktop(WLSession->WinlogonDesktop);
132         return FALSE;
133     }
134 
135     /* Wait for process termination */
136     WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
137 
138     GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
139 
140     /* Close handles */
141     CloseHandle(ProcessInformation.hThread);
142     CloseHandle(ProcessInformation.hProcess);
143 
144     // SwitchDesktop(WLSession->WinlogonDesktop);
145 
146     TRACE ("RunSetup() done\n");
147 
148     return TRUE;
149 }
150 
151 
152 BOOL
153 RunSetup(VOID)
154 {
155     HANDLE hThread;
156 
157     hThread = CreateThread(NULL,
158                            0,
159                            RunSetupThreadProc,
160                            NULL,
161                            0,
162                            NULL);
163     if (hThread != NULL)
164         CloseHandle(hThread);
165 
166     return hThread != NULL;
167 }
168 
169 /* EOF */
170