push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / programs / wineboot / wineboot.c
blob024eef3fa76486e44a734df5f4b2983a4ce4e8ec
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 WIN32_LEAN_AND_MEAN
59 #include <stdio.h>
60 #ifdef HAVE_GETOPT_H
61 # include <getopt.h>
62 #endif
63 #include <windows.h>
64 #include <wine/unicode.h>
65 #include <wine/debug.h>
67 #define COBJMACROS
68 #include <shlobj.h>
69 #include <shobjidl.h>
70 #include <shlwapi.h>
71 #include <shellapi.h>
73 WINE_DEFAULT_DEBUG_CHANNEL(wineboot);
75 #define MAX_LINE_LENGTH (2*MAX_PATH+2)
77 extern BOOL shutdown_close_windows( BOOL force );
78 extern void kill_processes( BOOL kill_desktop );
80 static WCHAR windowsdir[MAX_PATH];
82 /* Performs the rename operations dictated in %SystemRoot%\Wininit.ini.
83 * Returns FALSE if there was an error, or otherwise if all is ok.
85 static BOOL wininit(void)
87 static const WCHAR nulW[] = {'N','U','L',0};
88 static const WCHAR renameW[] = {'r','e','n','a','m','e',0};
89 static const WCHAR wininitW[] = {'w','i','n','i','n','i','t','.','i','n','i',0};
90 static const WCHAR wininitbakW[] = {'w','i','n','i','n','i','t','.','b','a','k',0};
91 WCHAR initial_buffer[1024];
92 WCHAR *str, *buffer = initial_buffer;
93 DWORD size = sizeof(initial_buffer)/sizeof(WCHAR);
94 DWORD res;
96 for (;;)
98 if (!(res = GetPrivateProfileSectionW( renameW, buffer, size, wininitW ))) return TRUE;
99 if (res < size - 2) break;
100 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
101 size *= 2;
102 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
105 for (str = buffer; *str; str += strlenW(str) + 1)
107 WCHAR *value;
109 if (*str == ';') continue; /* comment */
110 if (!(value = strchrW( str, '=' ))) continue;
112 /* split the line into key and value */
113 *value++ = 0;
115 if (!lstrcmpiW( nulW, str ))
117 WINE_TRACE("Deleting file %s\n", wine_dbgstr_w(value) );
118 if( !DeleteFileW( value ) )
119 WINE_WARN("Error deleting file %s\n", wine_dbgstr_w(value) );
121 else
123 WINE_TRACE("Renaming file %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
125 if( !MoveFileExW(value, str, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) )
126 WINE_WARN("Error renaming %s to %s\n", wine_dbgstr_w(value), wine_dbgstr_w(str) );
128 str = value;
131 if (buffer != initial_buffer) HeapFree( GetProcessHeap(), 0, buffer );
133 if( !MoveFileExW( wininitW, wininitbakW, MOVEFILE_REPLACE_EXISTING) )
135 WINE_ERR("Couldn't rename wininit.ini, error %d\n", GetLastError() );
137 return FALSE;
140 return TRUE;
143 static BOOL pendingRename(void)
145 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
146 'F','i','l','e','R','e','n','a','m','e',
147 'O','p','e','r','a','t','i','o','n','s',0};
148 static const WCHAR SessionW[] = { 'S','y','s','t','e','m','\\',
149 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
150 'C','o','n','t','r','o','l','\\',
151 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
152 WCHAR *buffer=NULL;
153 const WCHAR *src=NULL, *dst=NULL;
154 DWORD dataLength=0;
155 HKEY hSession=NULL;
156 DWORD res;
158 WINE_TRACE("Entered\n");
160 if( (res=RegOpenKeyExW( HKEY_LOCAL_MACHINE, SessionW, 0, KEY_ALL_ACCESS, &hSession ))
161 !=ERROR_SUCCESS )
163 WINE_TRACE("The key was not found - skipping\n");
164 return TRUE;
167 res=RegQueryValueExW( hSession, ValueName, NULL, NULL /* The value type does not really interest us, as it is not
168 truly a REG_MULTI_SZ anyways */,
169 NULL, &dataLength );
170 if( res==ERROR_FILE_NOT_FOUND )
172 /* No value - nothing to do. Great! */
173 WINE_TRACE("Value not present - nothing to rename\n");
174 res=TRUE;
175 goto end;
178 if( res!=ERROR_SUCCESS )
180 WINE_ERR("Couldn't query value's length (%d)\n", res );
181 res=FALSE;
182 goto end;
185 buffer=HeapAlloc( GetProcessHeap(),0,dataLength );
186 if( buffer==NULL )
188 WINE_ERR("Couldn't allocate %u bytes for the value\n", dataLength );
189 res=FALSE;
190 goto end;
193 res=RegQueryValueExW( hSession, ValueName, NULL, NULL, (LPBYTE)buffer, &dataLength );
194 if( res!=ERROR_SUCCESS )
196 WINE_ERR("Couldn't query value after successfully querying before (%u),\n"
197 "please report to wine-devel@winehq.org\n", res);
198 res=FALSE;
199 goto end;
202 /* Make sure that the data is long enough and ends with two NULLs. This
203 * simplifies the code later on.
205 if( dataLength<2*sizeof(buffer[0]) ||
206 buffer[dataLength/sizeof(buffer[0])-1]!='\0' ||
207 buffer[dataLength/sizeof(buffer[0])-2]!='\0' )
209 WINE_ERR("Improper value format - doesn't end with NULL\n");
210 res=FALSE;
211 goto end;
214 for( src=buffer; (src-buffer)*sizeof(src[0])<dataLength && *src!='\0';
215 src=dst+lstrlenW(dst)+1 )
217 DWORD dwFlags=0;
219 WINE_TRACE("processing next command\n");
221 dst=src+lstrlenW(src)+1;
223 /* We need to skip the \??\ header */
224 if( src[0]=='\\' && src[1]=='?' && src[2]=='?' && src[3]=='\\' )
225 src+=4;
227 if( dst[0]=='!' )
229 dwFlags|=MOVEFILE_REPLACE_EXISTING;
230 dst++;
233 if( dst[0]=='\\' && dst[1]=='?' && dst[2]=='?' && dst[3]=='\\' )
234 dst+=4;
236 if( *dst!='\0' )
238 /* Rename the file */
239 MoveFileExW( src, dst, dwFlags );
240 } else
242 /* Delete the file or directory */
243 if( (res=GetFileAttributesW(src))!=INVALID_FILE_ATTRIBUTES )
245 if( (res&FILE_ATTRIBUTE_DIRECTORY)==0 )
247 /* It's a file */
248 DeleteFileW(src);
249 } else
251 /* It's a directory */
252 RemoveDirectoryW(src);
254 } else
256 WINE_ERR("couldn't get file attributes (%d)\n", GetLastError() );
261 if((res=RegDeleteValueW(hSession, ValueName))!=ERROR_SUCCESS )
263 WINE_ERR("Error deleting the value (%u)\n", GetLastError() );
264 res=FALSE;
265 } else
266 res=TRUE;
268 end:
269 HeapFree(GetProcessHeap(), 0, buffer);
271 if( hSession!=NULL )
272 RegCloseKey( hSession );
274 return res;
277 enum runkeys {
278 RUNKEY_RUN, RUNKEY_RUNONCE, RUNKEY_RUNSERVICES, RUNKEY_RUNSERVICESONCE
281 const WCHAR runkeys_names[][30]=
283 {'R','u','n',0},
284 {'R','u','n','O','n','c','e',0},
285 {'R','u','n','S','e','r','v','i','c','e','s',0},
286 {'R','u','n','S','e','r','v','i','c','e','s','O','n','c','e',0}
289 #define INVALID_RUNCMD_RETURN -1
291 * This function runs the specified command in the specified dir.
292 * [in,out] cmdline - the command line to run. The function may change the passed buffer.
293 * [in] dir - the dir to run the command in. If it is NULL, then the current dir is used.
294 * [in] wait - whether to wait for the run program to finish before returning.
295 * [in] minimized - Whether to ask the program to run minimized.
297 * Returns:
298 * If running the process failed, returns INVALID_RUNCMD_RETURN. Use GetLastError to get the error code.
299 * If wait is FALSE - returns 0 if successful.
300 * If wait is TRUE - returns the program's return value.
302 static DWORD runCmd(LPWSTR cmdline, LPCWSTR dir, BOOL wait, BOOL minimized)
304 STARTUPINFOW si;
305 PROCESS_INFORMATION info;
306 DWORD exit_code=0;
308 memset(&si, 0, sizeof(si));
309 si.cb=sizeof(si);
310 if( minimized )
312 si.dwFlags=STARTF_USESHOWWINDOW;
313 si.wShowWindow=SW_MINIMIZE;
315 memset(&info, 0, sizeof(info));
317 if( !CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, dir, &si, &info) )
319 WINE_WARN("Failed to run command %s (%d)\n", wine_dbgstr_w(cmdline), GetLastError() );
320 return INVALID_RUNCMD_RETURN;
323 WINE_TRACE("Successfully ran command %s - Created process handle %p\n",
324 wine_dbgstr_w(cmdline), info.hProcess );
326 if(wait)
327 { /* wait for the process to exit */
328 WaitForSingleObject(info.hProcess, INFINITE);
329 GetExitCodeProcess(info.hProcess, &exit_code);
332 CloseHandle( info.hThread );
333 CloseHandle( info.hProcess );
335 return exit_code;
339 * Process a "Run" type registry key.
340 * hkRoot is the HKEY from which "Software\Microsoft\Windows\CurrentVersion" is
341 * opened.
342 * szKeyName is the key holding the actual entries.
343 * bDelete tells whether we should delete each value right before executing it.
344 * bSynchronous tells whether we should wait for the prog to complete before
345 * going on to the next prog.
347 static BOOL ProcessRunKeys( HKEY hkRoot, LPCWSTR szKeyName, BOOL bDelete,
348 BOOL bSynchronous )
350 static const WCHAR WINKEY_NAME[]={'S','o','f','t','w','a','r','e','\\',
351 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
352 'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
353 HKEY hkWin, hkRun;
354 DWORD res;
355 DWORD i, nMaxCmdLine=0, nMaxValue=0;
356 WCHAR *szCmdLine=NULL;
357 WCHAR *szValue=NULL;
359 if (hkRoot==HKEY_LOCAL_MACHINE)
360 WINE_TRACE("processing %s entries under HKLM\n",wine_dbgstr_w(szKeyName) );
361 else
362 WINE_TRACE("processing %s entries under HKCU\n",wine_dbgstr_w(szKeyName) );
364 if (RegOpenKeyExW( hkRoot, WINKEY_NAME, 0, KEY_READ, &hkWin ) != ERROR_SUCCESS)
365 return TRUE;
367 if (RegOpenKeyExW( hkWin, szKeyName, 0, bDelete?KEY_ALL_ACCESS:KEY_READ, &hkRun ) != ERROR_SUCCESS)
369 RegCloseKey( hkWin );
370 return TRUE;
372 RegCloseKey( hkWin );
374 if( (res=RegQueryInfoKeyW( hkRun, NULL, NULL, NULL, NULL, NULL, NULL, &i, &nMaxValue,
375 &nMaxCmdLine, NULL, NULL ))!=ERROR_SUCCESS )
376 goto end;
378 if( i==0 )
380 WINE_TRACE("No commands to execute.\n");
382 res=ERROR_SUCCESS;
383 goto end;
386 if( (szCmdLine=HeapAlloc(GetProcessHeap(),0,nMaxCmdLine))==NULL )
388 WINE_ERR("Couldn't allocate memory for the commands to be executed\n");
390 res=ERROR_NOT_ENOUGH_MEMORY;
391 goto end;
394 if( (szValue=HeapAlloc(GetProcessHeap(),0,(++nMaxValue)*sizeof(*szValue)))==NULL )
396 WINE_ERR("Couldn't allocate memory for the value names\n");
398 res=ERROR_NOT_ENOUGH_MEMORY;
399 goto end;
402 while( i>0 )
404 DWORD nValLength=nMaxValue, nDataLength=nMaxCmdLine;
405 DWORD type;
407 --i;
409 if( (res=RegEnumValueW( hkRun, i, szValue, &nValLength, 0, &type,
410 (LPBYTE)szCmdLine, &nDataLength ))!=ERROR_SUCCESS )
412 WINE_ERR("Couldn't read in value %d - %d\n", i, res );
414 continue;
417 if( bDelete && (res=RegDeleteValueW( hkRun, szValue ))!=ERROR_SUCCESS )
419 WINE_ERR("Couldn't delete value - %d, %d. Running command anyways.\n", i, res );
422 if( type!=REG_SZ )
424 WINE_ERR("Incorrect type of value #%d (%d)\n", i, type );
426 continue;
429 if( (res=runCmd(szCmdLine, NULL, bSynchronous, FALSE ))==INVALID_RUNCMD_RETURN )
431 WINE_ERR("Error running cmd #%d (%d)\n", i, GetLastError() );
434 WINE_TRACE("Done processing cmd #%d\n", i);
437 res=ERROR_SUCCESS;
439 end:
440 HeapFree( GetProcessHeap(), 0, szValue );
441 HeapFree( GetProcessHeap(), 0, szCmdLine );
443 if( hkRun!=NULL )
444 RegCloseKey( hkRun );
446 WINE_TRACE("done\n");
448 return res==ERROR_SUCCESS;
452 * WFP is Windows File Protection, in NT5 and Windows 2000 it maintains a cache
453 * of known good dlls and scans through and replaces corrupted DLLs with these
454 * known good versions. The only programs that should install into this dll
455 * cache are Windows Updates and IE (which is treated like a Windows Update)
457 * Implementing this allows installing ie in win2k mode to actually install the
458 * system dlls that we expect and need
460 static int ProcessWindowsFileProtection(void)
462 static const WCHAR winlogonW[] = {'S','o','f','t','w','a','r','e','\\',
463 'M','i','c','r','o','s','o','f','t','\\',
464 'W','i','n','d','o','w','s',' ','N','T','\\',
465 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
466 'W','i','n','l','o','g','o','n',0};
467 static const WCHAR cachedirW[] = {'S','F','C','D','l','l','C','a','c','h','e','D','i','r',0};
468 static const WCHAR dllcacheW[] = {'\\','d','l','l','c','a','c','h','e','\\','*',0};
469 static const WCHAR wildcardW[] = {'\\','*',0};
470 WIN32_FIND_DATAW finddata;
471 HANDLE find_handle;
472 BOOL find_rc;
473 DWORD rc;
474 HKEY hkey;
475 LPWSTR dllcache = NULL;
477 if (!RegOpenKeyW( HKEY_LOCAL_MACHINE, winlogonW, &hkey ))
479 DWORD sz = 0;
480 if (!RegQueryValueExW( hkey, cachedirW, 0, NULL, NULL, &sz))
482 sz += sizeof(WCHAR);
483 dllcache = HeapAlloc(GetProcessHeap(),0,sz + sizeof(wildcardW));
484 RegQueryValueExW( hkey, cachedirW, 0, NULL, (LPBYTE)dllcache, &sz);
485 strcatW( dllcache, wildcardW );
488 RegCloseKey(hkey);
490 if (!dllcache)
492 DWORD sz = GetSystemDirectoryW( NULL, 0 );
493 dllcache = HeapAlloc( GetProcessHeap(), 0, sz * sizeof(WCHAR) + sizeof(dllcacheW));
494 GetSystemDirectoryW( dllcache, sz );
495 strcatW( dllcache, dllcacheW );
498 find_handle = FindFirstFileW(dllcache,&finddata);
499 dllcache[ strlenW(dllcache) - 2] = 0; /* strip off wildcard */
500 find_rc = find_handle != INVALID_HANDLE_VALUE;
501 while (find_rc)
503 static const WCHAR dotW[] = {'.',0};
504 static const WCHAR dotdotW[] = {'.','.',0};
505 WCHAR targetpath[MAX_PATH];
506 WCHAR currentpath[MAX_PATH];
507 UINT sz;
508 UINT sz2;
509 WCHAR tempfile[MAX_PATH];
511 if (strcmpW(finddata.cFileName,dotW) == 0 || strcmpW(finddata.cFileName,dotdotW) == 0)
513 find_rc = FindNextFileW(find_handle,&finddata);
514 continue;
517 sz = MAX_PATH;
518 sz2 = MAX_PATH;
519 VerFindFileW(VFFF_ISSHAREDFILE, finddata.cFileName, windowsdir,
520 windowsdir, currentpath, &sz, targetpath, &sz2);
521 sz = MAX_PATH;
522 rc = VerInstallFileW(0, finddata.cFileName, finddata.cFileName,
523 dllcache, targetpath, currentpath, tempfile, &sz);
524 if (rc != ERROR_SUCCESS)
526 WINE_WARN("WFP: %s error 0x%x\n",wine_dbgstr_w(finddata.cFileName),rc);
527 DeleteFileW(tempfile);
530 /* now delete the source file so that we don't try to install it over and over again */
531 lstrcpynW( targetpath, dllcache, MAX_PATH - 1 );
532 sz = strlenW( targetpath );
533 targetpath[sz++] = '\\';
534 lstrcpynW( targetpath + sz, finddata.cFileName, MAX_PATH - sz );
535 if (!DeleteFileW( targetpath ))
536 WINE_WARN( "failed to delete %s: error %u\n", wine_dbgstr_w(targetpath), GetLastError() );
538 find_rc = FindNextFileW(find_handle,&finddata);
540 FindClose(find_handle);
541 HeapFree(GetProcessHeap(),0,dllcache);
542 return 1;
545 /* start services */
546 static void start_services(void)
548 static const WCHAR servicesW[] = {'S','y','s','t','e','m','\\',
549 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
550 'S','e','r','v','i','c','e','s',0};
551 static const WCHAR startW[] = {'S','t','a','r','t',0};
552 HKEY hkey, skey;
553 DWORD type, size, start, index = 0;
554 WCHAR name[MAX_PATH];
555 SC_HANDLE manager;
557 if (RegOpenKeyW( HKEY_LOCAL_MACHINE, servicesW, &hkey )) return;
559 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ALL_ACCESS )))
561 RegCloseKey( hkey );
562 return;
565 while (!RegEnumKeyW( hkey, index++, name, sizeof(name)/sizeof(name[0]) ))
567 if (RegOpenKeyW( hkey, name, &skey )) continue;
568 size = sizeof(start);
569 if (!RegQueryValueExW( skey, startW, NULL, &type, (LPBYTE)&start, &size ) && type == REG_DWORD)
571 if (start == SERVICE_BOOT_START ||
572 start == SERVICE_SYSTEM_START ||
573 start == SERVICE_AUTO_START)
575 SC_HANDLE handle = OpenServiceW( manager, name, SERVICE_ALL_ACCESS );
576 if (handle)
578 WINE_TRACE( "starting service %s start %u\n", wine_dbgstr_w(name), start );
579 StartServiceW( handle, 0, NULL );
580 CloseServiceHandle( handle );
584 RegCloseKey( skey );
586 CloseServiceHandle( manager );
587 RegCloseKey( hkey );
590 /* Process items in the StartUp group of the user's Programs under the Start Menu. Some installers put
591 * shell links here to restart themselves after boot. */
592 static BOOL ProcessStartupItems(void)
594 BOOL ret = FALSE;
595 HRESULT hr;
596 int iRet;
597 IMalloc *ppM = NULL;
598 IShellFolder *psfDesktop = NULL, *psfStartup = NULL;
599 LPITEMIDLIST pidlStartup = NULL, pidlItem;
600 ULONG NumPIDLs;
601 IEnumIDList *iEnumList = NULL;
602 STRRET strret;
603 WCHAR wszCommand[MAX_PATH];
605 WINE_TRACE("Processing items in the StartUp folder.\n");
607 hr = SHGetMalloc(&ppM);
608 if (FAILED(hr))
610 WINE_ERR("Couldn't get IMalloc object.\n");
611 goto done;
614 hr = SHGetDesktopFolder(&psfDesktop);
615 if (FAILED(hr))
617 WINE_ERR("Couldn't get desktop folder.\n");
618 goto done;
621 hr = SHGetSpecialFolderLocation(NULL, CSIDL_STARTUP, &pidlStartup);
622 if (FAILED(hr))
624 WINE_TRACE("Couldn't get StartUp folder location.\n");
625 goto done;
628 hr = IShellFolder_BindToObject(psfDesktop, pidlStartup, NULL, &IID_IShellFolder, (LPVOID*)&psfStartup);
629 if (FAILED(hr))
631 WINE_TRACE("Couldn't bind IShellFolder to StartUp folder.\n");
632 goto done;
635 hr = IShellFolder_EnumObjects(psfStartup, NULL, SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
636 if (FAILED(hr))
638 WINE_TRACE("Unable to enumerate StartUp objects.\n");
639 goto done;
642 while (IEnumIDList_Next(iEnumList, 1, &pidlItem, &NumPIDLs) == S_OK &&
643 (NumPIDLs) == 1)
645 hr = IShellFolder_GetDisplayNameOf(psfStartup, pidlItem, SHGDN_FORPARSING, &strret);
646 if (FAILED(hr))
647 WINE_TRACE("Unable to get display name of enumeration item.\n");
648 else
650 hr = StrRetToBufW(&strret, pidlItem, wszCommand, MAX_PATH);
651 if (FAILED(hr))
652 WINE_TRACE("Unable to parse display name.\n");
653 else
654 if ((iRet = (int)ShellExecuteW(NULL, NULL, wszCommand, NULL, NULL, SW_SHOWNORMAL)) <= 32)
655 WINE_WARN("Error %d executing command %s.\n", iRet, wine_dbgstr_w(wszCommand));
658 IMalloc_Free(ppM, pidlItem);
661 /* Return success */
662 ret = TRUE;
664 done:
665 if (iEnumList) IEnumIDList_Release(iEnumList);
666 if (psfStartup) IShellFolder_Release(psfStartup);
667 if (pidlStartup) IMalloc_Free(ppM, pidlStartup);
669 return ret;
672 static void usage(void)
674 WINE_MESSAGE( "Usage: wineboot [options]\n" );
675 WINE_MESSAGE( "Options;\n" );
676 WINE_MESSAGE( " -h,--help Display this help message\n" );
677 WINE_MESSAGE( " -e,--end-session End the current session cleanly\n" );
678 WINE_MESSAGE( " -f,--force Force exit for processes that don't exit cleanly\n" );
679 WINE_MESSAGE( " -i,--init Perform initialization for first Wine instance\n" );
680 WINE_MESSAGE( " -k,--kill Kill running processes without any cleanup\n" );
681 WINE_MESSAGE( " -r,--restart Restart only, don't do normal startup operations\n" );
682 WINE_MESSAGE( " -s,--shutdown Shutdown only, don't reboot\n" );
685 static const char short_options[] = "efhikrs";
687 static const struct option long_options[] =
689 { "help", 0, 0, 'h' },
690 { "end-session", 0, 0, 'e' },
691 { "force", 0, 0, 'f' },
692 { "init" , 0, 0, 'i' },
693 { "kill", 0, 0, 'k' },
694 { "restart", 0, 0, 'r' },
695 { "shutdown", 0, 0, 's' },
696 { NULL, 0, 0, 0 }
699 int main( int argc, char *argv[] )
701 extern HANDLE __wine_make_process_system(void);
702 static const WCHAR wineboot_eventW[] = {'_','_','w','i','n','e','b','o','o','t','_','e','v','e','n','t',0};
704 /* First, set the current directory to SystemRoot */
705 int optc;
706 int end_session = 0, force = 0, init = 0, kill = 0, restart = 0, shutdown = 0;
707 HANDLE event;
709 GetWindowsDirectoryW( windowsdir, MAX_PATH );
710 if( !SetCurrentDirectoryW( windowsdir ) )
712 WINE_ERR("Cannot set the dir to %s (%d)\n", wine_dbgstr_w(windowsdir), GetLastError() );
713 return 100;
716 while ((optc = getopt_long(argc, argv, short_options, long_options, NULL )) != -1)
718 switch(optc)
720 case 'e': end_session = kill = 1; break;
721 case 'f': force = 1; break;
722 case 'i': init = 1; break;
723 case 'k': kill = 1; break;
724 case 'r': restart = 1; break;
725 case 's': shutdown = 1; break;
726 case 'h': usage(); return 0;
727 case '?': usage(); return 1;
731 if (end_session)
733 if (!shutdown_close_windows( force )) return 1;
736 if (kill) kill_processes( shutdown );
738 if (shutdown) return 0;
740 event = CreateEventW( NULL, TRUE, FALSE, wineboot_eventW );
741 ResetEvent( event ); /* in case this is a restart */
743 wininit();
744 pendingRename();
746 ProcessWindowsFileProtection();
747 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICESONCE], TRUE, FALSE );
748 if (init || (kill && !restart))
750 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNSERVICES], FALSE, FALSE );
751 start_services();
753 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUNONCE], TRUE, TRUE );
755 if (!init && !restart)
757 ProcessRunKeys( HKEY_LOCAL_MACHINE, runkeys_names[RUNKEY_RUN], FALSE, FALSE );
758 ProcessRunKeys( HKEY_CURRENT_USER, runkeys_names[RUNKEY_RUN], FALSE, FALSE );
759 ProcessStartupItems();
762 WINE_TRACE("Operation done\n");
764 SetEvent( event );
766 /* FIXME: the wait is needed to keep services running */
767 /* it should be removed once we have a proper services.exe */
768 if (!restart) WaitForSingleObject( __wine_make_process_system(), INFINITE );
769 return 0;