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