kernel32: Add a stub implementation for CmdBatNotification.
[wine/testsucceed.git] / dlls / kernel / process.c
blob62b66690b11e55c75c27b464432850c36cba213f
1 /*
2 * Win32 processes
4 * Copyright 1996, 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <time.h>
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
33 #endif
34 #include <sys/types.h>
36 #include "ntstatus.h"
37 #define WIN32_NO_STATUS
38 #include "wine/winbase16.h"
39 #include "wine/winuser16.h"
40 #include "winioctl.h"
41 #include "winternl.h"
42 #include "kernel_private.h"
43 #include "wine/exception.h"
44 #include "wine/server.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 #ifdef HAVE_VALGRIND_MEMCHECK_H
49 #include <valgrind/memcheck.h>
50 #endif
52 WINE_DEFAULT_DEBUG_CHANNEL(process);
53 WINE_DECLARE_DEBUG_CHANNEL(file);
54 WINE_DECLARE_DEBUG_CHANNEL(relay);
56 typedef struct
58 LPSTR lpEnvAddress;
59 LPSTR lpCmdLine;
60 LPSTR lpCmdShow;
61 DWORD dwReserved;
62 } LOADPARMS32;
64 static UINT process_error_mode;
66 static DWORD shutdown_flags = 0;
67 static DWORD shutdown_priority = 0x280;
68 static DWORD process_dword;
70 HMODULE kernel32_handle = 0;
72 const WCHAR *DIR_Windows = NULL;
73 const WCHAR *DIR_System = NULL;
75 /* Process flags */
76 #define PDB32_DEBUGGED 0x0001 /* Process is being debugged */
77 #define PDB32_WIN16_PROC 0x0008 /* Win16 process */
78 #define PDB32_DOS_PROC 0x0010 /* Dos process */
79 #define PDB32_CONSOLE_PROC 0x0020 /* Console process */
80 #define PDB32_FILE_APIS_OEM 0x0040 /* File APIs are OEM */
81 #define PDB32_WIN32S_PROC 0x8000 /* Win32s process */
83 static const WCHAR comW[] = {'.','c','o','m',0};
84 static const WCHAR batW[] = {'.','b','a','t',0};
85 static const WCHAR pifW[] = {'.','p','i','f',0};
86 static const WCHAR winevdmW[] = {'w','i','n','e','v','d','m','.','e','x','e',0};
88 extern void SHELL_LoadRegistry(void);
91 /***********************************************************************
92 * contains_path
94 inline static int contains_path( LPCWSTR name )
96 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
100 /***********************************************************************
101 * is_special_env_var
103 * Check if an environment variable needs to be handled specially when
104 * passed through the Unix environment (i.e. prefixed with "WINE").
106 inline static int is_special_env_var( const char *var )
108 return (!strncmp( var, "PATH=", sizeof("PATH=")-1 ) ||
109 !strncmp( var, "HOME=", sizeof("HOME=")-1 ) ||
110 !strncmp( var, "TEMP=", sizeof("TEMP=")-1 ) ||
111 !strncmp( var, "TMP=", sizeof("TMP=")-1 ));
115 /***************************************************************************
116 * get_builtin_path
118 * Get the path of a builtin module when the native file does not exist.
120 static BOOL get_builtin_path( const WCHAR *libname, const WCHAR *ext, WCHAR *filename, UINT size )
122 WCHAR *file_part;
123 UINT len = strlenW( DIR_System );
125 if (contains_path( libname ))
127 if (RtlGetFullPathName_U( libname, size * sizeof(WCHAR),
128 filename, &file_part ) > size * sizeof(WCHAR))
129 return FALSE; /* too long */
131 if (strncmpiW( filename, DIR_System, len ) || filename[len] != '\\')
132 return FALSE;
133 while (filename[len] == '\\') len++;
134 if (filename + len != file_part) return FALSE;
136 else
138 if (strlenW(libname) + len + 2 >= size) return FALSE; /* too long */
139 memcpy( filename, DIR_System, len * sizeof(WCHAR) );
140 file_part = filename + len;
141 if (file_part > filename && file_part[-1] != '\\') *file_part++ = '\\';
142 strcpyW( file_part, libname );
144 if (ext && !strchrW( file_part, '.' ))
146 if (file_part + strlenW(file_part) + strlenW(ext) + 1 > filename + size)
147 return FALSE; /* too long */
148 strcatW( file_part, ext );
150 return TRUE;
154 /***********************************************************************
155 * open_builtin_exe_file
157 * Open an exe file for a builtin exe.
159 static void *open_builtin_exe_file( const WCHAR *name, char *error, int error_size,
160 int test_only, int *file_exists )
162 char exename[MAX_PATH];
163 WCHAR *p;
164 UINT i, len;
166 *file_exists = 0;
167 if ((p = strrchrW( name, '/' ))) name = p + 1;
168 if ((p = strrchrW( name, '\\' ))) name = p + 1;
170 /* we don't want to depend on the current codepage here */
171 len = strlenW( name ) + 1;
172 if (len >= sizeof(exename)) return NULL;
173 for (i = 0; i < len; i++)
175 if (name[i] > 127) return NULL;
176 exename[i] = (char)name[i];
177 if (exename[i] >= 'A' && exename[i] <= 'Z') exename[i] += 'a' - 'A';
179 return wine_dll_load_main_exe( exename, error, error_size, test_only, file_exists );
183 /***********************************************************************
184 * open_exe_file
186 * Open a specific exe file, taking load order into account.
187 * Returns the file handle or 0 for a builtin exe.
189 static HANDLE open_exe_file( const WCHAR *name )
191 HANDLE handle;
193 TRACE("looking for %s\n", debugstr_w(name) );
195 if ((handle = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE,
196 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
198 WCHAR buffer[MAX_PATH];
199 /* file doesn't exist, check for builtin */
200 if (!contains_path( name )) goto error;
201 if (!get_builtin_path( name, NULL, buffer, sizeof(buffer) )) goto error;
202 handle = 0;
204 return handle;
206 error:
207 SetLastError( ERROR_FILE_NOT_FOUND );
208 return INVALID_HANDLE_VALUE;
212 /***********************************************************************
213 * find_exe_file
215 * Open an exe file, and return the full name and file handle.
216 * Returns FALSE if file could not be found.
217 * If file exists but cannot be opened, returns TRUE and set handle to INVALID_HANDLE_VALUE.
218 * If file is a builtin exe, returns TRUE and sets handle to 0.
220 static BOOL find_exe_file( const WCHAR *name, WCHAR *buffer, int buflen, HANDLE *handle )
222 static const WCHAR exeW[] = {'.','e','x','e',0};
223 int file_exists;
225 TRACE("looking for %s\n", debugstr_w(name) );
227 if (!SearchPathW( NULL, name, exeW, buflen, buffer, NULL ) &&
228 !get_builtin_path( name, exeW, buffer, buflen ))
230 /* no builtin found, try native without extension in case it is a Unix app */
232 if (SearchPathW( NULL, name, NULL, buflen, buffer, NULL ))
234 TRACE( "Trying native/Unix binary %s\n", debugstr_w(buffer) );
235 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE,
236 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
237 return TRUE;
239 return FALSE;
242 TRACE( "Trying native exe %s\n", debugstr_w(buffer) );
243 if ((*handle = CreateFileW( buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_DELETE,
244 NULL, OPEN_EXISTING, 0, 0 )) != INVALID_HANDLE_VALUE)
245 return TRUE;
247 TRACE( "Trying built-in exe %s\n", debugstr_w(buffer) );
248 open_builtin_exe_file( buffer, NULL, 0, 1, &file_exists );
249 if (file_exists)
251 *handle = 0;
252 return TRUE;
255 return FALSE;
259 /***********************************************************************
260 * build_initial_environment
262 * Build the Win32 environment from the Unix environment
264 static BOOL build_initial_environment( char **environ )
266 SIZE_T size = 1;
267 char **e;
268 WCHAR *p, *endptr;
269 void *ptr;
271 /* Compute the total size of the Unix environment */
272 for (e = environ; *e; e++)
274 if (is_special_env_var( *e )) continue;
275 size += MultiByteToWideChar( CP_UNIXCP, 0, *e, -1, NULL, 0 );
277 size *= sizeof(WCHAR);
279 /* Now allocate the environment */
280 ptr = NULL;
281 if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
282 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
283 return FALSE;
285 NtCurrentTeb()->Peb->ProcessParameters->Environment = p = ptr;
286 endptr = p + size / sizeof(WCHAR);
288 /* And fill it with the Unix environment */
289 for (e = environ; *e; e++)
291 char *str = *e;
293 /* skip Unix special variables and use the Wine variants instead */
294 if (!strncmp( str, "WINE", 4 ))
296 if (is_special_env_var( str + 4 )) str += 4;
297 else if (!strncmp( str, "WINEPRELOADRESERVE=", 19 )) continue; /* skip it */
299 else if (is_special_env_var( str )) continue; /* skip it */
301 MultiByteToWideChar( CP_UNIXCP, 0, str, -1, p, endptr - p );
302 p += strlenW(p) + 1;
304 *p = 0;
305 return TRUE;
309 /***********************************************************************
310 * set_registry_variables
312 * Set environment variables by enumerating the values of a key;
313 * helper for set_registry_environment().
314 * Note that Windows happily truncates the value if it's too big.
316 static void set_registry_variables( HANDLE hkey, ULONG type )
318 UNICODE_STRING env_name, env_value;
319 NTSTATUS status;
320 DWORD size;
321 int index;
322 char buffer[1024*sizeof(WCHAR) + sizeof(KEY_VALUE_FULL_INFORMATION)];
323 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
325 for (index = 0; ; index++)
327 status = NtEnumerateValueKey( hkey, index, KeyValueFullInformation,
328 buffer, sizeof(buffer), &size );
329 if (status != STATUS_SUCCESS && status != STATUS_BUFFER_OVERFLOW)
330 break;
331 if (info->Type != type)
332 continue;
333 env_name.Buffer = info->Name;
334 env_name.Length = env_name.MaximumLength = info->NameLength;
335 env_value.Buffer = (WCHAR *)(buffer + info->DataOffset);
336 env_value.Length = env_value.MaximumLength = info->DataLength;
337 if (env_value.Length && !env_value.Buffer[env_value.Length/sizeof(WCHAR)-1])
338 env_value.Length--; /* don't count terminating null if any */
339 if (info->Type == REG_EXPAND_SZ)
341 WCHAR buf_expanded[1024];
342 UNICODE_STRING env_expanded;
343 env_expanded.Length = env_expanded.MaximumLength = sizeof(buf_expanded);
344 env_expanded.Buffer=buf_expanded;
345 status = RtlExpandEnvironmentStrings_U(NULL, &env_value, &env_expanded, NULL);
346 if (status == STATUS_SUCCESS || status == STATUS_BUFFER_OVERFLOW)
347 RtlSetEnvironmentVariable( NULL, &env_name, &env_expanded );
349 else
351 RtlSetEnvironmentVariable( NULL, &env_name, &env_value );
357 /***********************************************************************
358 * set_registry_environment
360 * Set the environment variables specified in the registry.
362 * Note: Windows handles REG_SZ and REG_EXPAND_SZ in one pass with the
363 * consequence that REG_EXPAND_SZ cannot be used reliably as it depends
364 * on the order in which the variables are processed. But on Windows it
365 * does not really matter since they only use %SystemDrive% and
366 * %SystemRoot% which are predefined. But Wine defines these in the
367 * registry, so we need two passes.
369 static void set_registry_environment(void)
371 static const WCHAR env_keyW[] = {'M','a','c','h','i','n','e','\\',
372 'S','y','s','t','e','m','\\',
373 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
374 'C','o','n','t','r','o','l','\\',
375 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
376 'E','n','v','i','r','o','n','m','e','n','t',0};
377 static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
379 OBJECT_ATTRIBUTES attr;
380 UNICODE_STRING nameW;
381 HANDLE hkey;
383 attr.Length = sizeof(attr);
384 attr.RootDirectory = 0;
385 attr.ObjectName = &nameW;
386 attr.Attributes = 0;
387 attr.SecurityDescriptor = NULL;
388 attr.SecurityQualityOfService = NULL;
390 /* first the system environment variables */
391 RtlInitUnicodeString( &nameW, env_keyW );
392 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) == STATUS_SUCCESS)
394 set_registry_variables( hkey, REG_SZ );
395 set_registry_variables( hkey, REG_EXPAND_SZ );
396 NtClose( hkey );
399 /* then the ones for the current user */
400 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &attr.RootDirectory ) != STATUS_SUCCESS) return;
401 RtlInitUnicodeString( &nameW, envW );
402 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) == STATUS_SUCCESS)
404 set_registry_variables( hkey, REG_SZ );
405 set_registry_variables( hkey, REG_EXPAND_SZ );
406 NtClose( hkey );
408 NtClose( attr.RootDirectory );
412 /***********************************************************************
413 * set_library_wargv
415 * Set the Wine library Unicode argv global variables.
417 static void set_library_wargv( char **argv )
419 int argc;
420 char *q;
421 WCHAR *p;
422 WCHAR **wargv;
423 DWORD total = 0;
425 for (argc = 0; argv[argc]; argc++)
426 total += MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, NULL, 0 );
428 wargv = RtlAllocateHeap( GetProcessHeap(), 0,
429 total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
430 p = (WCHAR *)(wargv + argc + 1);
431 for (argc = 0; argv[argc]; argc++)
433 DWORD reslen = MultiByteToWideChar( CP_UNIXCP, 0, argv[argc], -1, p, total );
434 wargv[argc] = p;
435 p += reslen;
436 total -= reslen;
438 wargv[argc] = NULL;
440 /* convert argv back from Unicode since it has to be in the Ansi codepage not the Unix one */
442 for (argc = 0; wargv[argc]; argc++)
443 total += WideCharToMultiByte( CP_ACP, 0, wargv[argc], -1, NULL, 0, NULL, NULL );
445 argv = RtlAllocateHeap( GetProcessHeap(), 0, total + (argc + 1) * sizeof(*argv) );
446 q = (char *)(argv + argc + 1);
447 for (argc = 0; wargv[argc]; argc++)
449 DWORD reslen = WideCharToMultiByte( CP_ACP, 0, wargv[argc], -1, q, total, NULL, NULL );
450 argv[argc] = q;
451 q += reslen;
452 total -= reslen;
454 argv[argc] = NULL;
456 __wine_main_argv = argv;
457 __wine_main_wargv = wargv;
461 /***********************************************************************
462 * build_command_line
464 * Build the command line of a process from the argv array.
466 * Note that it does NOT necessarily include the file name.
467 * Sometimes we don't even have any command line options at all.
469 * We must quote and escape characters so that the argv array can be rebuilt
470 * from the command line:
471 * - spaces and tabs must be quoted
472 * 'a b' -> '"a b"'
473 * - quotes must be escaped
474 * '"' -> '\"'
475 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
476 * resulting in an odd number of '\' followed by a '"'
477 * '\"' -> '\\\"'
478 * '\\"' -> '\\\\\"'
479 * - '\'s that are not followed by a '"' can be left as is
480 * 'a\b' == 'a\b'
481 * 'a\\b' == 'a\\b'
483 static BOOL build_command_line( WCHAR **argv )
485 int len;
486 WCHAR **arg;
487 LPWSTR p;
488 RTL_USER_PROCESS_PARAMETERS* rupp = NtCurrentTeb()->Peb->ProcessParameters;
490 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
492 len = 0;
493 for (arg = argv; *arg; arg++)
495 int has_space,bcount;
496 WCHAR* a;
498 has_space=0;
499 bcount=0;
500 a=*arg;
501 if( !*a ) has_space=1;
502 while (*a!='\0') {
503 if (*a=='\\') {
504 bcount++;
505 } else {
506 if (*a==' ' || *a=='\t') {
507 has_space=1;
508 } else if (*a=='"') {
509 /* doubling of '\' preceding a '"',
510 * plus escaping of said '"'
512 len+=2*bcount+1;
514 bcount=0;
516 a++;
518 len+=(a-*arg)+1 /* for the separating space */;
519 if (has_space)
520 len+=2; /* for the quotes */
523 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR))))
524 return FALSE;
526 p = rupp->CommandLine.Buffer;
527 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
528 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
529 for (arg = argv; *arg; arg++)
531 int has_space,has_quote;
532 WCHAR* a;
534 /* Check for quotes and spaces in this argument */
535 has_space=has_quote=0;
536 a=*arg;
537 if( !*a ) has_space=1;
538 while (*a!='\0') {
539 if (*a==' ' || *a=='\t') {
540 has_space=1;
541 if (has_quote)
542 break;
543 } else if (*a=='"') {
544 has_quote=1;
545 if (has_space)
546 break;
548 a++;
551 /* Now transfer it to the command line */
552 if (has_space)
553 *p++='"';
554 if (has_quote) {
555 int bcount;
556 WCHAR* a;
558 bcount=0;
559 a=*arg;
560 while (*a!='\0') {
561 if (*a=='\\') {
562 *p++=*a;
563 bcount++;
564 } else {
565 if (*a=='"') {
566 int i;
568 /* Double all the '\\' preceding this '"', plus one */
569 for (i=0;i<=bcount;i++)
570 *p++='\\';
571 *p++='"';
572 } else {
573 *p++=*a;
575 bcount=0;
577 a++;
579 } else {
580 WCHAR* x = *arg;
581 while ((*p=*x++)) p++;
583 if (has_space)
584 *p++='"';
585 *p++=' ';
587 if (p > rupp->CommandLine.Buffer)
588 p--; /* remove last space */
589 *p = '\0';
591 return TRUE;
595 static void version(void)
597 MESSAGE( "%s\n", PACKAGE_STRING );
598 ExitProcess(0);
601 static void usage(void)
603 MESSAGE( "%s\n", PACKAGE_STRING );
604 MESSAGE( "Usage: wine PROGRAM [ARGUMENTS...] Run the specified program\n" );
605 MESSAGE( " wine --help Display this help and exit\n");
606 MESSAGE( " wine --version Output version information and exit\n");
607 ExitProcess(0);
611 /***********************************************************************
612 * init_current_directory
614 * Initialize the current directory from the Unix cwd or the parent info.
616 static void init_current_directory( CURDIR *cur_dir )
618 UNICODE_STRING dir_str;
619 char *cwd;
620 int size;
622 /* if we received a cur dir from the parent, try this first */
624 if (cur_dir->DosPath.Length)
626 if (RtlSetCurrentDirectory_U( &cur_dir->DosPath ) == STATUS_SUCCESS) goto done;
629 /* now try to get it from the Unix cwd */
631 for (size = 256; ; size *= 2)
633 if (!(cwd = HeapAlloc( GetProcessHeap(), 0, size ))) break;
634 if (getcwd( cwd, size )) break;
635 HeapFree( GetProcessHeap(), 0, cwd );
636 if (errno == ERANGE) continue;
637 cwd = NULL;
638 break;
641 if (cwd)
643 WCHAR *dirW;
644 int lenW = MultiByteToWideChar( CP_UNIXCP, 0, cwd, -1, NULL, 0 );
645 if ((dirW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
647 MultiByteToWideChar( CP_UNIXCP, 0, cwd, -1, dirW, lenW );
648 RtlInitUnicodeString( &dir_str, dirW );
649 RtlSetCurrentDirectory_U( &dir_str );
650 RtlFreeUnicodeString( &dir_str );
654 if (!cur_dir->DosPath.Length) /* still not initialized */
656 MESSAGE("Warning: could not find DOS drive for current working directory '%s', "
657 "starting in the Windows directory.\n", cwd ? cwd : "" );
658 RtlInitUnicodeString( &dir_str, DIR_Windows );
659 RtlSetCurrentDirectory_U( &dir_str );
661 HeapFree( GetProcessHeap(), 0, cwd );
663 done:
664 if (!cur_dir->Handle) chdir("/"); /* change to root directory so as not to lock cdroms */
665 TRACE( "starting in %s %p\n", debugstr_w( cur_dir->DosPath.Buffer ), cur_dir->Handle );
669 /***********************************************************************
670 * init_windows_dirs
672 * Initialize the windows and system directories from the environment.
674 static void init_windows_dirs(void)
676 extern void __wine_init_windows_dir( const WCHAR *windir, const WCHAR *sysdir );
678 static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
679 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
680 static const WCHAR default_windirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
681 static const WCHAR default_sysdirW[] = {'\\','s','y','s','t','e','m','3','2',0};
683 DWORD len;
684 WCHAR *buffer;
686 if ((len = GetEnvironmentVariableW( windirW, NULL, 0 )))
688 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
689 GetEnvironmentVariableW( windirW, buffer, len );
690 DIR_Windows = buffer;
692 else DIR_Windows = default_windirW;
694 if ((len = GetEnvironmentVariableW( winsysdirW, NULL, 0 )))
696 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
697 GetEnvironmentVariableW( winsysdirW, buffer, len );
698 DIR_System = buffer;
700 else
702 len = strlenW( DIR_Windows );
703 buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) + sizeof(default_sysdirW) );
704 memcpy( buffer, DIR_Windows, len * sizeof(WCHAR) );
705 memcpy( buffer + len, default_sysdirW, sizeof(default_sysdirW) );
706 DIR_System = buffer;
709 if (GetFileAttributesW( DIR_Windows ) == INVALID_FILE_ATTRIBUTES)
710 MESSAGE( "Warning: the specified Windows directory %s is not accessible.\n",
711 debugstr_w(DIR_Windows) );
712 if (GetFileAttributesW( DIR_System ) == INVALID_FILE_ATTRIBUTES)
713 MESSAGE( "Warning: the specified System directory %s is not accessible.\n",
714 debugstr_w(DIR_System) );
716 TRACE_(file)( "WindowsDir = %s\n", debugstr_w(DIR_Windows) );
717 TRACE_(file)( "SystemDir = %s\n", debugstr_w(DIR_System) );
719 /* set the directories in ntdll too */
720 __wine_init_windows_dir( DIR_Windows, DIR_System );
724 /***********************************************************************
725 * process_init
727 * Main process initialisation code
729 static BOOL process_init(void)
731 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2',0};
732 PEB *peb = NtCurrentTeb()->Peb;
733 RTL_USER_PROCESS_PARAMETERS *params = peb->ProcessParameters;
735 PTHREAD_Init();
737 setbuf(stdout,NULL);
738 setbuf(stderr,NULL);
739 setlocale(LC_CTYPE,"");
741 kernel32_handle = GetModuleHandleW(kernel32W);
743 LOCALE_Init();
745 if (!params->Environment)
747 /* Copy the parent environment */
748 if (!build_initial_environment( __wine_main_environ )) return FALSE;
750 /* convert old configuration to new format */
751 convert_old_config();
753 set_registry_environment();
756 init_windows_dirs();
757 init_current_directory( &params->CurrentDirectory );
759 /* convert value from server:
760 * + 0 => INVALID_HANDLE_VALUE
761 * + console handle needs to be mapped
763 if (!params->hStdInput)
764 params->hStdInput = INVALID_HANDLE_VALUE;
765 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdInput)))
766 params->hStdInput = console_handle_map(params->hStdInput);
768 if (!params->hStdOutput)
769 params->hStdOutput = INVALID_HANDLE_VALUE;
770 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdOutput)))
771 params->hStdOutput = console_handle_map(params->hStdOutput);
773 if (!params->hStdError)
774 params->hStdError = INVALID_HANDLE_VALUE;
775 else if (VerifyConsoleIoHandle(console_handle_map(params->hStdError)))
776 params->hStdError = console_handle_map(params->hStdError);
778 return TRUE;
782 /***********************************************************************
783 * init_stack
785 * Allocate the stack of new process.
787 static void *init_stack(void)
789 void *base;
790 SIZE_T stack_size, page_size = getpagesize();
791 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
793 stack_size = max( nt->OptionalHeader.SizeOfStackReserve, nt->OptionalHeader.SizeOfStackCommit );
794 stack_size += page_size; /* for the guard page */
795 stack_size = (stack_size + 0xffff) & ~0xffff; /* round to 64K boundary */
796 if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */
798 if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_READWRITE )))
800 ERR( "failed to allocate main process stack\n" );
801 ExitProcess( 1 );
804 /* note: limit is lower than base since the stack grows down */
805 NtCurrentTeb()->DeallocationStack = base;
806 NtCurrentTeb()->Tib.StackBase = (char *)base + stack_size;
807 NtCurrentTeb()->Tib.StackLimit = (char *)base + page_size;
809 #ifdef VALGRIND_STACK_REGISTER
810 /* no need to de-register the stack as it's the one of the main thread */
811 VALGRIND_STACK_REGISTER(NtCurrentTeb()->Tib.StackLimit, NtCurrentTeb()->Tib.StackBase);
812 #endif
814 /* setup guard page */
815 VirtualProtect( base, page_size, PAGE_NOACCESS, NULL );
816 return NtCurrentTeb()->Tib.StackBase;
820 /***********************************************************************
821 * start_process
823 * Startup routine of a new process. Runs on the new process stack.
825 static void start_process( void *arg )
827 __TRY
829 PEB *peb = NtCurrentTeb()->Peb;
830 IMAGE_NT_HEADERS *nt;
831 LPTHREAD_START_ROUTINE entry;
833 LdrInitializeThunk( 0, 0, 0, 0 );
835 nt = RtlImageNtHeader( peb->ImageBaseAddress );
836 entry = (LPTHREAD_START_ROUTINE)((char *)peb->ImageBaseAddress +
837 nt->OptionalHeader.AddressOfEntryPoint);
839 if (TRACE_ON(relay))
840 DPRINTF( "%04lx:Starting process %s (entryproc=%p)\n", GetCurrentThreadId(),
841 debugstr_w(peb->ProcessParameters->ImagePathName.Buffer), entry );
843 SetLastError( 0 ); /* clear error code */
844 if (peb->BeingDebugged) DbgBreakPoint();
845 ExitProcess( entry( peb ) );
847 __EXCEPT(UnhandledExceptionFilter)
849 TerminateThread( GetCurrentThread(), GetExceptionCode() );
851 __ENDTRY
855 /***********************************************************************
856 * __wine_kernel_init
858 * Wine initialisation: load and start the main exe file.
860 void __wine_kernel_init(void)
862 static const WCHAR dotW[] = {'.',0};
863 static const WCHAR exeW[] = {'.','e','x','e',0};
865 WCHAR *p, main_exe_name[MAX_PATH];
866 HMODULE module;
867 DWORD type, error = 0;
868 PEB *peb = NtCurrentTeb()->Peb;
870 /* Initialize everything */
871 if (!process_init()) exit(1);
873 __wine_main_argv++; /* remove argv[0] (wine itself) */
874 __wine_main_argc--;
876 if (peb->ProcessParameters->ImagePathName.Buffer)
878 strcpyW( main_exe_name, peb->ProcessParameters->ImagePathName.Buffer );
880 else
882 WCHAR exe_nameW[MAX_PATH];
884 if (!__wine_main_argv[0]) usage();
885 if (__wine_main_argc == 1)
887 if (strcmp(__wine_main_argv[0], "--help") == 0) usage();
888 if (strcmp(__wine_main_argv[0], "--version") == 0) version();
891 MultiByteToWideChar( CP_UNIXCP, 0, __wine_main_argv[0], -1, exe_nameW, MAX_PATH );
892 if (!SearchPathW( NULL, exe_nameW, exeW, MAX_PATH, main_exe_name, NULL ) &&
893 !get_builtin_path( exe_nameW, exeW, main_exe_name, MAX_PATH ))
895 MESSAGE( "wine: cannot find '%s'\n", __wine_main_argv[0] );
896 ExitProcess( GetLastError() );
900 /* if there's no extension, append a dot to prevent LoadLibrary from appending .dll */
901 p = strrchrW( main_exe_name, '.' );
902 if (!p || strchrW( p, '/' ) || strchrW( p, '\\' )) strcatW( main_exe_name, dotW );
904 TRACE( "starting process name=%s argv[0]=%s\n",
905 debugstr_w(main_exe_name), debugstr_a(__wine_main_argv[0]) );
907 RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->DllPath,
908 MODULE_get_dll_load_path(main_exe_name) );
910 if (!(module = LoadLibraryExW( main_exe_name, 0, DONT_RESOLVE_DLL_REFERENCES )))
912 error = GetLastError();
913 /* check for a DOS binary and start winevdm if needed */
914 if (error == ERROR_BAD_EXE_FORMAT && GetBinaryTypeW( main_exe_name, &type ))
916 if (type == SCS_WOW_BINARY || type == SCS_DOS_BINARY ||
917 type == SCS_OS216_BINARY || type == SCS_PIF_BINARY)
919 __wine_main_argv--;
920 __wine_main_argc++;
921 __wine_main_argv[0] = "winevdm.exe";
922 module = LoadLibraryExW( winevdmW, 0, DONT_RESOLVE_DLL_REFERENCES );
927 if (!module)
929 char msg[1024];
930 FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, msg, sizeof(msg), NULL );
931 MESSAGE( "wine: could not load %s: %s", debugstr_w(main_exe_name), msg );
932 ExitProcess( error );
935 peb->ImageBaseAddress = module;
937 /* build command line */
938 set_library_wargv( __wine_main_argv );
939 if (!build_command_line( __wine_main_wargv )) goto error;
941 /* switch to the new stack */
942 wine_switch_to_stack( start_process, NULL, init_stack() );
944 error:
945 ExitProcess( GetLastError() );
949 /***********************************************************************
950 * build_argv
952 * Build an argv array from a command-line.
953 * 'reserved' is the number of args to reserve before the first one.
955 static char **build_argv( const WCHAR *cmdlineW, int reserved )
957 int argc;
958 char** argv;
959 char *arg,*s,*d,*cmdline;
960 int in_quotes,bcount,len;
962 len = WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, NULL, 0, NULL, NULL );
963 if (!(cmdline = malloc(len))) return NULL;
964 WideCharToMultiByte( CP_UNIXCP, 0, cmdlineW, -1, cmdline, len, NULL, NULL );
966 argc=reserved+1;
967 bcount=0;
968 in_quotes=0;
969 s=cmdline;
970 while (1) {
971 if (*s=='\0' || ((*s==' ' || *s=='\t') && !in_quotes)) {
972 /* space */
973 argc++;
974 /* skip the remaining spaces */
975 while (*s==' ' || *s=='\t') {
976 s++;
978 if (*s=='\0')
979 break;
980 bcount=0;
981 continue;
982 } else if (*s=='\\') {
983 /* '\', count them */
984 bcount++;
985 } else if ((*s=='"') && ((bcount & 1)==0)) {
986 /* unescaped '"' */
987 in_quotes=!in_quotes;
988 bcount=0;
989 } else {
990 /* a regular character */
991 bcount=0;
993 s++;
995 argv=malloc(argc*sizeof(*argv));
996 if (!argv)
997 return NULL;
999 arg=d=s=cmdline;
1000 bcount=0;
1001 in_quotes=0;
1002 argc=reserved;
1003 while (*s) {
1004 if ((*s==' ' || *s=='\t') && !in_quotes) {
1005 /* Close the argument and copy it */
1006 *d=0;
1007 argv[argc++]=arg;
1009 /* skip the remaining spaces */
1010 do {
1011 s++;
1012 } while (*s==' ' || *s=='\t');
1014 /* Start with a new argument */
1015 arg=d=s;
1016 bcount=0;
1017 } else if (*s=='\\') {
1018 /* '\\' */
1019 *d++=*s++;
1020 bcount++;
1021 } else if (*s=='"') {
1022 /* '"' */
1023 if ((bcount & 1)==0) {
1024 /* Preceded by an even number of '\', this is half that
1025 * number of '\', plus a '"' which we discard.
1027 d-=bcount/2;
1028 s++;
1029 in_quotes=!in_quotes;
1030 } else {
1031 /* Preceded by an odd number of '\', this is half that
1032 * number of '\' followed by a '"'
1034 d=d-bcount/2-1;
1035 *d++='"';
1036 s++;
1038 bcount=0;
1039 } else {
1040 /* a regular character */
1041 *d++=*s++;
1042 bcount=0;
1045 if (*arg) {
1046 *d='\0';
1047 argv[argc++]=arg;
1049 argv[argc]=NULL;
1051 return argv;
1055 /***********************************************************************
1056 * alloc_env_string
1058 * Allocate an environment string; helper for build_envp
1060 static char *alloc_env_string( const char *name, const char *value )
1062 char *ret = malloc( strlen(name) + strlen(value) + 1 );
1063 strcpy( ret, name );
1064 strcat( ret, value );
1065 return ret;
1068 /***********************************************************************
1069 * build_envp
1071 * Build the environment of a new child process.
1073 static char **build_envp( const WCHAR *envW )
1075 const WCHAR *end;
1076 char **envp;
1077 char *env, *p;
1078 int count = 0, length;
1080 for (end = envW; *end; count++) end += strlenW(end) + 1;
1081 end++;
1082 length = WideCharToMultiByte( CP_UNIXCP, 0, envW, end - envW, NULL, 0, NULL, NULL );
1083 if (!(env = malloc( length ))) return NULL;
1084 WideCharToMultiByte( CP_UNIXCP, 0, envW, end - envW, env, length, NULL, NULL );
1086 count += 4;
1088 if ((envp = malloc( count * sizeof(*envp) )))
1090 char **envptr = envp;
1092 /* some variables must not be modified, so we get them directly from the unix env */
1093 if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p );
1094 if ((p = getenv("TEMP"))) *envptr++ = alloc_env_string( "TEMP=", p );
1095 if ((p = getenv("TMP"))) *envptr++ = alloc_env_string( "TMP=", p );
1096 if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p );
1097 /* now put the Windows environment strings */
1098 for (p = env; *p; p += strlen(p) + 1)
1100 if (*p == '=') continue; /* skip drive curdirs, this crashes some unix apps */
1101 if (!strncmp( p, "WINEPRELOADRESERVE=", sizeof("WINEPRELOADRESERVE=")-1 )) continue;
1102 if (is_special_env_var( p )) /* prefix it with "WINE" */
1103 *envptr++ = alloc_env_string( "WINE", p );
1104 else
1105 *envptr++ = p;
1107 *envptr = 0;
1109 return envp;
1113 /***********************************************************************
1114 * fork_and_exec
1116 * Fork and exec a new Unix binary, checking for errors.
1118 static int fork_and_exec( const char *filename, const WCHAR *cmdline,
1119 const WCHAR *env, const char *newdir )
1121 int fd[2];
1122 int pid, err;
1124 if (!env) env = GetEnvironmentStringsW();
1126 if (pipe(fd) == -1)
1128 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1129 return -1;
1131 fcntl( fd[1], F_SETFD, 1 ); /* set close on exec */
1132 if (!(pid = fork())) /* child */
1134 char **argv = build_argv( cmdline, 0 );
1135 char **envp = build_envp( env );
1136 close( fd[0] );
1138 /* Reset signals that we previously set to SIG_IGN */
1139 signal( SIGPIPE, SIG_DFL );
1140 signal( SIGCHLD, SIG_DFL );
1142 if (newdir) chdir(newdir);
1144 if (argv && envp) execve( filename, argv, envp );
1145 err = errno;
1146 write( fd[1], &err, sizeof(err) );
1147 _exit(1);
1149 close( fd[1] );
1150 if ((pid != -1) && (read( fd[0], &err, sizeof(err) ) > 0)) /* exec failed */
1152 errno = err;
1153 pid = -1;
1155 if (pid == -1) FILE_SetDosError();
1156 close( fd[0] );
1157 return pid;
1161 /***********************************************************************
1162 * create_user_params
1164 static RTL_USER_PROCESS_PARAMETERS *create_user_params( LPCWSTR filename, LPCWSTR cmdline,
1165 LPCWSTR cur_dir, LPWSTR env, DWORD flags,
1166 const STARTUPINFOW *startup )
1168 RTL_USER_PROCESS_PARAMETERS *params;
1169 UNICODE_STRING image_str, cmdline_str, curdir_str, desktop, title, runtime;
1170 NTSTATUS status;
1171 WCHAR buffer[MAX_PATH];
1173 if(!GetLongPathNameW( filename, buffer, MAX_PATH ))
1174 lstrcpynW( buffer, filename, MAX_PATH );
1175 if(!GetFullPathNameW( buffer, MAX_PATH, buffer, NULL ))
1176 lstrcpynW( buffer, filename, MAX_PATH );
1177 RtlInitUnicodeString( &image_str, buffer );
1179 RtlInitUnicodeString( &cmdline_str, cmdline );
1180 if (cur_dir) RtlInitUnicodeString( &curdir_str, cur_dir );
1181 if (startup->lpDesktop) RtlInitUnicodeString( &desktop, startup->lpDesktop );
1182 if (startup->lpTitle) RtlInitUnicodeString( &title, startup->lpTitle );
1183 if (startup->lpReserved2 && startup->cbReserved2)
1185 runtime.Length = 0;
1186 runtime.MaximumLength = startup->cbReserved2;
1187 runtime.Buffer = (WCHAR*)startup->lpReserved2;
1190 status = RtlCreateProcessParameters( &params, &image_str, NULL,
1191 cur_dir ? &curdir_str : NULL,
1192 &cmdline_str, env,
1193 startup->lpTitle ? &title : NULL,
1194 startup->lpDesktop ? &desktop : NULL,
1195 NULL,
1196 (startup->lpReserved2 && startup->cbReserved2) ? &runtime : NULL );
1197 if (status != STATUS_SUCCESS)
1199 SetLastError( RtlNtStatusToDosError(status) );
1200 return NULL;
1203 if (flags & CREATE_NEW_PROCESS_GROUP) params->ConsoleFlags = 1;
1204 if (flags & CREATE_NEW_CONSOLE) params->ConsoleHandle = (HANDLE)1; /* FIXME: cf. kernel_main.c */
1206 params->hStdInput = startup->hStdInput;
1207 params->hStdOutput = startup->hStdOutput;
1208 params->hStdError = startup->hStdError;
1209 params->dwX = startup->dwX;
1210 params->dwY = startup->dwY;
1211 params->dwXSize = startup->dwXSize;
1212 params->dwYSize = startup->dwYSize;
1213 params->dwXCountChars = startup->dwXCountChars;
1214 params->dwYCountChars = startup->dwYCountChars;
1215 params->dwFillAttribute = startup->dwFillAttribute;
1216 params->dwFlags = startup->dwFlags;
1217 params->wShowWindow = startup->wShowWindow;
1218 return params;
1222 /***********************************************************************
1223 * create_process
1225 * Create a new process. If hFile is a valid handle we have an exe
1226 * file, otherwise it is a Winelib app.
1228 static BOOL create_process( HANDLE hFile, LPCWSTR filename, LPWSTR cmd_line, LPWSTR env,
1229 LPCWSTR cur_dir, LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1230 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1231 LPPROCESS_INFORMATION info, LPCSTR unixdir,
1232 void *res_start, void *res_end )
1234 BOOL ret, success = FALSE;
1235 HANDLE process_info;
1236 WCHAR *env_end;
1237 char *winedebug = NULL;
1238 RTL_USER_PROCESS_PARAMETERS *params;
1239 int startfd[2];
1240 int execfd[2];
1241 pid_t pid;
1242 int err;
1243 char dummy = 0;
1244 char preloader_reserve[64];
1246 if (!env) RtlAcquirePebLock();
1248 if (!(params = create_user_params( filename, cmd_line, cur_dir, env, flags, startup )))
1250 if (!env) RtlReleasePebLock();
1251 return FALSE;
1253 env_end = params->Environment;
1254 while (*env_end)
1256 static const WCHAR WINEDEBUG[] = {'W','I','N','E','D','E','B','U','G','=',0};
1257 if (!winedebug && !strncmpW( env_end, WINEDEBUG, sizeof(WINEDEBUG)/sizeof(WCHAR) - 1 ))
1259 DWORD len = WideCharToMultiByte( CP_UNIXCP, 0, env_end, -1, NULL, 0, NULL, NULL );
1260 if ((winedebug = HeapAlloc( GetProcessHeap(), 0, len )))
1261 WideCharToMultiByte( CP_UNIXCP, 0, env_end, -1, winedebug, len, NULL, NULL );
1263 env_end += strlenW(env_end) + 1;
1265 env_end++;
1267 sprintf( preloader_reserve, "WINEPRELOADRESERVE=%lx-%lx%c",
1268 (unsigned long)res_start, (unsigned long)res_end, 0 );
1270 /* create the synchronization pipes */
1272 if (pipe( startfd ) == -1)
1274 if (!env) RtlReleasePebLock();
1275 HeapFree( GetProcessHeap(), 0, winedebug );
1276 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1277 RtlDestroyProcessParameters( params );
1278 return FALSE;
1280 if (pipe( execfd ) == -1)
1282 if (!env) RtlReleasePebLock();
1283 HeapFree( GetProcessHeap(), 0, winedebug );
1284 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
1285 close( startfd[0] );
1286 close( startfd[1] );
1287 RtlDestroyProcessParameters( params );
1288 return FALSE;
1290 fcntl( execfd[1], F_SETFD, 1 ); /* set close on exec */
1292 /* create the child process */
1294 if (!(pid = fork())) /* child */
1296 char **argv = build_argv( cmd_line, 1 );
1298 close( startfd[1] );
1299 close( execfd[0] );
1301 /* wait for parent to tell us to start */
1302 if (read( startfd[0], &dummy, 1 ) != 1) _exit(1);
1304 close( startfd[0] );
1305 /* Reset signals that we previously set to SIG_IGN */
1306 signal( SIGPIPE, SIG_DFL );
1307 signal( SIGCHLD, SIG_DFL );
1309 putenv( preloader_reserve );
1310 if (winedebug) putenv( winedebug );
1311 if (unixdir) chdir(unixdir);
1313 if (argv) wine_exec_wine_binary( NULL, argv, getenv("WINELOADER") );
1315 err = errno;
1316 write( execfd[1], &err, sizeof(err) );
1317 _exit(1);
1320 /* this is the parent */
1322 close( startfd[0] );
1323 close( execfd[1] );
1324 HeapFree( GetProcessHeap(), 0, winedebug );
1325 if (pid == -1)
1327 if (!env) RtlReleasePebLock();
1328 close( startfd[1] );
1329 close( execfd[0] );
1330 FILE_SetDosError();
1331 RtlDestroyProcessParameters( params );
1332 return FALSE;
1335 /* create the process on the server side */
1337 SERVER_START_REQ( new_process )
1339 req->inherit_all = inherit;
1340 req->create_flags = flags;
1341 req->unix_pid = pid;
1342 req->exe_file = hFile;
1343 if (startup->dwFlags & STARTF_USESTDHANDLES)
1345 req->hstdin = startup->hStdInput;
1346 req->hstdout = startup->hStdOutput;
1347 req->hstderr = startup->hStdError;
1349 else
1351 req->hstdin = GetStdHandle( STD_INPUT_HANDLE );
1352 req->hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
1353 req->hstderr = GetStdHandle( STD_ERROR_HANDLE );
1356 if ((flags & (CREATE_NEW_CONSOLE | DETACHED_PROCESS)) != 0)
1358 /* this is temporary (for console handles). We have no way to control that the handle is invalid in child process otherwise */
1359 if (is_console_handle(req->hstdin)) req->hstdin = INVALID_HANDLE_VALUE;
1360 if (is_console_handle(req->hstdout)) req->hstdout = INVALID_HANDLE_VALUE;
1361 if (is_console_handle(req->hstderr)) req->hstderr = INVALID_HANDLE_VALUE;
1363 else
1365 if (is_console_handle(req->hstdin)) req->hstdin = console_handle_unmap(req->hstdin);
1366 if (is_console_handle(req->hstdout)) req->hstdout = console_handle_unmap(req->hstdout);
1367 if (is_console_handle(req->hstderr)) req->hstderr = console_handle_unmap(req->hstderr);
1370 wine_server_add_data( req, params, params->Size );
1371 wine_server_add_data( req, params->Environment, (env_end-params->Environment)*sizeof(WCHAR) );
1372 ret = !wine_server_call_err( req );
1373 process_info = reply->info;
1375 SERVER_END_REQ;
1377 if (!env) RtlReleasePebLock();
1378 RtlDestroyProcessParameters( params );
1379 if (!ret)
1381 close( startfd[1] );
1382 close( execfd[0] );
1383 return FALSE;
1386 /* tell child to start and wait for it to exec */
1388 write( startfd[1], &dummy, 1 );
1389 close( startfd[1] );
1391 if (read( execfd[0], &err, sizeof(err) ) > 0) /* exec failed */
1393 errno = err;
1394 FILE_SetDosError();
1395 close( execfd[0] );
1396 CloseHandle( process_info );
1397 return FALSE;
1399 close( execfd[0] );
1401 /* wait for the new process info to be ready */
1403 WaitForSingleObject( process_info, INFINITE );
1404 SERVER_START_REQ( get_new_process_info )
1406 req->info = process_info;
1407 req->process_access = PROCESS_ALL_ACCESS;
1408 req->process_attr = (psa && (psa->nLength >= sizeof(*psa)) && psa->bInheritHandle) ? OBJ_INHERIT : 0;
1409 req->thread_access = THREAD_ALL_ACCESS;
1410 req->thread_attr = (tsa && (tsa->nLength >= sizeof(*tsa)) && tsa->bInheritHandle) ? OBJ_INHERIT : 0;
1411 if ((ret = !wine_server_call_err( req )))
1413 info->dwProcessId = (DWORD)reply->pid;
1414 info->dwThreadId = (DWORD)reply->tid;
1415 info->hProcess = reply->phandle;
1416 info->hThread = reply->thandle;
1417 success = reply->success;
1420 SERVER_END_REQ;
1422 if (ret && !success) /* new process failed to start */
1424 DWORD exitcode;
1425 if (GetExitCodeProcess( info->hProcess, &exitcode )) SetLastError( exitcode );
1426 CloseHandle( info->hThread );
1427 CloseHandle( info->hProcess );
1428 ret = FALSE;
1430 CloseHandle( process_info );
1431 return ret;
1435 /***********************************************************************
1436 * create_vdm_process
1438 * Create a new VDM process for a 16-bit or DOS application.
1440 static BOOL create_vdm_process( LPCWSTR filename, LPWSTR cmd_line, LPWSTR env, LPCWSTR cur_dir,
1441 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1442 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1443 LPPROCESS_INFORMATION info, LPCSTR unixdir )
1445 static const WCHAR argsW[] = {'%','s',' ','-','-','a','p','p','-','n','a','m','e',' ','"','%','s','"',' ','%','s',0};
1447 BOOL ret;
1448 LPWSTR new_cmd_line = HeapAlloc( GetProcessHeap(), 0,
1449 (strlenW(filename) + strlenW(cmd_line) + 30) * sizeof(WCHAR) );
1451 if (!new_cmd_line)
1453 SetLastError( ERROR_OUTOFMEMORY );
1454 return FALSE;
1456 sprintfW( new_cmd_line, argsW, winevdmW, filename, cmd_line );
1457 ret = create_process( 0, winevdmW, new_cmd_line, env, cur_dir, psa, tsa, inherit,
1458 flags, startup, info, unixdir, NULL, NULL );
1459 HeapFree( GetProcessHeap(), 0, new_cmd_line );
1460 return ret;
1464 /***********************************************************************
1465 * create_cmd_process
1467 * Create a new cmd shell process for a .BAT file.
1469 static BOOL create_cmd_process( LPCWSTR filename, LPWSTR cmd_line, LPVOID env, LPCWSTR cur_dir,
1470 LPSECURITY_ATTRIBUTES psa, LPSECURITY_ATTRIBUTES tsa,
1471 BOOL inherit, DWORD flags, LPSTARTUPINFOW startup,
1472 LPPROCESS_INFORMATION info )
1475 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
1476 static const WCHAR slashcW[] = {' ','/','c',' ',0};
1477 WCHAR comspec[MAX_PATH];
1478 WCHAR *newcmdline;
1479 BOOL ret;
1481 if (!GetEnvironmentVariableW( comspecW, comspec, sizeof(comspec)/sizeof(WCHAR) ))
1482 return FALSE;
1483 if (!(newcmdline = HeapAlloc( GetProcessHeap(), 0,
1484 (strlenW(comspec) + 4 + strlenW(cmd_line) + 1) * sizeof(WCHAR))))
1485 return FALSE;
1487 strcpyW( newcmdline, comspec );
1488 strcatW( newcmdline, slashcW );
1489 strcatW( newcmdline, cmd_line );
1490 ret = CreateProcessW( comspec, newcmdline, psa, tsa, inherit,
1491 flags, env, cur_dir, startup, info );
1492 HeapFree( GetProcessHeap(), 0, newcmdline );
1493 return ret;
1497 /*************************************************************************
1498 * get_file_name
1500 * Helper for CreateProcess: retrieve the file name to load from the
1501 * app name and command line. Store the file name in buffer, and
1502 * return a possibly modified command line.
1503 * Also returns a handle to the opened file if it's a Windows binary.
1505 static LPWSTR get_file_name( LPCWSTR appname, LPWSTR cmdline, LPWSTR buffer,
1506 int buflen, HANDLE *handle )
1508 static const WCHAR quotesW[] = {'"','%','s','"',0};
1510 WCHAR *name, *pos, *ret = NULL;
1511 const WCHAR *p;
1512 BOOL got_space;
1514 /* if we have an app name, everything is easy */
1516 if (appname)
1518 /* use the unmodified app name as file name */
1519 lstrcpynW( buffer, appname, buflen );
1520 *handle = open_exe_file( buffer );
1521 if (!(ret = cmdline) || !cmdline[0])
1523 /* no command-line, create one */
1524 if ((ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(appname) + 3) * sizeof(WCHAR) )))
1525 sprintfW( ret, quotesW, appname );
1527 return ret;
1530 if (!cmdline)
1532 SetLastError( ERROR_INVALID_PARAMETER );
1533 return NULL;
1536 /* first check for a quoted file name */
1538 if ((cmdline[0] == '"') && ((p = strchrW( cmdline + 1, '"' ))))
1540 int len = p - cmdline - 1;
1541 /* extract the quoted portion as file name */
1542 if (!(name = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) ))) return NULL;
1543 memcpy( name, cmdline + 1, len * sizeof(WCHAR) );
1544 name[len] = 0;
1546 if (find_exe_file( name, buffer, buflen, handle ))
1547 ret = cmdline; /* no change necessary */
1548 goto done;
1551 /* now try the command-line word by word */
1553 if (!(name = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 1) * sizeof(WCHAR) )))
1554 return NULL;
1555 pos = name;
1556 p = cmdline;
1557 got_space = FALSE;
1559 while (*p)
1561 do *pos++ = *p++; while (*p && *p != ' ' && *p != '\t');
1562 *pos = 0;
1563 if (find_exe_file( name, buffer, buflen, handle ))
1565 ret = cmdline;
1566 break;
1568 if (*p) got_space = TRUE;
1571 if (ret && got_space) /* now build a new command-line with quotes */
1573 if (!(ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(cmdline) + 3) * sizeof(WCHAR) )))
1574 goto done;
1575 sprintfW( ret, quotesW, name );
1576 strcatW( ret, p );
1579 done:
1580 HeapFree( GetProcessHeap(), 0, name );
1581 return ret;
1585 /**********************************************************************
1586 * CreateProcessA (KERNEL32.@)
1588 BOOL WINAPI CreateProcessA( LPCSTR app_name, LPSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1589 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit,
1590 DWORD flags, LPVOID env, LPCSTR cur_dir,
1591 LPSTARTUPINFOA startup_info, LPPROCESS_INFORMATION info )
1593 BOOL ret = FALSE;
1594 WCHAR *app_nameW = NULL, *cmd_lineW = NULL, *cur_dirW = NULL;
1595 UNICODE_STRING desktopW, titleW;
1596 STARTUPINFOW infoW;
1598 desktopW.Buffer = NULL;
1599 titleW.Buffer = NULL;
1600 if (app_name && !(app_nameW = FILE_name_AtoW( app_name, TRUE ))) goto done;
1601 if (cmd_line && !(cmd_lineW = FILE_name_AtoW( cmd_line, TRUE ))) goto done;
1602 if (cur_dir && !(cur_dirW = FILE_name_AtoW( cur_dir, TRUE ))) goto done;
1604 if (startup_info->lpDesktop) RtlCreateUnicodeStringFromAsciiz( &desktopW, startup_info->lpDesktop );
1605 if (startup_info->lpTitle) RtlCreateUnicodeStringFromAsciiz( &titleW, startup_info->lpTitle );
1607 memcpy( &infoW, startup_info, sizeof(infoW) );
1608 infoW.lpDesktop = desktopW.Buffer;
1609 infoW.lpTitle = titleW.Buffer;
1611 if (startup_info->lpReserved)
1612 FIXME("StartupInfo.lpReserved is used, please report (%s)\n",
1613 debugstr_a(startup_info->lpReserved));
1615 ret = CreateProcessW( app_nameW, cmd_lineW, process_attr, thread_attr,
1616 inherit, flags, env, cur_dirW, &infoW, info );
1617 done:
1618 HeapFree( GetProcessHeap(), 0, app_nameW );
1619 HeapFree( GetProcessHeap(), 0, cmd_lineW );
1620 HeapFree( GetProcessHeap(), 0, cur_dirW );
1621 RtlFreeUnicodeString( &desktopW );
1622 RtlFreeUnicodeString( &titleW );
1623 return ret;
1627 /**********************************************************************
1628 * CreateProcessW (KERNEL32.@)
1630 BOOL WINAPI CreateProcessW( LPCWSTR app_name, LPWSTR cmd_line, LPSECURITY_ATTRIBUTES process_attr,
1631 LPSECURITY_ATTRIBUTES thread_attr, BOOL inherit, DWORD flags,
1632 LPVOID env, LPCWSTR cur_dir, LPSTARTUPINFOW startup_info,
1633 LPPROCESS_INFORMATION info )
1635 BOOL retv = FALSE;
1636 HANDLE hFile = 0;
1637 char *unixdir = NULL;
1638 WCHAR name[MAX_PATH];
1639 WCHAR *tidy_cmdline, *p, *envW = env;
1640 void *res_start, *res_end;
1642 /* Process the AppName and/or CmdLine to get module name and path */
1644 TRACE("app %s cmdline %s\n", debugstr_w(app_name), debugstr_w(cmd_line) );
1646 if (!(tidy_cmdline = get_file_name( app_name, cmd_line, name, sizeof(name)/sizeof(WCHAR), &hFile )))
1647 return FALSE;
1648 if (hFile == INVALID_HANDLE_VALUE) goto done;
1650 /* Warn if unsupported features are used */
1652 if (flags & (IDLE_PRIORITY_CLASS | HIGH_PRIORITY_CLASS | REALTIME_PRIORITY_CLASS |
1653 CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM | CREATE_SHARED_WOW_VDM |
1654 CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |
1655 PROFILE_USER | PROFILE_KERNEL | PROFILE_SERVER))
1656 WARN("(%s,...): ignoring some flags in %lx\n", debugstr_w(name), flags);
1658 if (cur_dir)
1660 unixdir = wine_get_unix_file_name( cur_dir );
1662 else
1664 WCHAR buf[MAX_PATH];
1665 if (GetCurrentDirectoryW(MAX_PATH, buf)) unixdir = wine_get_unix_file_name( buf );
1668 if (env && !(flags & CREATE_UNICODE_ENVIRONMENT)) /* convert environment to unicode */
1670 char *p = env;
1671 DWORD lenW;
1673 while (*p) p += strlen(p) + 1;
1674 p++; /* final null */
1675 lenW = MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, NULL, 0 );
1676 envW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) );
1677 MultiByteToWideChar( CP_ACP, 0, env, p - (char*)env, envW, lenW );
1678 flags |= CREATE_UNICODE_ENVIRONMENT;
1681 info->hThread = info->hProcess = 0;
1682 info->dwProcessId = info->dwThreadId = 0;
1684 /* Determine executable type */
1686 if (!hFile) /* builtin exe */
1688 TRACE( "starting %s as Winelib app\n", debugstr_w(name) );
1689 retv = create_process( 0, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1690 inherit, flags, startup_info, info, unixdir, NULL, NULL );
1691 goto done;
1694 switch( MODULE_GetBinaryType( hFile, &res_start, &res_end ))
1696 case BINARY_PE_EXE:
1697 TRACE( "starting %s as Win32 binary (%p-%p)\n", debugstr_w(name), res_start, res_end );
1698 retv = create_process( hFile, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1699 inherit, flags, startup_info, info, unixdir, res_start, res_end );
1700 break;
1701 case BINARY_OS216:
1702 case BINARY_WIN16:
1703 case BINARY_DOS:
1704 TRACE( "starting %s as Win16/DOS binary\n", debugstr_w(name) );
1705 retv = create_vdm_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1706 inherit, flags, startup_info, info, unixdir );
1707 break;
1708 case BINARY_PE_DLL:
1709 TRACE( "not starting %s since it is a dll\n", debugstr_w(name) );
1710 SetLastError( ERROR_BAD_EXE_FORMAT );
1711 break;
1712 case BINARY_UNIX_LIB:
1713 TRACE( "%s is a Unix library, starting as Winelib app\n", debugstr_w(name) );
1714 retv = create_process( hFile, name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1715 inherit, flags, startup_info, info, unixdir, NULL, NULL );
1716 break;
1717 case BINARY_UNKNOWN:
1718 /* check for .com or .bat extension */
1719 if ((p = strrchrW( name, '.' )))
1721 if (!strcmpiW( p, comW ) || !strcmpiW( p, pifW ))
1723 TRACE( "starting %s as DOS binary\n", debugstr_w(name) );
1724 retv = create_vdm_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1725 inherit, flags, startup_info, info, unixdir );
1726 break;
1728 if (!strcmpiW( p, batW ))
1730 TRACE( "starting %s as batch binary\n", debugstr_w(name) );
1731 retv = create_cmd_process( name, tidy_cmdline, envW, cur_dir, process_attr, thread_attr,
1732 inherit, flags, startup_info, info );
1733 break;
1736 /* fall through */
1737 case BINARY_UNIX_EXE:
1739 /* unknown file, try as unix executable */
1740 char *unix_name;
1742 TRACE( "starting %s as Unix binary\n", debugstr_w(name) );
1744 if ((unix_name = wine_get_unix_file_name( name )))
1746 retv = (fork_and_exec( unix_name, tidy_cmdline, envW, unixdir ) != -1);
1747 HeapFree( GetProcessHeap(), 0, unix_name );
1750 break;
1752 CloseHandle( hFile );
1754 done:
1755 if (tidy_cmdline != cmd_line) HeapFree( GetProcessHeap(), 0, tidy_cmdline );
1756 if (envW != env) HeapFree( GetProcessHeap(), 0, envW );
1757 HeapFree( GetProcessHeap(), 0, unixdir );
1758 return retv;
1762 /***********************************************************************
1763 * wait_input_idle
1765 * Wrapper to call WaitForInputIdle USER function
1767 typedef DWORD (WINAPI *WaitForInputIdle_ptr)( HANDLE hProcess, DWORD dwTimeOut );
1769 static DWORD wait_input_idle( HANDLE process, DWORD timeout )
1771 HMODULE mod = GetModuleHandleA( "user32.dll" );
1772 if (mod)
1774 WaitForInputIdle_ptr ptr = (WaitForInputIdle_ptr)GetProcAddress( mod, "WaitForInputIdle" );
1775 if (ptr) return ptr( process, timeout );
1777 return 0;
1781 /***********************************************************************
1782 * WinExec (KERNEL32.@)
1784 UINT WINAPI WinExec( LPCSTR lpCmdLine, UINT nCmdShow )
1786 PROCESS_INFORMATION info;
1787 STARTUPINFOA startup;
1788 char *cmdline;
1789 UINT ret;
1791 memset( &startup, 0, sizeof(startup) );
1792 startup.cb = sizeof(startup);
1793 startup.dwFlags = STARTF_USESHOWWINDOW;
1794 startup.wShowWindow = nCmdShow;
1796 /* cmdline needs to be writeable for CreateProcess */
1797 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(lpCmdLine)+1 ))) return 0;
1798 strcpy( cmdline, lpCmdLine );
1800 if (CreateProcessA( NULL, cmdline, NULL, NULL, FALSE,
1801 0, NULL, NULL, &startup, &info ))
1803 /* Give 30 seconds to the app to come up */
1804 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1805 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1806 ret = 33;
1807 /* Close off the handles */
1808 CloseHandle( info.hThread );
1809 CloseHandle( info.hProcess );
1811 else if ((ret = GetLastError()) >= 32)
1813 FIXME("Strange error set by CreateProcess: %d\n", ret );
1814 ret = 11;
1816 HeapFree( GetProcessHeap(), 0, cmdline );
1817 return ret;
1821 /**********************************************************************
1822 * LoadModule (KERNEL32.@)
1824 HINSTANCE WINAPI LoadModule( LPCSTR name, LPVOID paramBlock )
1826 LOADPARMS32 *params = paramBlock;
1827 PROCESS_INFORMATION info;
1828 STARTUPINFOA startup;
1829 HINSTANCE hInstance;
1830 LPSTR cmdline, p;
1831 char filename[MAX_PATH];
1832 BYTE len;
1834 if (!name) return (HINSTANCE)ERROR_FILE_NOT_FOUND;
1836 if (!SearchPathA( NULL, name, ".exe", sizeof(filename), filename, NULL ) &&
1837 !SearchPathA( NULL, name, NULL, sizeof(filename), filename, NULL ))
1838 return (HINSTANCE)GetLastError();
1840 len = (BYTE)params->lpCmdLine[0];
1841 if (!(cmdline = HeapAlloc( GetProcessHeap(), 0, strlen(filename) + len + 2 )))
1842 return (HINSTANCE)ERROR_NOT_ENOUGH_MEMORY;
1844 strcpy( cmdline, filename );
1845 p = cmdline + strlen(cmdline);
1846 *p++ = ' ';
1847 memcpy( p, params->lpCmdLine + 1, len );
1848 p[len] = 0;
1850 memset( &startup, 0, sizeof(startup) );
1851 startup.cb = sizeof(startup);
1852 if (params->lpCmdShow)
1854 startup.dwFlags = STARTF_USESHOWWINDOW;
1855 startup.wShowWindow = ((WORD *)params->lpCmdShow)[1];
1858 if (CreateProcessA( filename, cmdline, NULL, NULL, FALSE, 0,
1859 params->lpEnvAddress, NULL, &startup, &info ))
1861 /* Give 30 seconds to the app to come up */
1862 if (wait_input_idle( info.hProcess, 30000 ) == WAIT_FAILED)
1863 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
1864 hInstance = (HINSTANCE)33;
1865 /* Close off the handles */
1866 CloseHandle( info.hThread );
1867 CloseHandle( info.hProcess );
1869 else if ((hInstance = (HINSTANCE)GetLastError()) >= (HINSTANCE)32)
1871 FIXME("Strange error set by CreateProcess: %p\n", hInstance );
1872 hInstance = (HINSTANCE)11;
1875 HeapFree( GetProcessHeap(), 0, cmdline );
1876 return hInstance;
1880 /******************************************************************************
1881 * TerminateProcess (KERNEL32.@)
1883 BOOL WINAPI TerminateProcess( HANDLE handle, DWORD exit_code )
1885 NTSTATUS status = NtTerminateProcess( handle, exit_code );
1886 if (status) SetLastError( RtlNtStatusToDosError(status) );
1887 return !status;
1891 /***********************************************************************
1892 * ExitProcess (KERNEL32.@)
1894 void WINAPI ExitProcess( DWORD status )
1896 LdrShutdownProcess();
1897 NtTerminateProcess(GetCurrentProcess(), status);
1898 exit(status);
1902 /***********************************************************************
1903 * GetExitCodeProcess [KERNEL32.@]
1905 * Gets termination status of specified process.
1907 * PARAMS
1908 * hProcess [in] Handle to the process.
1909 * lpExitCode [out] Address to receive termination status.
1911 * RETURNS
1912 * Success: TRUE
1913 * Failure: FALSE
1915 BOOL WINAPI GetExitCodeProcess( HANDLE hProcess, LPDWORD lpExitCode )
1917 NTSTATUS status;
1918 PROCESS_BASIC_INFORMATION pbi;
1920 status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi,
1921 sizeof(pbi), NULL);
1922 if (status == STATUS_SUCCESS)
1924 if (lpExitCode) *lpExitCode = pbi.ExitStatus;
1925 return TRUE;
1927 SetLastError( RtlNtStatusToDosError(status) );
1928 return FALSE;
1932 /***********************************************************************
1933 * SetErrorMode (KERNEL32.@)
1935 UINT WINAPI SetErrorMode( UINT mode )
1937 UINT old = process_error_mode;
1938 process_error_mode = mode;
1939 return old;
1943 /**********************************************************************
1944 * TlsAlloc [KERNEL32.@]
1946 * Allocates a thread local storage index.
1948 * RETURNS
1949 * Success: TLS index.
1950 * Failure: 0xFFFFFFFF
1952 DWORD WINAPI TlsAlloc( void )
1954 DWORD index;
1955 PEB * const peb = NtCurrentTeb()->Peb;
1957 RtlAcquirePebLock();
1958 index = RtlFindClearBitsAndSet( peb->TlsBitmap, 1, 0 );
1959 if (index != ~0U) NtCurrentTeb()->TlsSlots[index] = 0; /* clear the value */
1960 else
1962 index = RtlFindClearBitsAndSet( peb->TlsExpansionBitmap, 1, 0 );
1963 if (index != ~0U)
1965 if (!NtCurrentTeb()->TlsExpansionSlots &&
1966 !(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1967 8 * sizeof(peb->TlsExpansionBitmapBits) * sizeof(void*) )))
1969 RtlClearBits( peb->TlsExpansionBitmap, index, 1 );
1970 index = ~0U;
1971 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1973 else
1975 NtCurrentTeb()->TlsExpansionSlots[index] = 0; /* clear the value */
1976 index += TLS_MINIMUM_AVAILABLE;
1979 else SetLastError( ERROR_NO_MORE_ITEMS );
1981 RtlReleasePebLock();
1982 return index;
1986 /**********************************************************************
1987 * TlsFree [KERNEL32.@]
1989 * Releases a thread local storage index, making it available for reuse.
1991 * PARAMS
1992 * index [in] TLS index to free.
1994 * RETURNS
1995 * Success: TRUE
1996 * Failure: FALSE
1998 BOOL WINAPI TlsFree( DWORD index )
2000 BOOL ret;
2002 RtlAcquirePebLock();
2003 if (index >= TLS_MINIMUM_AVAILABLE)
2005 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 );
2006 if (ret) RtlClearBits( NtCurrentTeb()->Peb->TlsExpansionBitmap, index - TLS_MINIMUM_AVAILABLE, 1 );
2008 else
2010 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
2011 if (ret) RtlClearBits( NtCurrentTeb()->Peb->TlsBitmap, index, 1 );
2013 if (ret) NtSetInformationThread( GetCurrentThread(), ThreadZeroTlsCell, &index, sizeof(index) );
2014 else SetLastError( ERROR_INVALID_PARAMETER );
2015 RtlReleasePebLock();
2016 return TRUE;
2020 /**********************************************************************
2021 * TlsGetValue [KERNEL32.@]
2023 * Gets value in a thread's TLS slot.
2025 * PARAMS
2026 * index [in] TLS index to retrieve value for.
2028 * RETURNS
2029 * Success: Value stored in calling thread's TLS slot for index.
2030 * Failure: 0 and GetLastError() returns NO_ERROR.
2032 LPVOID WINAPI TlsGetValue( DWORD index )
2034 LPVOID ret;
2036 if (index < TLS_MINIMUM_AVAILABLE)
2038 ret = NtCurrentTeb()->TlsSlots[index];
2040 else
2042 index -= TLS_MINIMUM_AVAILABLE;
2043 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
2045 SetLastError( ERROR_INVALID_PARAMETER );
2046 return NULL;
2048 if (!NtCurrentTeb()->TlsExpansionSlots) ret = NULL;
2049 else ret = NtCurrentTeb()->TlsExpansionSlots[index];
2051 SetLastError( ERROR_SUCCESS );
2052 return ret;
2056 /**********************************************************************
2057 * TlsSetValue [KERNEL32.@]
2059 * Stores a value in the thread's TLS slot.
2061 * PARAMS
2062 * index [in] TLS index to set value for.
2063 * value [in] Value to be stored.
2065 * RETURNS
2066 * Success: TRUE
2067 * Failure: FALSE
2069 BOOL WINAPI TlsSetValue( DWORD index, LPVOID value )
2071 if (index < TLS_MINIMUM_AVAILABLE)
2073 NtCurrentTeb()->TlsSlots[index] = value;
2075 else
2077 index -= TLS_MINIMUM_AVAILABLE;
2078 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
2080 SetLastError( ERROR_INVALID_PARAMETER );
2081 return FALSE;
2083 if (!NtCurrentTeb()->TlsExpansionSlots &&
2084 !(NtCurrentTeb()->TlsExpansionSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
2085 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits) * sizeof(void*) )))
2087 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
2088 return FALSE;
2090 NtCurrentTeb()->TlsExpansionSlots[index] = value;
2092 return TRUE;
2096 /***********************************************************************
2097 * GetProcessFlags (KERNEL32.@)
2099 DWORD WINAPI GetProcessFlags( DWORD processid )
2101 IMAGE_NT_HEADERS *nt;
2102 DWORD flags = 0;
2104 if (processid && processid != GetCurrentProcessId()) return 0;
2106 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2108 if (nt->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI)
2109 flags |= PDB32_CONSOLE_PROC;
2111 if (!AreFileApisANSI()) flags |= PDB32_FILE_APIS_OEM;
2112 if (IsDebuggerPresent()) flags |= PDB32_DEBUGGED;
2113 return flags;
2117 /***********************************************************************
2118 * GetProcessDword (KERNEL.485)
2119 * GetProcessDword (KERNEL32.18)
2120 * 'Of course you cannot directly access Windows internal structures'
2122 DWORD WINAPI GetProcessDword( DWORD dwProcessID, INT offset )
2124 DWORD x, y;
2125 STARTUPINFOW siw;
2127 TRACE("(%ld, %d)\n", dwProcessID, offset );
2129 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2131 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2132 return 0;
2135 switch ( offset )
2137 case GPD_APP_COMPAT_FLAGS:
2138 return GetAppCompatFlags16(0);
2139 case GPD_LOAD_DONE_EVENT:
2140 return 0;
2141 case GPD_HINSTANCE16:
2142 return GetTaskDS16();
2143 case GPD_WINDOWS_VERSION:
2144 return GetExeVersion16();
2145 case GPD_THDB:
2146 return (DWORD)NtCurrentTeb() - 0x10 /* FIXME */;
2147 case GPD_PDB:
2148 return (DWORD)NtCurrentTeb()->Peb;
2149 case GPD_STARTF_SHELLDATA: /* return stdoutput handle from startupinfo ??? */
2150 GetStartupInfoW(&siw);
2151 return (DWORD)siw.hStdOutput;
2152 case GPD_STARTF_HOTKEY: /* return stdinput handle from startupinfo ??? */
2153 GetStartupInfoW(&siw);
2154 return (DWORD)siw.hStdInput;
2155 case GPD_STARTF_SHOWWINDOW:
2156 GetStartupInfoW(&siw);
2157 return siw.wShowWindow;
2158 case GPD_STARTF_SIZE:
2159 GetStartupInfoW(&siw);
2160 x = siw.dwXSize;
2161 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2162 y = siw.dwYSize;
2163 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2164 return MAKELONG( x, y );
2165 case GPD_STARTF_POSITION:
2166 GetStartupInfoW(&siw);
2167 x = siw.dwX;
2168 if ( (INT)x == CW_USEDEFAULT ) x = CW_USEDEFAULT16;
2169 y = siw.dwY;
2170 if ( (INT)y == CW_USEDEFAULT ) y = CW_USEDEFAULT16;
2171 return MAKELONG( x, y );
2172 case GPD_STARTF_FLAGS:
2173 GetStartupInfoW(&siw);
2174 return siw.dwFlags;
2175 case GPD_PARENT:
2176 return 0;
2177 case GPD_FLAGS:
2178 return GetProcessFlags(0);
2179 case GPD_USERDATA:
2180 return process_dword;
2181 default:
2182 ERR("Unknown offset %d\n", offset );
2183 return 0;
2187 /***********************************************************************
2188 * SetProcessDword (KERNEL.484)
2189 * 'Of course you cannot directly access Windows internal structures'
2191 void WINAPI SetProcessDword( DWORD dwProcessID, INT offset, DWORD value )
2193 TRACE("(%ld, %d)\n", dwProcessID, offset );
2195 if (dwProcessID && dwProcessID != GetCurrentProcessId())
2197 ERR("%d: process %lx not accessible\n", offset, dwProcessID);
2198 return;
2201 switch ( offset )
2203 case GPD_APP_COMPAT_FLAGS:
2204 case GPD_LOAD_DONE_EVENT:
2205 case GPD_HINSTANCE16:
2206 case GPD_WINDOWS_VERSION:
2207 case GPD_THDB:
2208 case GPD_PDB:
2209 case GPD_STARTF_SHELLDATA:
2210 case GPD_STARTF_HOTKEY:
2211 case GPD_STARTF_SHOWWINDOW:
2212 case GPD_STARTF_SIZE:
2213 case GPD_STARTF_POSITION:
2214 case GPD_STARTF_FLAGS:
2215 case GPD_PARENT:
2216 case GPD_FLAGS:
2217 ERR("Not allowed to modify offset %d\n", offset );
2218 break;
2219 case GPD_USERDATA:
2220 process_dword = value;
2221 break;
2222 default:
2223 ERR("Unknown offset %d\n", offset );
2224 break;
2229 /***********************************************************************
2230 * ExitProcess (KERNEL.466)
2232 void WINAPI ExitProcess16( WORD status )
2234 DWORD count;
2235 ReleaseThunkLock( &count );
2236 ExitProcess( status );
2240 /*********************************************************************
2241 * OpenProcess (KERNEL32.@)
2243 HANDLE WINAPI OpenProcess( DWORD access, BOOL inherit, DWORD id )
2245 NTSTATUS status;
2246 HANDLE handle;
2247 OBJECT_ATTRIBUTES attr;
2248 CLIENT_ID cid;
2250 cid.UniqueProcess = (HANDLE)id;
2251 cid.UniqueThread = 0; /* FIXME ? */
2253 attr.Length = sizeof(OBJECT_ATTRIBUTES);
2254 attr.RootDirectory = NULL;
2255 attr.Attributes = inherit ? OBJ_INHERIT : 0;
2256 attr.SecurityDescriptor = NULL;
2257 attr.SecurityQualityOfService = NULL;
2258 attr.ObjectName = NULL;
2260 status = NtOpenProcess(&handle, access, &attr, &cid);
2261 if (status != STATUS_SUCCESS)
2263 SetLastError( RtlNtStatusToDosError(status) );
2264 return NULL;
2266 return handle;
2270 /*********************************************************************
2271 * MapProcessHandle (KERNEL.483)
2272 * GetProcessId (KERNEL32.@)
2274 DWORD WINAPI GetProcessId( HANDLE hProcess )
2276 NTSTATUS status;
2277 PROCESS_BASIC_INFORMATION pbi;
2279 status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi,
2280 sizeof(pbi), NULL);
2281 if (status == STATUS_SUCCESS) return pbi.UniqueProcessId;
2282 SetLastError( RtlNtStatusToDosError(status) );
2283 return 0;
2287 /*********************************************************************
2288 * CloseW32Handle (KERNEL.474)
2289 * CloseHandle (KERNEL32.@)
2291 BOOL WINAPI CloseHandle( HANDLE handle )
2293 NTSTATUS status;
2295 /* stdio handles need special treatment */
2296 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
2297 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
2298 (handle == (HANDLE)STD_ERROR_HANDLE))
2299 handle = GetStdHandle( (DWORD)handle );
2301 if (is_console_handle(handle))
2302 return CloseConsoleHandle(handle);
2304 status = NtClose( handle );
2305 if (status) SetLastError( RtlNtStatusToDosError(status) );
2306 return !status;
2310 /*********************************************************************
2311 * GetHandleInformation (KERNEL32.@)
2313 BOOL WINAPI GetHandleInformation( HANDLE handle, LPDWORD flags )
2315 OBJECT_DATA_INFORMATION info;
2316 NTSTATUS status = NtQueryObject( handle, ObjectDataInformation, &info, sizeof(info), NULL );
2318 if (status) SetLastError( RtlNtStatusToDosError(status) );
2319 else if (flags)
2321 *flags = 0;
2322 if (info.InheritHandle) *flags |= HANDLE_FLAG_INHERIT;
2323 if (info.ProtectFromClose) *flags |= HANDLE_FLAG_PROTECT_FROM_CLOSE;
2325 return !status;
2329 /*********************************************************************
2330 * SetHandleInformation (KERNEL32.@)
2332 BOOL WINAPI SetHandleInformation( HANDLE handle, DWORD mask, DWORD flags )
2334 OBJECT_DATA_INFORMATION info;
2335 NTSTATUS status;
2337 /* if not setting both fields, retrieve current value first */
2338 if ((mask & (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE)) !=
2339 (HANDLE_FLAG_INHERIT | HANDLE_FLAG_PROTECT_FROM_CLOSE))
2341 if ((status = NtQueryObject( handle, ObjectDataInformation, &info, sizeof(info), NULL )))
2343 SetLastError( RtlNtStatusToDosError(status) );
2344 return FALSE;
2347 if (mask & HANDLE_FLAG_INHERIT)
2348 info.InheritHandle = (flags & HANDLE_FLAG_INHERIT) != 0;
2349 if (mask & HANDLE_FLAG_PROTECT_FROM_CLOSE)
2350 info.ProtectFromClose = (flags & HANDLE_FLAG_PROTECT_FROM_CLOSE) != 0;
2352 status = NtSetInformationObject( handle, ObjectDataInformation, &info, sizeof(info) );
2353 if (status) SetLastError( RtlNtStatusToDosError(status) );
2354 return !status;
2358 /*********************************************************************
2359 * DuplicateHandle (KERNEL32.@)
2361 BOOL WINAPI DuplicateHandle( HANDLE source_process, HANDLE source,
2362 HANDLE dest_process, HANDLE *dest,
2363 DWORD access, BOOL inherit, DWORD options )
2365 NTSTATUS status;
2367 if (is_console_handle(source))
2369 /* FIXME: this test is not sufficient, we need to test process ids, not handles */
2370 if (source_process != dest_process ||
2371 source_process != GetCurrentProcess())
2373 SetLastError(ERROR_INVALID_PARAMETER);
2374 return FALSE;
2376 *dest = DuplicateConsoleHandle( source, access, inherit, options );
2377 return (*dest != INVALID_HANDLE_VALUE);
2379 status = NtDuplicateObject( source_process, source, dest_process, dest,
2380 access, inherit ? OBJ_INHERIT : 0, options );
2381 if (status) SetLastError( RtlNtStatusToDosError(status) );
2382 return !status;
2386 /***********************************************************************
2387 * ConvertToGlobalHandle (KERNEL.476)
2388 * ConvertToGlobalHandle (KERNEL32.@)
2390 HANDLE WINAPI ConvertToGlobalHandle(HANDLE hSrc)
2392 HANDLE ret = INVALID_HANDLE_VALUE;
2393 DuplicateHandle( GetCurrentProcess(), hSrc, GetCurrentProcess(), &ret, 0, FALSE,
2394 DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
2395 return ret;
2399 /***********************************************************************
2400 * SetHandleContext (KERNEL32.@)
2402 BOOL WINAPI SetHandleContext(HANDLE hnd,DWORD context)
2404 FIXME("(%p,%ld), stub. In case this got called by WSOCK32/WS2_32: "
2405 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd,context);
2406 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2407 return FALSE;
2411 /***********************************************************************
2412 * GetHandleContext (KERNEL32.@)
2414 DWORD WINAPI GetHandleContext(HANDLE hnd)
2416 FIXME("(%p), stub. In case this got called by WSOCK32/WS2_32: "
2417 "the external WINSOCK DLLs won't work with WINE, don't use them.\n",hnd);
2418 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2419 return 0;
2423 /***********************************************************************
2424 * CreateSocketHandle (KERNEL32.@)
2426 HANDLE WINAPI CreateSocketHandle(void)
2428 FIXME("(), stub. In case this got called by WSOCK32/WS2_32: "
2429 "the external WINSOCK DLLs won't work with WINE, don't use them.\n");
2430 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2431 return INVALID_HANDLE_VALUE;
2435 /***********************************************************************
2436 * SetPriorityClass (KERNEL32.@)
2438 BOOL WINAPI SetPriorityClass( HANDLE hprocess, DWORD priorityclass )
2440 NTSTATUS status;
2441 PROCESS_PRIORITY_CLASS ppc;
2443 ppc.Foreground = FALSE;
2444 switch (priorityclass)
2446 case IDLE_PRIORITY_CLASS:
2447 ppc.PriorityClass = PROCESS_PRIOCLASS_IDLE; break;
2448 case BELOW_NORMAL_PRIORITY_CLASS:
2449 ppc.PriorityClass = PROCESS_PRIOCLASS_BELOW_NORMAL; break;
2450 case NORMAL_PRIORITY_CLASS:
2451 ppc.PriorityClass = PROCESS_PRIOCLASS_NORMAL; break;
2452 case ABOVE_NORMAL_PRIORITY_CLASS:
2453 ppc.PriorityClass = PROCESS_PRIOCLASS_ABOVE_NORMAL; break;
2454 case HIGH_PRIORITY_CLASS:
2455 ppc.PriorityClass = PROCESS_PRIOCLASS_HIGH; break;
2456 case REALTIME_PRIORITY_CLASS:
2457 ppc.PriorityClass = PROCESS_PRIOCLASS_REALTIME; break;
2458 default:
2459 SetLastError(ERROR_INVALID_PARAMETER);
2460 return FALSE;
2463 status = NtSetInformationProcess(hprocess, ProcessPriorityClass,
2464 &ppc, sizeof(ppc));
2466 if (status != STATUS_SUCCESS)
2468 SetLastError( RtlNtStatusToDosError(status) );
2469 return FALSE;
2471 return TRUE;
2475 /***********************************************************************
2476 * GetPriorityClass (KERNEL32.@)
2478 DWORD WINAPI GetPriorityClass(HANDLE hProcess)
2480 NTSTATUS status;
2481 PROCESS_BASIC_INFORMATION pbi;
2483 status = NtQueryInformationProcess(hProcess, ProcessBasicInformation, &pbi,
2484 sizeof(pbi), NULL);
2485 if (status != STATUS_SUCCESS)
2487 SetLastError( RtlNtStatusToDosError(status) );
2488 return 0;
2490 switch (pbi.BasePriority)
2492 case PROCESS_PRIOCLASS_IDLE: return IDLE_PRIORITY_CLASS;
2493 case PROCESS_PRIOCLASS_BELOW_NORMAL: return BELOW_NORMAL_PRIORITY_CLASS;
2494 case PROCESS_PRIOCLASS_NORMAL: return NORMAL_PRIORITY_CLASS;
2495 case PROCESS_PRIOCLASS_ABOVE_NORMAL: return ABOVE_NORMAL_PRIORITY_CLASS;
2496 case PROCESS_PRIOCLASS_HIGH: return HIGH_PRIORITY_CLASS;
2497 case PROCESS_PRIOCLASS_REALTIME: return REALTIME_PRIORITY_CLASS;
2499 SetLastError( ERROR_INVALID_PARAMETER );
2500 return 0;
2504 /***********************************************************************
2505 * SetProcessAffinityMask (KERNEL32.@)
2507 BOOL WINAPI SetProcessAffinityMask( HANDLE hProcess, DWORD_PTR affmask )
2509 NTSTATUS status;
2511 status = NtSetInformationProcess(hProcess, ProcessAffinityMask,
2512 &affmask, sizeof(DWORD_PTR));
2513 if (!status)
2515 SetLastError( RtlNtStatusToDosError(status) );
2516 return FALSE;
2518 return TRUE;
2522 /**********************************************************************
2523 * GetProcessAffinityMask (KERNEL32.@)
2525 BOOL WINAPI GetProcessAffinityMask( HANDLE hProcess,
2526 PDWORD_PTR lpProcessAffinityMask,
2527 PDWORD_PTR lpSystemAffinityMask )
2529 PROCESS_BASIC_INFORMATION pbi;
2530 NTSTATUS status;
2532 status = NtQueryInformationProcess(hProcess,
2533 ProcessBasicInformation,
2534 &pbi, sizeof(pbi), NULL);
2535 if (status)
2537 SetLastError( RtlNtStatusToDosError(status) );
2538 return FALSE;
2540 if (lpProcessAffinityMask) *lpProcessAffinityMask = pbi.AffinityMask;
2541 /* FIXME */
2542 if (lpSystemAffinityMask) *lpSystemAffinityMask = 1;
2543 return TRUE;
2547 /***********************************************************************
2548 * GetProcessVersion (KERNEL32.@)
2550 DWORD WINAPI GetProcessVersion( DWORD processid )
2552 IMAGE_NT_HEADERS *nt;
2554 if (processid && processid != GetCurrentProcessId())
2556 FIXME("should use ReadProcessMemory\n");
2557 return 0;
2559 if ((nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress )))
2560 return ((nt->OptionalHeader.MajorSubsystemVersion << 16) |
2561 nt->OptionalHeader.MinorSubsystemVersion);
2562 return 0;
2566 /***********************************************************************
2567 * SetProcessWorkingSetSize [KERNEL32.@]
2568 * Sets the min/max working set sizes for a specified process.
2570 * PARAMS
2571 * hProcess [I] Handle to the process of interest
2572 * minset [I] Specifies minimum working set size
2573 * maxset [I] Specifies maximum working set size
2575 * RETURNS
2576 * Success: TRUE
2577 * Failure: FALSE
2579 BOOL WINAPI SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T minset,
2580 SIZE_T maxset)
2582 FIXME("(%p,%ld,%ld): stub - harmless\n",hProcess,minset,maxset);
2583 if(( minset == (SIZE_T)-1) && (maxset == (SIZE_T)-1)) {
2584 /* Trim the working set to zero */
2585 /* Swap the process out of physical RAM */
2587 return TRUE;
2590 /***********************************************************************
2591 * GetProcessWorkingSetSize (KERNEL32.@)
2593 BOOL WINAPI GetProcessWorkingSetSize(HANDLE hProcess, PSIZE_T minset,
2594 PSIZE_T maxset)
2596 FIXME("(%p,%p,%p): stub\n",hProcess,minset,maxset);
2597 /* 32 MB working set size */
2598 if (minset) *minset = 32*1024*1024;
2599 if (maxset) *maxset = 32*1024*1024;
2600 return TRUE;
2604 /***********************************************************************
2605 * SetProcessShutdownParameters (KERNEL32.@)
2607 BOOL WINAPI SetProcessShutdownParameters(DWORD level, DWORD flags)
2609 FIXME("(%08lx, %08lx): partial stub.\n", level, flags);
2610 shutdown_flags = flags;
2611 shutdown_priority = level;
2612 return TRUE;
2616 /***********************************************************************
2617 * GetProcessShutdownParameters (KERNEL32.@)
2620 BOOL WINAPI GetProcessShutdownParameters( LPDWORD lpdwLevel, LPDWORD lpdwFlags )
2622 *lpdwLevel = shutdown_priority;
2623 *lpdwFlags = shutdown_flags;
2624 return TRUE;
2628 /***********************************************************************
2629 * GetProcessPriorityBoost (KERNEL32.@)
2631 BOOL WINAPI GetProcessPriorityBoost(HANDLE hprocess,PBOOL pDisablePriorityBoost)
2633 FIXME("(%p,%p): semi-stub\n", hprocess, pDisablePriorityBoost);
2635 /* Report that no boost is present.. */
2636 *pDisablePriorityBoost = FALSE;
2638 return TRUE;
2641 /***********************************************************************
2642 * SetProcessPriorityBoost (KERNEL32.@)
2644 BOOL WINAPI SetProcessPriorityBoost(HANDLE hprocess,BOOL disableboost)
2646 FIXME("(%p,%d): stub\n",hprocess,disableboost);
2647 /* Say we can do it. I doubt the program will notice that we don't. */
2648 return TRUE;
2652 /***********************************************************************
2653 * ReadProcessMemory (KERNEL32.@)
2655 BOOL WINAPI ReadProcessMemory( HANDLE process, LPCVOID addr, LPVOID buffer, SIZE_T size,
2656 SIZE_T *bytes_read )
2658 NTSTATUS status = NtReadVirtualMemory( process, addr, buffer, size, bytes_read );
2659 if (status) SetLastError( RtlNtStatusToDosError(status) );
2660 return !status;
2664 /***********************************************************************
2665 * WriteProcessMemory (KERNEL32.@)
2667 BOOL WINAPI WriteProcessMemory( HANDLE process, LPVOID addr, LPCVOID buffer, SIZE_T size,
2668 SIZE_T *bytes_written )
2670 NTSTATUS status = NtWriteVirtualMemory( process, addr, buffer, size, bytes_written );
2671 if (status) SetLastError( RtlNtStatusToDosError(status) );
2672 return !status;
2676 /****************************************************************************
2677 * FlushInstructionCache (KERNEL32.@)
2679 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
2681 NTSTATUS status;
2682 if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
2683 status = NtFlushInstructionCache( hProcess, lpBaseAddress, dwSize );
2684 if (status) SetLastError( RtlNtStatusToDosError(status) );
2685 return !status;
2689 /******************************************************************
2690 * GetProcessIoCounters (KERNEL32.@)
2692 BOOL WINAPI GetProcessIoCounters(HANDLE hProcess, PIO_COUNTERS ioc)
2694 NTSTATUS status;
2696 status = NtQueryInformationProcess(hProcess, ProcessIoCounters,
2697 ioc, sizeof(*ioc), NULL);
2698 if (status) SetLastError( RtlNtStatusToDosError(status) );
2699 return !status;
2702 /***********************************************************************
2703 * ProcessIdToSessionId (KERNEL32.@)
2704 * This function is available on Terminal Server 4SP4 and Windows 2000
2706 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
2708 /* According to MSDN, if the calling process is not in a terminal
2709 * services environment, then the sessionid returned is zero.
2711 *sessionid_ptr = 0;
2712 return TRUE;
2716 /***********************************************************************
2717 * RegisterServiceProcess (KERNEL.491)
2718 * RegisterServiceProcess (KERNEL32.@)
2720 * A service process calls this function to ensure that it continues to run
2721 * even after a user logged off.
2723 DWORD WINAPI RegisterServiceProcess(DWORD dwProcessId, DWORD dwType)
2725 /* I don't think that Wine needs to do anything in this function */
2726 return 1; /* success */
2730 /***********************************************************************
2731 * GetCurrentProcess (KERNEL32.@)
2733 * Get a handle to the current process.
2735 * PARAMS
2736 * None.
2738 * RETURNS
2739 * A handle representing the current process.
2741 #undef GetCurrentProcess
2742 HANDLE WINAPI GetCurrentProcess(void)
2744 return (HANDLE)0xffffffff;
2747 /***********************************************************************
2748 * CmdBatNotification (KERNEL32.@)
2750 * Called by cmd.exe with
2751 * (1) when a batch file is started
2752 * (0) when a batch file finishes executing
2754 BOOL WINAPI CmdBatNotification( BOOL bBatchRunning )
2756 FIXME("%d\n", bBatchRunning);
2757 return FALSE;