- Sync with trunk r58248 to bring the latest changes from Amine (headers) and others...
[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 WINE_DEFAULT_DEBUG_CHANNEL(winlogon);
14
15 /* FUNCTIONS ****************************************************************/
16
17 DWORD
18 GetSetupType(VOID)
19 {
20 DWORD dwError;
21 HKEY hKey;
22 DWORD dwType;
23 DWORD dwSize;
24 DWORD dwSetupType;
25
26 TRACE("GetSetupType()\n");
27
28 /* Open key */
29 dwError = RegOpenKeyExW(
30 HKEY_LOCAL_MACHINE,
31 L"SYSTEM\\Setup",
32 0,
33 KEY_QUERY_VALUE,
34 &hKey);
35 if (dwError != ERROR_SUCCESS)
36 return 0;
37
38 /* Read key */
39 dwSize = sizeof(DWORD);
40 dwError = RegQueryValueExW(
41 hKey,
42 L"SetupType",
43 NULL,
44 &dwType,
45 (LPBYTE)&dwSetupType,
46 &dwSize);
47
48 /* Close key, and check if returned values are correct */
49 RegCloseKey(hKey);
50 if (dwError != ERROR_SUCCESS || dwType != REG_DWORD || dwSize != sizeof(DWORD))
51 return 0;
52
53 TRACE("GetSetupType() returns %lu\n", dwSetupType);
54 return dwSetupType;
55 }
56
57 static DWORD WINAPI
58 RunSetupThreadProc(
59 IN LPVOID lpParameter)
60 {
61 PROCESS_INFORMATION ProcessInformation;
62 STARTUPINFOW StartupInfo;
63 WCHAR Shell[MAX_PATH];
64 WCHAR CommandLine[MAX_PATH];
65 BOOL Result;
66 DWORD dwError;
67 HKEY hKey;
68 DWORD dwType;
69 DWORD dwSize;
70 DWORD dwExitCode;
71
72 TRACE("RunSetup() called\n");
73
74 /* Open key */
75 dwError = RegOpenKeyExW(
76 HKEY_LOCAL_MACHINE,
77 L"SYSTEM\\Setup",
78 0,
79 KEY_QUERY_VALUE,
80 &hKey);
81 if (dwError != ERROR_SUCCESS)
82 return FALSE;
83
84 /* Read key */
85 dwSize = (sizeof(Shell) / sizeof(Shell[0])) - 1;
86 dwError = RegQueryValueExW(
87 hKey,
88 L"CmdLine",
89 NULL,
90 &dwType,
91 (LPBYTE)Shell,
92 &dwSize);
93 RegCloseKey(hKey);
94 if (dwError != ERROR_SUCCESS)
95 return FALSE;
96
97 /* Finish string */
98 Shell[dwSize / sizeof(WCHAR)] = UNICODE_NULL;
99
100 /* Expand string (if applicable) */
101 if (dwType == REG_EXPAND_SZ)
102 ExpandEnvironmentStringsW(Shell, CommandLine, MAX_PATH);
103 else if (dwType == REG_SZ)
104 wcscpy(CommandLine, Shell);
105 else
106 return FALSE;
107
108 TRACE("Should run '%s' now\n", debugstr_w(CommandLine));
109
110 /* Start process */
111 StartupInfo.cb = sizeof(StartupInfo);
112 StartupInfo.lpReserved = NULL;
113 StartupInfo.lpDesktop = NULL;
114 StartupInfo.lpTitle = NULL;
115 StartupInfo.dwFlags = 0;
116 StartupInfo.cbReserved2 = 0;
117 StartupInfo.lpReserved2 = 0;
118 Result = CreateProcessW(
119 NULL,
120 CommandLine,
121 NULL,
122 NULL,
123 FALSE,
124 DETACHED_PROCESS,
125 NULL,
126 NULL,
127 &StartupInfo,
128 &ProcessInformation);
129 if (!Result)
130 {
131 TRACE("Failed to run setup process\n");
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 TRACE ("RunSetup() done\n");
145
146 return TRUE;
147 }
148
149 BOOL
150 RunSetup(VOID)
151 {
152 HANDLE hThread;
153
154 hThread = CreateThread(NULL, 0, RunSetupThreadProc, NULL, 0, NULL);
155 return hThread != NULL;
156 }
157
158 /* EOF */