Sync to trunk revision 61757.
[reactos.git] / base / system / winlogon / setup.c
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) / sizeof(Shell[0])) - 1;
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, MAX_PATH);
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 /* Start process */
108 StartupInfo.cb = sizeof(StartupInfo);
109 StartupInfo.lpReserved = NULL;
110 StartupInfo.lpDesktop = NULL;
111 StartupInfo.lpTitle = NULL;
112 StartupInfo.dwFlags = 0;
113 StartupInfo.cbReserved2 = 0;
114 StartupInfo.lpReserved2 = 0;
115
116 Result = CreateProcessW(NULL,
117 CommandLine,
118 NULL,
119 NULL,
120 FALSE,
121 DETACHED_PROCESS,
122 NULL,
123 NULL,
124 &StartupInfo,
125 &ProcessInformation);
126 if (!Result)
127 {
128 TRACE("Failed to run setup process\n");
129 return FALSE;
130 }
131
132 /* Wait for process termination */
133 WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
134
135 GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
136
137 /* Close handles */
138 CloseHandle(ProcessInformation.hThread);
139 CloseHandle(ProcessInformation.hProcess);
140
141 TRACE ("RunSetup() done\n");
142
143 return TRUE;
144 }
145
146
147 BOOL
148 RunSetup(VOID)
149 {
150 HANDLE hThread;
151
152 hThread = CreateThread(NULL,
153 0,
154 RunSetupThreadProc,
155 NULL,
156 0,
157 NULL);
158
159 return hThread != NULL;
160 }
161
162 /* EOF */