ntdll: Translate signal to trap when trap code is 0 on ARM.
[wine.git] / programs / wineboot / wineboot.c
blobb85a3b6b6ea9fb596648c4c97980d47b20ad1eda
1 /*
2 * Copyright (C) 2002 Andreas Mohr
3 * Copyright (C) 2002 Shachar Shemesh
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 /* Wine "bootup" handler application
21 * This app handles the various "hooks" windows allows for applications to perform
22 * as part of the bootstrap process. These are roughly divided into three types.
23 * Knowledge base articles that explain this are 137367, 179365, 232487 and 232509.
24 * Also, 119941 has some info on grpconv.exe
25 * The operations performed are (by order of execution):
27 * Preboot (prior to fully loading the Windows kernel):
28 * - wininit.exe (rename operations left in wininit.ini - Win 9x only)
29 * - PendingRenameOperations (rename operations left in the registry - Win NT+ only)
31 * Startup (before the user logs in)
32 * - Services (NT)
33 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce (9x, asynch)
34 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices (9x, asynch)
36 * After log in
37 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, synch)
38 * - HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
39 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run (all, asynch)
40 * - Startup folders (all, ?asynch?)
41 * - HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce (all, asynch)
43 * Somewhere in there is processing the RunOnceEx entries (also no imp)
45 * Bugs:
46 * - If a pending rename registry does not start with \??\ the entry is
47 * processed anyways. I'm not sure that is the Windows behaviour.
48 * - Need to check what is the windows behaviour when trying to delete files
49 * and directories that are read-only
50 * - In the pending rename registry processing - there are no traces of the files
51 * processed (requires translations from Unicode to Ansi).
54 #include "config.h"
55 #include "wine/port.h"
57 #define COBJMACROS
58 #define WIN32_LEAN_AND_MEAN
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #ifdef HAVE_GETOPT_H
65 # include <getopt.h>
66 #endif
67 #ifdef HAVE_SYS_STAT_H
68 # include <sys/stat.h>
69 #endif
70 #ifdef HAVE_UNISTD_H
71 # include <unistd.h>
72 #endif
73 #include <windows.h>
74 #include <winternl.h>
75 #include <wine/svcctl.h>
76 #include <wine/unicode.h>
77 #include <wine/library.h>
78 #include <wine/debug.h>
80 #include <shlobj.h>
81 #include <shobjidl.h>
82 #include <shlwapi.h>
83 #include <shellapi.h>
84 #include "resource.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
88 extern BOOL shutdown_close_windows( BOOL force );
89 extern BOOL shutdown_all_desktops( BOOL force );
90 extern void kill_processes( BOOL kill_desktop );
92 static WCHAR windowsdir[MAX_PATH];
94 /* retrieve the (unix) path to the wine.inf file */
95 static char *get_wine_inf_path(void)
97 const char *build_dir, *data_dir;
98 char *name = NULL;
100 if ((data_dir = wine_get_data_dir()))
102 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(data_dir) + sizeof("/wine.inf") )))
103 return NULL;
104 strcpy( name, data_dir );
105 strcat( name, "/wine.inf" );
107 else if ((build_dir = wine_get_build_dir()))
109 if (!(name = HeapAlloc( GetProcessHeap(), 0, strlen(build_dir) + sizeof("/loader/wine.inf") )))
110 return NULL;
111 strcpy( name, build_dir );
112 strcat( name, "/loader/wine.inf" );
114 return name;
117 /* update the timestamp if different from the reference time */
118 static BOOL update_timestamp( const char *config_dir, unsigned long timestamp )
120 BOOL ret = FALSE;
121 int fd, count;
122 char buffer[100];
123 char *file = HeapAlloc( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/.update-timestamp") );
125 if (!file) return FALSE;
126 strcpy( file, config_dir );
127 strcat( file, "/.update-timestamp" );
129 if ((fd = open( file, O_RDWR )) != -1)
131 if ((count = read( fd, buffer, sizeof(buffer) - 1 )) >= 0)
133 buffer[count] = 0;
134 if (!strncmp( buffer, "disable", sizeof("disable")-1 )) goto done;
135 if (timestamp == strtoul( buffer, NULL, 10 )) goto done;
137 lseek( fd, 0, SEEK_SET );
138 ftruncate( fd, 0 );
140 else
142 if (errno != ENOENT) goto done;
143 if ((fd = open( file, O_WRONLY | O_CREAT | O_TRUNC, 0666 )) == -1) goto done;
146 count = sprintf( buffer, "%lu\n", timestamp );
147 if (write( fd, buffer, count ) != count)
149 WINE_WARN( "failed to update timestamp in %s\n", file );
150 ftruncate( fd, 0 );
152 else ret = TRUE;
154 done:
155 if (fd != -1) close( fd );
156 HeapFree( GetProcessHeap(), 0, file );
157 return ret;
160 /* wrapper for RegSetValueExW */
161 static DWORD set_reg_value( HKEY hkey, const WCHAR *name, const WCHAR *value )
163 return RegSetValueExW( hkey, name, 0, REG_SZ, (const BYTE *)value, (strlenW(value) + 1) * sizeof(WCHAR) );
166 extern void do_cpuid( unsigned int ax, unsigned int *p );
167 #if defined(_MSC_VER)
168 void do_cpuid( unsigned int ax, unsigned int *p )
170 __cpuid( p, ax );
172 #elif defined(__i386__)
173 __ASM_GLOBAL_FUNC( do_cpuid,
174 "pushl %esi\n\t"
175 "pushl %ebx\n\t"
176 "movl 12(%esp),%eax\n\t"
177 "movl 16(%esp),%esi\n\t"
178 "cpuid\n\t"
179 "movl %eax,(%esi)\n\t"
180 "movl %ebx,4(%esi)\n\t"
181 "movl %ecx,8(%esi)\n\t"
182 "movl %edx,12(%esi)\n\t"
183 "popl %ebx\n\t"
184 "popl %esi\n\t"
185 "ret" )
186 #elif defined(__x86_64__)
187 __ASM_GLOBAL_FUNC( do_cpuid,
188 "pushq %rbx\n\t"
189 "movl %edi,%eax\n\t"
190 "cpuid\n\t"
191 "movl %eax,(%rsi)\n\t"
192 "movl %ebx,4(%rsi)\n\t"
193 "movl %ecx,8(%rsi)\n\t"
194 "movl %edx,12(%rsi)\n\t"
195 "popq %rbx\n\t"
196 "ret" )
197 #else
198 void do_cpuid( unsigned int ax, unsigned int *p )
200 FIXME("\n");
202 #endif
204 static void regs_to_str( unsigned int *regs, unsigned int len, WCHAR *buffer )
206 unsigned int i;
207 unsigned char *p = (unsigned char *)regs;
209 for (i = 0; i < len; i++) { buffer[i] = *p++; }
210 buffer[i] = 0;
213 static unsigned int get_model( unsigned int reg0, unsigned int *stepping, unsigned int *family )
215 unsigned int model, family_id = (reg0 & (0x0f << 8)) >> 8;
217 model = (reg0 & (0x0f << 4)) >> 4;
218 if (family_id == 6 || family_id == 15) model |= (reg0 & (0x0f << 16)) >> 12;
220 *family = family_id;
221 if (family_id == 15) *family += (reg0 & (0xff << 20)) >> 20;
223 *stepping = reg0 & 0x0f;
224 return model;
227 static void get_identifier( WCHAR *buf, const WCHAR *arch )
229 static const WCHAR fmtW[] = {'%','s',' ','F','a','m','i','l','y',' ','%','u',' ','M','o','d','e','l',
230 ' ','%','u',' ','S','t','e','p','p','i','n','g',' ','%','u',0};
231 unsigned int regs[4] = {0, 0, 0, 0}, family, model, stepping;
233 do_cpuid( 1, regs );
234 model = get_model( regs[0], &stepping, &family );
235 sprintfW( buf, fmtW, arch, family, model, stepping );
238 static void get_vendorid( WCHAR *buf )
240 unsigned int tmp, regs[4] = {0, 0, 0, 0};
242 do_cpuid( 0, regs );
243 tmp = regs[2]; /* swap edx and ecx */
244 regs[2] = regs[3];
245 regs[3] = tmp;
247 regs_to_str( regs + 1, 12, buf );
250 static void get_namestring( WCHAR *buf )
252 unsigned int regs[4] = {0, 0, 0, 0};
253 int i;
255 do_cpuid( 0x80000000, regs );
256 if (regs[0] >= 0x80000004)
258 do_cpuid( 0x80000002, regs );
259 regs_to_str( regs, 16, buf );
260 do_cpuid( 0x80000003, regs );
261 regs_to_str( regs, 16, buf + 16 );
262 do_cpuid( 0x80000004, regs );
263 regs_to_str( regs, 16, buf + 32 );
265 for (i = strlenW(buf) - 1; i >= 0 && buf[i] == ' '; i--) buf[i] = 0;
268 /* create the volatile hardware registry keys */
269 static void create_hardware_registry_keys(void)
271 static const WCHAR SystemW[] = {'H','a','r','d','w','a','r','e','\\','D','e','s','c','r','i','p','t','i','o','n','\\',
272 'S','y','s','t','e','m',0};
273 static const WCHAR fpuW[] = {'F','l','o','a','t','i','n','g','P','o','i','n','t','P','r','o','c','e','s','s','o','r',0};
274 static const WCHAR cpuW[] = {'C','e','n','t','r','a','l','P','r','o','c','e','s','s','o','r',0};
275 static const WCHAR FeatureSetW[] = {'F','e','a','t','u','r','e','S','e','t',0};
276 static const WCHAR IdentifierW[] = {'I','d','e','n','t','i','f','i','e','r',0};
277 static const WCHAR ProcessorNameStringW[] = {'P','r','o','c','e','s','s','o','r','N','a','m','e','S','t','r','i','n','g',0};
278 static const WCHAR SysidW[] = {'A','T',' ','c','o','m','p','a','t','i','b','l','e',0};
279 static const WCHAR ARMSysidW[] = {'A','R','M',' ','p','r','o','c','e','s','s','o','r',' ','f','a','m','i','l','y',0};
280 static const WCHAR mhzKeyW[] = {'~','M','H','z',0};
281 static const WCHAR VendorIdentifierW[] = {'V','e','n','d','o','r','I','d','e','n','t','i','f','i','e','r',0};
282 static const WCHAR PercentDW[] = {'%','d',0};
283 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
284 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
285 static const WCHAR x86W[] = {'x','8','6',0};
286 static const WCHAR intel64W[] = {'I','n','t','e','l','6','4',0};
287 static const WCHAR amd64W[] = {'A','M','D','6','4',0};
288 static const WCHAR authenticamdW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0};
289 unsigned int i;
290 HKEY hkey, system_key, cpu_key, fpu_key;
291 SYSTEM_CPU_INFORMATION sci;
292 PROCESSOR_POWER_INFORMATION* power_info;
293 ULONG sizeof_power_info = sizeof(PROCESSOR_POWER_INFORMATION) * NtCurrentTeb()->Peb->NumberOfProcessors;
294 WCHAR id[60], namestr[49], vendorid[13];
296 get_namestring( namestr );
297 get_vendorid( vendorid );
298 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
300 power_info = HeapAlloc( GetProcessHeap(), 0, sizeof_power_info );
301 if (power_info == NULL)
302 return;
303 if (NtPowerInformation( ProcessorInformation, NULL, 0, power_info, sizeof_power_info ))
304 memset( power_info, 0, sizeof_power_info );
306 switch (sci.Architecture)
308 case PROCESSOR_ARCHITECTURE_ARM:
309 case PROCESSOR_ARCHITECTURE_ARM64:
310 sprintfW( id, ARMCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
311 break;
313 case PROCESSOR_ARCHITECTURE_AMD64:
314 get_identifier( id, !strcmpW(vendorid, authenticamdW) ? amd64W : intel64W );
315 break;
317 case PROCESSOR_ARCHITECTURE_INTEL:
318 default:
319 get_identifier( id, x86W );
320 break;
323 if (RegCreateKeyExW( HKEY_LOCAL_MACHINE, SystemW, 0, NULL, REG_OPTION_VOLATILE,
324 KEY_ALL_ACCESS, NULL, &system_key, NULL ))
326 HeapFree( GetProcessHeap(), 0, power_info );
327 return;
330 switch (sci.Architecture)
332 case PROCESSOR_ARCHITECTURE_ARM:
333 case PROCESSOR_ARCHITECTURE_ARM64:
334 set_reg_value( system_key, IdentifierW, ARMSysidW );
335 break;
337 case PROCESSOR_ARCHITECTURE_INTEL:
338 case PROCESSOR_ARCHITECTURE_AMD64:
339 default:
340 set_reg_value( system_key, IdentifierW, SysidW );
341 break;
344 if (sci.Architecture == PROCESSOR_ARCHITECTURE_ARM ||
345 sci.Architecture == PROCESSOR_ARCHITECTURE_ARM64 ||
346 RegCreateKeyExW( system_key, fpuW, 0, NULL, REG_OPTION_VOLATILE,
347 KEY_ALL_ACCESS, NULL, &fpu_key, NULL ))
348 fpu_key = 0;
349 if (RegCreateKeyExW( system_key, cpuW, 0, NULL, REG_OPTION_VOLATILE,
350 KEY_ALL_ACCESS, NULL, &cpu_key, NULL ))
351 cpu_key = 0;
353 for (i = 0; i < NtCurrentTeb()->Peb->NumberOfProcessors; i++)
355 WCHAR numW[10];
357 sprintfW( numW, PercentDW, i );
358 if (!RegCreateKeyExW( cpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
359 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
361 RegSetValueExW( hkey, FeatureSetW, 0, REG_DWORD, (BYTE *)&sci.FeatureSet, sizeof(DWORD) );
362 set_reg_value( hkey, IdentifierW, id );
363 /* TODO: report ARM properly */
364 set_reg_value( hkey, ProcessorNameStringW, namestr );
365 set_reg_value( hkey, VendorIdentifierW, vendorid );
366 RegSetValueExW( hkey, mhzKeyW, 0, REG_DWORD, (BYTE *)&power_info[i].MaxMhz, sizeof(DWORD) );
367 RegCloseKey( hkey );
369 if (sci.Architecture != PROCESSOR_ARCHITECTURE_ARM &&
370 sci.Architecture != PROCESSOR_ARCHITECTURE_ARM64 &&
371 !RegCreateKeyExW( fpu_key, numW, 0, NULL, REG_OPTION_VOLATILE,
372 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
374 set_reg_value( hkey, IdentifierW, id );
375 RegCloseKey( hkey );
378 RegCloseKey( fpu_key );
379 RegCloseKey( cpu_key );
380 RegCloseKey( system_key );
381 HeapFree( GetProcessHeap(), 0, power_info );
385 /* create the DynData registry keys */
386 static void create_dynamic_registry_keys(void)
388 static const WCHAR StatDataW[] = {'P','e','r','f','S','t','a','t','s','\\',
389 'S','t','a','t','D','a','t','a',0};
390 static const WCHAR ConfigManagerW[] = {'C','o','n','f','i','g',' ','M','a','n','a','g','e','r','\\',
391 'E','n','u','m',0};
392 HKEY key;
394 if (!RegCreateKeyExW( HKEY_DYN_DATA, StatDataW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
395 RegCloseKey( key );
396 if (!RegCreateKeyExW( HKEY_DYN_DATA, ConfigManagerW, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL ))
397 RegCloseKey( key );
400 /* create the platform-specific environment registry keys */
401 static void create_environment_registry_keys( void )
403 static const WCHAR EnvironW[] = {'S','y','s','t','e','m','\\',
404 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
405 'C','o','n','t','r','o','l','\\',
406 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
407 'E','n','v','i','r','o','n','m','e','n','t',0};
408 static const WCHAR NumProcW[] = {'N','U','M','B','E','R','_','O','F','_','P','R','O','C','E','S','S','O','R','S',0};
409 static const WCHAR ProcArchW[] = {'P','R','O','C','E','S','S','O','R','_','A','R','C','H','I','T','E','C','T','U','R','E',0};
410 static const WCHAR x86W[] = {'x','8','6',0};
411 static const WCHAR intel64W[] = {'I','n','t','e','l','6','4',0};
412 static const WCHAR amd64W[] = {'A','M','D','6','4',0};
413 static const WCHAR authenticamdW[] = {'A','u','t','h','e','n','t','i','c','A','M','D',0};
414 static const WCHAR commaW[] = {',',' ',0};
415 static const WCHAR ProcIdW[] = {'P','R','O','C','E','S','S','O','R','_','I','D','E','N','T','I','F','I','E','R',0};
416 static const WCHAR ProcLvlW[] = {'P','R','O','C','E','S','S','O','R','_','L','E','V','E','L',0};
417 static const WCHAR ProcRevW[] = {'P','R','O','C','E','S','S','O','R','_','R','E','V','I','S','I','O','N',0};
418 static const WCHAR PercentDW[] = {'%','d',0};
419 static const WCHAR Percent04XW[] = {'%','0','4','x',0};
420 static const WCHAR ARMCpuDescrW[] = {'A','R','M',' ','F','a','m','i','l','y',' ','%','d',' ','M','o','d','e','l',' ','%','d',
421 ' ','R','e','v','i','s','i','o','n',' ','%','d',0};
422 HKEY env_key;
423 SYSTEM_CPU_INFORMATION sci;
424 WCHAR buffer[60], vendorid[13];
425 const WCHAR *arch, *parch;
427 if (RegCreateKeyW( HKEY_LOCAL_MACHINE, EnvironW, &env_key )) return;
429 get_vendorid( vendorid );
430 NtQuerySystemInformation( SystemCpuInformation, &sci, sizeof(sci), NULL );
432 sprintfW( buffer, PercentDW, NtCurrentTeb()->Peb->NumberOfProcessors );
433 set_reg_value( env_key, NumProcW, buffer );
435 switch (sci.Architecture)
437 case PROCESSOR_ARCHITECTURE_AMD64:
438 arch = amd64W;
439 parch = !strcmpW(vendorid, authenticamdW) ? amd64W : intel64W;
440 break;
442 case PROCESSOR_ARCHITECTURE_INTEL:
443 default:
444 arch = parch = x86W;
445 break;
447 set_reg_value( env_key, ProcArchW, arch );
449 switch (sci.Architecture)
451 case PROCESSOR_ARCHITECTURE_ARM:
452 case PROCESSOR_ARCHITECTURE_ARM64:
453 sprintfW( buffer, ARMCpuDescrW, sci.Level, HIBYTE(sci.Revision), LOBYTE(sci.Revision) );
454 break;
456 case PROCESSOR_ARCHITECTURE_AMD64:
457 case PROCESSOR_ARCHITECTURE_INTEL:
458 default:
459 get_identifier( buffer, parch );
460 strcatW( buffer, commaW );
461 strcatW( buffer, vendorid );
462 break;
464 set_reg_value( env_key, ProcIdW, buffer );
466 sprintfW( buffer, PercentDW, sci.Level );
467 set_reg_value( env_key, ProcLvlW, buffer );
469 sprintfW( buffer, Percent04XW, sci.Revision );
470 set_reg_value( env_key, ProcRevW, buffer );
472 RegCloseKey( env_key );
475 static void create_volatile_environment_registry_key(void)
477 static const WCHAR VolatileEnvW[] = {'V','o','l','a','t','i','l','e',' ','E','n','v','i','r','o','n','m','e','n','t',0};
478 static const WCHAR AppDataW[] = {'A','P','P','D','A','T','A',0};
479 static const WCHAR ClientNameW[] = {'C','L','I','E','N','T','N','A','M','E',0};
480 static const WCHAR HomeDriveW[] = {'H','O','M','E','D','R','I','V','E',0};
481 static const WCHAR HomePathW[] = {'H','O','M','E','P','A','T','H',0};
482 static const WCHAR HomeShareW[] = {'H','O','M','E','S','H','A','R','E',0};
483 static const WCHAR LocalAppDataW[] = {'L','O','C','A','L','A','P','P','D','A','T','A',0};
484 static const WCHAR LogonServerW[] = {'L','O','G','O','N','S','E','R','V','E','R',0};
485 static const WCHAR SessionNameW[] = {'S','E','S','S','I','O','N','N','A','M','E',0};
486 static const WCHAR UserNameW[] = {'U','S','E','R','N','A','M','E',0};
487 static const WCHAR UserDomainW[] = {'U','S','E','R','D','O','M','A','I','N',0};
488 static const WCHAR UserProfileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
489 static const WCHAR ConsoleW[] = {'C','o','n','s','o','l','e',0};
490 static const WCHAR EmptyW[] = {0};
491 WCHAR path[MAX_PATH];
492 WCHAR computername[MAX_COMPUTERNAME_LENGTH + 1 + 2];
493 DWORD size;
494 HKEY hkey;
495 HRESULT hr;
497 if (RegCreateKeyExW( HKEY_CURRENT_USER, VolatileEnvW, 0, NULL, REG_OPTION_VOLATILE,
498 KEY_ALL_ACCESS, NULL, &hkey, NULL ))
499 return;
501 hr = SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
502 if (SUCCEEDED(hr)) set_reg_value( hkey, AppDataW, path );
504 set_reg_value( hkey, ClientNameW, ConsoleW );
506 /* Write the profile path's drive letter and directory components into
507 * HOMEDRIVE and HOMEPATH respectively. */
508 hr = SHGetFolderPathW( NULL, CSIDL_PROFILE | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
509 if (SUCCEEDED(hr))
511 set_reg_value( hkey, UserProfileW, path );
512 set_reg_value( hkey, HomePathW, path + 2 );
513 path[2] = '\0';
514 set_reg_value( hkey, HomeDriveW, path );
517 size = ARRAY_SIZE(path);
518 if (GetUserNameW( path, &size )) set_reg_value( hkey, UserNameW, path );
520 set_reg_value( hkey, HomeShareW, EmptyW );
522 hr = SHGetFolderPathW( NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, path );
523 if (SUCCEEDED(hr))
524 set_reg_value( hkey, LocalAppDataW, path );
526 size = ARRAY_SIZE(computername) - 2;
527 if (GetComputerNameW(&computername[2], &size))
529 set_reg_value( hkey, UserDomainW, &computername[2] );
530 computername[0] = computername[1] = '\\';
531 set_reg_value( hkey, LogonServerW, computername );
534 set_reg_value( hkey, SessionNameW, ConsoleW );
535 RegCloseKey( hkey );
538 /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
539 * Returns FALSE if there was an error, or otherwise if all is ok.
541 static BOOL wininit(void)
543 static const WCHAR nulW[] = {'N','U','L',0};
544 static const WCHAR renameW[] = {'r','e','n','a','m','e',0};
545 static const WCHAR wininitW[] = {'w','i','n','i','n','i','t','.','i','n','i',0};
546 static const WCHAR wininitbakW[] = {'w','i','n','i','n','i','t','.','b','a','k',0};
547 WCHAR initial_buffer[1024];
548 WCHAR *str, *buffer = initial_buffer;
549 DWORD size = ARRAY_SIZE(initial_buffer);
550 DWORD res;
552 for (;;)
554 if (!(res = GetPrivateProfileSectionW( renameW, buffer, size, wininitW ))) return TRUE;
555 if (res < size - 2) break;
556 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
557 size *= 2;
558 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
561 for (str = buffer; *str; str += strlenW(str) + 1)
563 WCHAR *value;
565 if (*str == ';') continue; /* comment */
566 if (!(value = strchrW( str, '=' ))) continue;
568 /* split the line into key and value */
569 *value++ = 0;
571 if (!lstrcmpiW( nulW, str ))
573 WINE_TRACE("Deleting file %s\n", wine_dbgstr_w(value) );
574 if( !DeleteFileW( value ) )
575 WINE_WARN("Error deleting file %s\n", wine_dbgstr_w(value) );
577 else
579 WINE_TRACE("Renaming file %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
581 if( !MoveFileExW(value, str, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) )
582 WINE_WARN("Error renaming %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
584 str = value;
587 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
589 if( !MoveFileExW( wininitW, wininitbakW, MOVEFILE_REPLACE_EXISTING) )
591 WINE_ERR("Couldn't rename wininit.ini, error %d\n", GetLastError() );
593 return FALSE;
596 return TRUE;
599 static BOOL pendingRename(void)
601 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
602 'F','i','l','e','R','e','n','a','m','e',
603 'O','p','e','r','a','t','i','o','n','s',0};
604 static const WCHAR SessionW[] = { 'S','y','s','t','e','m','\\',
605 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
606 'C','o','n','t','r','o','l','\\',
607 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
608 WCHAR *buffer=NULL;
609 const WCHAR *src=NULL, *dst=NULL;
610 DWORD dataLength=0;
611 HKEY hSession=NULL;
612 DWORD res;
614 WINE_TRACE("Entered\n");
616 if( (res=RegOpenKeyExW( HKEY_LOCAL_MACHINE, SessionW, 0, KEY_ALL_ACCESS, &hSession ))
617 !=ERROR_SUCCESS )
619 WINE_TRACE("The key was not found - skipping\n");
620 return TRUE;
623 res=RegQueryValueExW( hSession, ValueName, NULL, NULL /* The value type does not really interest us, as it is not
624 truly a REG_MULTI_SZ anyways */,
625 NULL, &dataLength );
626 if( res==ERROR_FILE_NOT_FOUND )
628 /* No value - nothing to do. Great! */
629 WINE_TRACE("Value not present - nothing to rename\n");
630 res=TRUE;
631 goto end;
634 if( res!=ERROR_SUCCESS )
636 WINE_ERR("Couldn't query value's length (%d)\n", res );
637 res=FALSE;
638 goto end;
641 buffer=HeapAlloc( GetProcessHeap(),0,dataLength );
642 if( buffer==NULL )
644 WINE_ERR("Couldn't allocate %u bytes for the value\n", dataLength );
645 res=FALSE;
646 goto end;
649 res=RegQueryValueExW( hSession, ValueName, NULL, NULL, (LPBYTE)buffer, &dataLength );
650 if( res!=ERROR_SUCCESS )
652 WINE_ERR("Couldn't query value after successfully querying before (%u),\n"
653 "please report to wine-devel@winehq.org\n", res);
654 res=FALSE;
655 goto end;
658 /* Make sure that the data is long enough and ends with two NULLs. This
659 * simplifies the code later on.
661 if( dataLength<2*sizeof(buffer[0]) ||
662 buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
663 buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
665 WINE_ERR("Improper value format - doesn't end with NULL\n");
666 res=FALSE;
667 goto end;
670 for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
671 src=dst+lstrlenW(dst)+1 )
673 DWORD dwFlags=0;
675 WINE_TRACE("processing next command\n");
677 dst=src+lstrlenW(src)+1;
679 /* We need to skip the \??\ header */
680 if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
681 src+=4;
683 if( dst[0]=='!' )
685 dwFlags|=MOVEFILE_REPLACE_EXISTING;
686 dst++;
689 if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
690 dst+=4;
692 if( *dst!='\0' )
694 /* Rename the file */
695 MoveFileExW( src, dst, dwFlags );
696 } else
698 /* Delete the file or directory */
699 if (!RemoveDirectoryW( src ) && GetLastError() == ERROR_DIRECTORY) DeleteFileW( src );
703 if((res=RegDeleteValueW(hSession, ValueName))!=ERROR_SUCCESS )
705 WINE_ERR("Error deleting the value (%u)\n", GetLastError() );
706 res=FALSE;
707 } else
708 res=TRUE;
710 end:
711 HeapFree(GetProcessHeap(), 0, buffer);
713 if( hSession!=NULL )
714 RegCloseKey( hSession );
716 return res;
719 #define INVALID_RUNCMD_RETURN -1
721 * This function runs the specified command in the specified dir.
722 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
723 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
724 * [in] wait - whether to wait for the run program to finish before returning.
725 * [in] minimized - Whether to ask the program to run minimized.
727 * Returns:
728 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
729 * If wait is FALSE - returns 0 if successful.
730 * If wait is TRUE - returns the program's return value.
732 static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
734 STARTUPINFOW si;
735 PROCESS_INFORMATION info;
736 DWORD exit_code=0;
738 memset(&si, 0, sizeof(si));
739 si.cb=sizeof(si);
740 if( minimized )
742 si.dwFlags=STARTF_USESHOWWINDOW;
743 si.wShowWindow=SW_MINIMIZE;
745 memset(&info, 0, sizeof(info));
747 if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
749 WINE_WARN("Failed to run command %s (%d)\n", wine_dbgstr_w(cmdline), GetLastError() );
750 return INVALID_RUNCMD_RETURN;
753 WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
754 wine_dbgstr_w(cmdline), info.hProcess );
756 if(wait)
757 { /* wait for the process to exit */
758 WaitForSingleObject(info.hProcess, INFINITE);
759 GetExitCodeProcess(info.hProcess, &exit_code);
762 CloseHandle( info.hThread );
763 CloseHandle( info.hProcess );
765 return exit_code;
769 * Process a "Run" type registry key.
770 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
771 * opened.
772 * szKeyName is the key holding the actual entries.
773 * bDelete tells whether we should delete each value right before executing it.
774 * bSynchronous tells whether we should wait for the prog to complete before
775 * going on to the next prog.
777 static BOOL ProcessRunKeys( HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
778 BOOL bSynchronous )
780 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
781 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
782 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
783 HKEY hkWin, hkRun;
784 DWORD res, dispos;
785 DWORD i, nMaxCmdLine=0, nMaxValue=0;
786 WCHAR *szCmdLine=NULL;
787 WCHAR *szValue=NULL;
789 if (hkRoot==HKEY_LOCAL_MACHINE)
790 WINE_TRACE("processing %s entries under HKLM\n",wine_dbgstr_w(szKeyName) );
791 else
792 WINE_TRACE("processing %s entries under HKCU\n",wine_dbgstr_w(szKeyName) );
794 if (RegCreateKeyExW( hkRoot, WINKEY_NAME, 0, NULL, 0, KEY_READ, NULL, &hkWin, NULL ) != ERROR_SUCCESS)
795 return TRUE;
797 if ((res = RegCreateKeyExW( hkWin, szKeyName, 0, NULL, 0, bDelete ? KEY_ALL_ACCESS : KEY_READ,
798 NULL, &hkRun, &dispos )) != ERROR_SUCCESS)
800 RegCloseKey( hkWin );
801 return TRUE;
803 RegCloseKey( hkWin );
804 if (dispos == REG_CREATED_NEW_KEY) goto end;
806 if( (res=RegQueryInfoKeyW( hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
807 &nMaxCmdLine, NULL, NULL ))!=ERROR_SUCCESS )
808 goto end;
810 if( i==0 )
812 WINE_TRACE("No commands to execute.\n");
814 res=ERROR_SUCCESS;
815 goto end;
818 if( (szCmdLine=HeapAlloc(GetProcessHeap(),0,nMaxCmdLine))==NULL )
820 WINE_ERR("Couldn't allocate memory for the commands to be executed\n");
822 res=ERROR_NOT_ENOUGH_MEMORY;
823 goto end;
826 if( (szValue=HeapAlloc(GetProcessHeap(),0,(++nMaxValue)*sizeof(*szValue)))==NULL )
828 WINE_ERR("Couldn't allocate memory for the value names\n");
830 res=ERROR_NOT_ENOUGH_MEMORY;
831 goto end;
834 while( i>0 )
836 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
837 DWORD type;
839 --i;
841 if( (res=RegEnumValueW( hkRun, i, szValue, &nValLength, 0, &type,
842 (LPBYTE)szCmdLine, &nDataLength ))!=ERROR_SUCCESS )
844 WINE_ERR("Couldn't read in value %d - %d\n", i, res );
846 continue;
849 if( bDelete && (res=RegDeleteValueW( hkRun, szValue ))!=ERROR_SUCCESS )
851 WINE_ERR("Couldn't delete value - %d, %d. Running command anyways.\n", i, res );
854 if( type!=REG_SZ )
856 WINE_ERR("Incorrect type of value #%d (%d)\n", i, type );
858 continue;
861 if( (res=runCmd(szCmdLine, NULL, bSynchronous, FALSE ))==INVALID_RUNCMD_RETURN )
863 WINE_ERR("Error running cmd %s (%d)\n", wine_dbgstr_w(szCmdLine), GetLastError() );
866 WINE_TRACE("Done processing cmd #%d\n", i);
869 res=ERROR_SUCCESS;
871 end:
872 HeapFree( GetProcessHeap(), 0, szValue );
873 HeapFree( GetProcessHeap(), 0, szCmdLine );
875 if( hkRun!=NULL )
876 RegCloseKey( hkRun );
878 WINE_TRACE("done\n");
880 return res==ERROR_SUCCESS;
884 * WFP is Windows File Protection, in NT5 and Windows 2000 it maintains a cache
885 * of known good dlls and scans through and replaces corrupted DLLs with these
886 * known good versions. The only programs that should install into this dll
887 * cache are Windows Updates and IE (which is treated like a Windows Update)
889 * Implementing this allows installing ie in win2k mode to actually install the
890 * system dlls that we expect and need
892 static int ProcessWindowsFileProtection(void)
894 static const WCHAR winlogonW[] = {'S','o','f','t','w','a','r','e','\\',
895 'M','i','c','r','o','s','o','f','t','\\',
896 'W','i','n','d','o','w','s',' ','N','T','\\',
897 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
898 'W','i','n','l','o','g','o','n',0};
899 static const WCHAR cachedirW[] = {'S','F','C','D','l','l','C','a','c','h','e','D','i','r',0};
900 static const WCHAR dllcacheW[] = {'\\','d','l','l','c','a','c','h','e','\\','*',0};
901 static const WCHAR wildcardW[] = {'\\','*',0};
902 WIN32_FIND_DATAW finddata;
903 HANDLE find_handle;
904 BOOL find_rc;
905 DWORD rc;
906 HKEY hkey;
907 LPWSTR dllcache = NULL;
909 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, winlogonW, &hkey ))
911 DWORD sz = 0;
912 if (!RegQueryValueExW( hkey, cachedirW, 0, NULL, NULL, &sz))
914 sz += sizeof(WCHAR);
915 dllcache = HeapAlloc(GetProcessHeap(),0,sz + sizeof(wildcardW));
916 RegQueryValueExW( hkey, cachedirW, 0, NULL, (LPBYTE)dllcache, &sz);
917 strcatW( dllcache, wildcardW );
920 RegCloseKey(hkey);
922 if (!dllcache)
924 DWORD sz = GetSystemDirectoryW( NULL, 0 );
925 dllcache = HeapAlloc( GetProcessHeap(), 0, sz * sizeof(WCHAR) + sizeof(dllcacheW));
926 GetSystemDirectoryW( dllcache, sz );
927 strcatW( dllcache, dllcacheW );
930 find_handle = FindFirstFileW(dllcache,&finddata);
931 dllcache[ strlenW(dllcache) - 2] = 0; /* strip off wildcard */
932 find_rc = find_handle != INVALID_HANDLE_VALUE;
933 while (find_rc)
935 static const WCHAR dotW[] = {'.',0};
936 static const WCHAR dotdotW[] = {'.','.',0};
937 WCHAR targetpath[MAX_PATH];
938 WCHAR currentpath[MAX_PATH];
939 UINT sz;
940 UINT sz2;
941 WCHAR tempfile[MAX_PATH];
943 if (strcmpW(finddata.cFileName,dotW) == 0 || strcmpW(finddata.cFileName,dotdotW) == 0)
945 find_rc = FindNextFileW(find_handle,&finddata);
946 continue;
949 sz = MAX_PATH;
950 sz2 = MAX_PATH;
951 VerFindFileW(VFFF_ISSHAREDFILE, finddata.cFileName, windowsdir,
952 windowsdir, currentpath, &sz, targetpath, &sz2);
953 sz = MAX_PATH;
954 rc = VerInstallFileW(0, finddata.cFileName, finddata.cFileName,
955 dllcache, targetpath, currentpath, tempfile, &sz);
956 if (rc != ERROR_SUCCESS)
958 WINE_WARN("WFP: %s error 0x%x\n",wine_dbgstr_w(finddata.cFileName),rc);
959 DeleteFileW(tempfile);
962 /* now delete the source file so that we don't try to install it over and over again */
963 lstrcpynW( targetpath, dllcache, MAX_PATH - 1 );
964 sz = strlenW( targetpath );
965 targetpath[sz++] = '\\';
966 lstrcpynW( targetpath + sz, finddata.cFileName, MAX_PATH - sz );
967 if (!DeleteFileW( targetpath ))
968 WINE_WARN( "failed to delete %s: error %u\n", wine_dbgstr_w(targetpath), GetLastError() );
970 find_rc = FindNextFileW(find_handle,&finddata);
972 FindClose(find_handle);
973 HeapFree(GetProcessHeap(),0,dllcache);
974 return 1;
977 static BOOL start_services_process(void)
979 static const WCHAR svcctl_started_event[] = SVCCTL_STARTED_EVENT;
980 static const WCHAR services[] = {'\\','s','e','r','v','i','c','e','s','.','e','x','e',0};
981 PROCESS_INFORMATION pi;
982 STARTUPINFOW si;
983 HANDLE wait_handles[2];
984 WCHAR path[MAX_PATH];
986 if (!GetSystemDirectoryW(path, MAX_PATH - strlenW(services)))
987 return FALSE;
988 strcatW(path, services);
989 ZeroMemory(&si, sizeof(si));
990 si.cb = sizeof(si);
991 if (!CreateProcessW(path, path, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi))
993 WINE_ERR("Couldn't start services.exe: error %u\n", GetLastError());
994 return FALSE;
996 CloseHandle(pi.hThread);
998 wait_handles[0] = CreateEventW(NULL, TRUE, FALSE, svcctl_started_event);
999 wait_handles[1] = pi.hProcess;
1001 /* wait for the event to become available or the process to exit */
1002 if ((WaitForMultipleObjects(2, wait_handles, FALSE, INFINITE)) == WAIT_OBJECT_0 + 1)
1004 DWORD exit_code;
1005 GetExitCodeProcess(pi.hProcess, &exit_code);
1006 WINE_ERR("Unexpected termination of services.exe - exit code %d\n", exit_code);
1007 CloseHandle(pi.hProcess);
1008 CloseHandle(wait_handles[0]);
1009 return FALSE;
1012 CloseHandle(pi.hProcess);
1013 CloseHandle(wait_handles[0]);
1014 return TRUE;
1017 static INT_PTR CALLBACK wait_dlgproc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
1019 switch (msg)
1021 case WM_INITDIALOG:
1023 WCHAR *buffer, text[1024];
1024 const WCHAR *name = (WCHAR *)lp;
1025 HICON icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 48, 48, LR_SHARED );
1026 SendDlgItemMessageW( hwnd, IDC_WAITICON, STM_SETICON, (WPARAM)icon, 0 );
1027 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_GETTEXT, 1024, (LPARAM)text );
1028 buffer = HeapAlloc( GetProcessHeap(), 0, (strlenW(text) + strlenW(name) + 1) * sizeof(WCHAR) );
1029 sprintfW( buffer, text, name );
1030 SendDlgItemMessageW( hwnd, IDC_WAITTEXT, WM_SETTEXT, 0, (LPARAM)buffer );
1031 HeapFree( GetProcessHeap(), 0, buffer );
1033 break;
1035 return 0;
1038 static HWND show_wait_window(void)
1040 const char *config_dir = wine_get_config_dir();
1041 WCHAR *name;
1042 HWND hwnd;
1043 DWORD len;
1045 len = MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, NULL, 0 );
1046 name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1047 MultiByteToWideChar( CP_UNIXCP, 0, config_dir, -1, name, len );
1048 hwnd = CreateDialogParamW( GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_WAITDLG), 0,
1049 wait_dlgproc, (LPARAM)name );
1050 ShowWindow( hwnd, SW_SHOWNORMAL );
1051 HeapFree( GetProcessHeap(), 0, name );
1052 return hwnd;
1055 static HANDLE start_rundll32( const char *inf_path, BOOL wow64 )
1057 static const WCHAR rundll[] = {'\\','r','u','n','d','l','l','3','2','.','e','x','e',0};
1058 static const WCHAR setupapi[] = {' ','s','e','t','u','p','a','p','i',',',
1059 'I','n','s','t','a','l','l','H','i','n','f','S','e','c','t','i','o','n',0};
1060 static const WCHAR definstall[] = {' ','D','e','f','a','u','l','t','I','n','s','t','a','l','l',0};
1061 static const WCHAR wowinstall[] = {' ','W','o','w','6','4','I','n','s','t','a','l','l',0};
1062 static const WCHAR inf[] = {' ','1','2','8',' ','\\','\\','?','\\','u','n','i','x',0 };
1064 WCHAR app[MAX_PATH + ARRAY_SIZE(rundll)];
1065 STARTUPINFOW si;
1066 PROCESS_INFORMATION pi;
1067 WCHAR *buffer;
1068 DWORD inf_len, cmd_len;
1070 memset( &si, 0, sizeof(si) );
1071 si.cb = sizeof(si);
1073 if (wow64)
1075 if (!GetSystemWow64DirectoryW( app, MAX_PATH )) return 0; /* not on 64-bit */
1077 else GetSystemDirectoryW( app, MAX_PATH );
1079 strcatW( app, rundll );
1081 cmd_len = strlenW(app) * sizeof(WCHAR) + sizeof(setupapi) + sizeof(definstall) + sizeof(inf);
1082 inf_len = MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, NULL, 0 );
1084 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, cmd_len + inf_len * sizeof(WCHAR) ))) return 0;
1086 strcpyW( buffer, app );
1087 strcatW( buffer, setupapi );
1088 strcatW( buffer, wow64 ? wowinstall : definstall );
1089 strcatW( buffer, inf );
1090 MultiByteToWideChar( CP_UNIXCP, 0, inf_path, -1, buffer + strlenW(buffer), inf_len );
1092 if (CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
1093 CloseHandle( pi.hThread );
1094 else
1095 pi.hProcess = 0;
1097 HeapFree( GetProcessHeap(), 0, buffer );
1098 return pi.hProcess;
1101 /* execute rundll32 on the wine.inf file if necessary */
1102 static void update_wineprefix( BOOL force )
1104 const char *config_dir = wine_get_config_dir();
1105 char *inf_path = get_wine_inf_path();
1106 int fd;
1107 struct stat st;
1109 if (!inf_path)
1111 WINE_MESSAGE( "wine: failed to update %s, wine.inf not found\n", config_dir );
1112 return;
1114 if ((fd = open( inf_path, O_RDONLY )) == -1)
1116 WINE_MESSAGE( "wine: failed to update %s with %s: %s\n",
1117 config_dir, inf_path, strerror(errno) );
1118 goto done;
1120 fstat( fd, &st );
1121 close( fd );
1123 if (update_timestamp( config_dir, st.st_mtime ) || force)
1125 HANDLE process;
1126 DWORD count = 0;
1128 if ((process = start_rundll32( inf_path, FALSE )))
1130 HWND hwnd = show_wait_window();
1131 for (;;)
1133 MSG msg;
1134 DWORD res = MsgWaitForMultipleObjects( 1, &process, FALSE, INFINITE, QS_ALLINPUT );
1135 if (res == WAIT_OBJECT_0)
1137 CloseHandle( process );
1138 if (count++ || !(process = start_rundll32( inf_path, TRUE ))) break;
1140 else while (PeekMessageW( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageW( &msg );
1142 DestroyWindow( hwnd );
1144 WINE_MESSAGE( "wine: configuration in '%s' has been updated.\n", config_dir );
1147 done:
1148 HeapFree( GetProcessHeap(), 0, inf_path );
1151 /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
1152 * shell links here to restart themselves after boot. */
1153 static BOOL ProcessStartupItems(void)
1155 BOOL ret = FALSE;
1156 HRESULT hr;
1157 IMalloc *ppM = NULL;
1158 IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
1159 LPITEMIDLIST pidlStartup = NULL, pidlItem;
1160 ULONG NumPIDLs;
1161 IEnumIDList *iEnumList = NULL;
1162 STRRET strret;
1163 WCHAR wszCommand[MAX_PATH];
1165 WINE_TRACE("Processing items in the StartUp folder.\n");
1167 hr = SHGetMalloc(&ppM);
1168 if (FAILED(hr))
1170 WINE_ERR("Couldn't get IMalloc object.\n");
1171 goto done;
1174 hr = SHGetDesktopFolder(&psfDesktop);
1175 if (FAILED(hr))
1177 WINE_ERR("Couldn't get desktop folder.\n");
1178 goto done;
1181 hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
1182 if (FAILED(hr))
1184 WINE_TRACE("Couldn't get StartUp folder location.\n");
1185 goto done;
1188 hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
1189 if (FAILED(hr))
1191 WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
1192 goto done;
1195 hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
1196 if (FAILED(hr))
1198 WINE_TRACE("Unable to enumerate StartUp objects.\n");
1199 goto done;
1202 while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
1203 (NumPIDLs) == 1)
1205 hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
1206 if (FAILED(hr))
1207 WINE_TRACE("Unable to get display name of enumeration item.\n");
1208 else
1210 hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
1211 if (FAILED(hr))
1212 WINE_TRACE("Unable to parse display name.\n");
1213 else
1215 HINSTANCE hinst;
1217 hinst = ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL);
1218 if (PtrToUlong(hinst) <= 32)
1219 WINE_WARN("Error %p executing command %s.\n", hinst, wine_dbgstr_w(wszCommand));
1223 IMalloc_Free(ppM, pidlItem);
1226 /* Return success */
1227 ret = TRUE;
1229 done:
1230 if (iEnumList) IEnumIDList_Release(iEnumList);
1231 if (psfStartup) IShellFolder_Release(psfStartup);
1232 if (pidlStartup) IMalloc_Free(ppM, pidlStartup);
1234 return ret;
1237 static void usage(void)
1239 WINE_MESSAGE( "Usage: wineboot [options]\n" );
1240 WINE_MESSAGE( "Options;\n" );
1241 WINE_MESSAGE( " -h,--help Display this help message\n" );
1242 WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
1243 WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
1244 WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
1245 WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
1246 WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
1247 WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
1248 WINE_MESSAGE( " -u,--update Update the wineprefix directory\n" );
1251 static const char short_options[] = "efhikrsu";
1253 static const struct option long_options[] =
1255 { "help", 0, 0, 'h' },
1256 { "end-session", 0, 0, 'e' },
1257 { "force", 0, 0, 'f' },
1258 { "init" , 0, 0, 'i' },
1259 { "kill", 0, 0, 'k' },
1260 { "restart", 0, 0, 'r' },
1261 { "shutdown", 0, 0, 's' },
1262 { "update", 0, 0, 'u' },
1263 { NULL, 0, 0, 0 }
1266 int main( int argc, char *argv[] )
1268 static const WCHAR RunW[] = {'R','u','n',0};
1269 static const WCHAR RunOnceW[] = {'R','u','n','O','n','c','e',0};
1270 static const WCHAR RunServicesW[] = {'R','u','n','S','e','r','v','i','c','e','s',0};
1271 static const WCHAR RunServicesOnceW[] = {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0};
1272 static const WCHAR wineboot_eventW[] = {'_','_','w','i','n','e','b','o','o','t','_','e','v','e','n','t',0};
1274 /* First, set the current directory to SystemRoot */
1275 int optc;
1276 BOOL end_session, force, init, kill, restart, shutdown, update;
1277 HANDLE event;
1278 SECURITY_ATTRIBUTES sa;
1279 BOOL is_wow64;
1281 end_session = force = init = kill = restart = shutdown = update = FALSE;
1282 GetWindowsDirectoryW( windowsdir, MAX_PATH );
1283 if( !SetCurrentDirectoryW( windowsdir ) )
1284 WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
1286 if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64)
1288 STARTUPINFOW si;
1289 PROCESS_INFORMATION pi;
1290 WCHAR filename[MAX_PATH];
1291 void *redir;
1292 DWORD exit_code;
1294 memset( &si, 0, sizeof(si) );
1295 si.cb = sizeof(si);
1296 GetModuleFileNameW( 0, filename, MAX_PATH );
1298 Wow64DisableWow64FsRedirection( &redir );
1299 if (CreateProcessW( filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ))
1301 WINE_TRACE( "restarting %s\n", wine_dbgstr_w(filename) );
1302 WaitForSingleObject( pi.hProcess, INFINITE );
1303 GetExitCodeProcess( pi.hProcess, &exit_code );
1304 ExitProcess( exit_code );
1306 else WINE_ERR( "failed to restart 64-bit %s, err %d\n", wine_dbgstr_w(filename), GetLastError() );
1307 Wow64RevertWow64FsRedirection( redir );
1310 while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
1312 switch(optc)
1314 case 'e': end_session = TRUE; break;
1315 case 'f': force = TRUE; break;
1316 case 'i': init = TRUE; break;
1317 case 'k': kill = TRUE; break;
1318 case 'r': restart = TRUE; break;
1319 case 's': shutdown = TRUE; break;
1320 case 'u': update = TRUE; break;
1321 case 'h': usage(); return 0;
1322 case '?': usage(); return 1;
1326 if (end_session)
1328 if (kill)
1330 if (!shutdown_all_desktops( force )) return 1;
1332 else if (!shutdown_close_windows( force )) return 1;
1335 if (kill) kill_processes( shutdown );
1337 if (shutdown) return 0;
1339 sa.nLength = sizeof(sa);
1340 sa.lpSecurityDescriptor = NULL;
1341 sa.bInheritHandle = TRUE; /* so that services.exe inherits it */
1342 event = CreateEventW( &sa, TRUE, FALSE, wineboot_eventW );
1344 ResetEvent( event ); /* in case this is a restart */
1346 create_hardware_registry_keys();
1347 create_dynamic_registry_keys();
1348 create_environment_registry_keys();
1349 wininit();
1350 pendingRename();
1352 ProcessWindowsFileProtection();
1353 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesOnceW, TRUE, FALSE );
1355 if (init || (kill && !restart))
1357 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunServicesW, FALSE, FALSE );
1358 start_services_process();
1360 if (init || update) update_wineprefix( update );
1362 create_volatile_environment_registry_key();
1364 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunOnceW, TRUE, TRUE );
1366 if (!init && !restart)
1368 ProcessRunKeys( HKEY_LOCAL_MACHINE, RunW, FALSE, FALSE );
1369 ProcessRunKeys( HKEY_CURRENT_USER, RunW, FALSE, FALSE );
1370 ProcessStartupItems();
1373 WINE_TRACE("Operation done\n");
1375 SetEvent( event );
1376 return 0;