2 * Process environment management
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
22 #include "wine/port.h"
33 #include "wine/server.h"
34 #include "wine/library.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(environ);
42 * - contrary to Microsoft docs, the environment strings do not appear
43 * to be sorted on Win95 (although they are on NT); so we don't bother
44 * to sort them either.
47 static STARTUPINFOW startup_infoW;
48 static STARTUPINFOA startup_infoA;
51 /***********************************************************************
52 * GetCommandLineA (KERNEL32.@)
54 * WARNING: there's a Windows incompatibility lurking here !
55 * Win32s always includes the full path of the program file,
56 * whereas Windows NT only returns the full file path plus arguments
57 * in case the program has been started with a full path.
58 * Win9x seems to have inherited NT behaviour.
60 * Note that both Start Menu Execute and Explorer start programs with
61 * fully specified quoted app file paths, which is why probably the only case
62 * where you'll see single file names is in case of direct launch
63 * via CreateProcess or WinExec.
65 * Perhaps we should take care of Win3.1 programs here (Win32s "feature").
67 * References: MS KB article q102762.txt (special Win32s handling)
69 LPSTR WINAPI GetCommandLineA(void)
71 static char *cmdlineA; /* ASCII command line */
73 if (!cmdlineA) /* make an ansi version if we don't have it */
78 cmdlineA = (RtlUnicodeStringToAnsiString( &ansi, &NtCurrentTeb()->Peb->ProcessParameters->CommandLine, TRUE) == STATUS_SUCCESS) ?
85 /***********************************************************************
86 * GetCommandLineW (KERNEL32.@)
88 LPWSTR WINAPI GetCommandLineW(void)
90 return NtCurrentTeb()->Peb->ProcessParameters->CommandLine.Buffer;
94 /***********************************************************************
95 * GetEnvironmentStrings (KERNEL32.@)
96 * GetEnvironmentStringsA (KERNEL32.@)
98 LPSTR WINAPI GetEnvironmentStringsA(void)
108 ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
111 slen = strlenW(ptrW) + 1;
112 len += WideCharToMultiByte( CP_ACP, 0, ptrW, slen, NULL, 0, NULL, NULL );
116 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )) != NULL)
118 ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment;
122 slen = strlenW(ptrW) + 1;
123 WideCharToMultiByte( CP_ACP, 0, ptrW, slen, ptrA, len, NULL, NULL );
125 ptrA += strlen(ptrA) + 1;
135 /***********************************************************************
136 * GetEnvironmentStringsW (KERNEL32.@)
138 LPWSTR WINAPI GetEnvironmentStringsW(void)
140 return NtCurrentTeb()->Peb->ProcessParameters->Environment;
144 /***********************************************************************
145 * FreeEnvironmentStringsA (KERNEL32.@)
147 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
149 return HeapFree( GetProcessHeap(), 0, ptr );
153 /***********************************************************************
154 * FreeEnvironmentStringsW (KERNEL32.@)
156 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
162 /***********************************************************************
163 * GetEnvironmentVariableA (KERNEL32.@)
165 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
167 UNICODE_STRING us_name;
173 SetLastError(ERROR_ENVVAR_NOT_FOUND);
177 if (!(valueW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR))))
180 RtlCreateUnicodeStringFromAsciiz( &us_name, name );
182 ret = GetEnvironmentVariableW( us_name.Buffer, valueW, size);
183 if (ret && ret < size)
185 WideCharToMultiByte( CP_ACP, 0, valueW, ret + 1, value, size, NULL, NULL );
187 /* this is needed to tell, with 0 as a return value, the difference between:
188 * - an error (GetLastError() != 0)
189 * - returning an empty string (in this case, we need to update the buffer)
191 if (ret == 0 && size && GetLastError() == 0)
194 RtlFreeUnicodeString( &us_name );
195 HeapFree(GetProcessHeap(), 0, valueW);
201 /***********************************************************************
202 * GetEnvironmentVariableW (KERNEL32.@)
204 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR name, LPWSTR val, DWORD size )
206 UNICODE_STRING us_name;
207 UNICODE_STRING us_value;
211 TRACE("(%s %p %lu)\n", debugstr_w(name), val, size);
215 SetLastError(ERROR_ENVVAR_NOT_FOUND);
219 RtlInitUnicodeString(&us_name, name);
221 us_value.MaximumLength = (size ? size - 1 : 0) * sizeof(WCHAR);
222 us_value.Buffer = val;
224 status = RtlQueryEnvironmentVariable_U(NULL, &us_name, &us_value);
225 len = us_value.Length / sizeof(WCHAR);
226 if (status != STATUS_SUCCESS)
228 SetLastError( RtlNtStatusToDosError(status) );
229 return (status == STATUS_BUFFER_TOO_SMALL) ? len + 1 : 0;
231 if (size) val[len] = '\0';
233 return us_value.Length / sizeof(WCHAR);
237 /***********************************************************************
238 * SetEnvironmentVariableA (KERNEL32.@)
240 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
242 UNICODE_STRING us_name;
247 SetLastError(ERROR_ENVVAR_NOT_FOUND);
251 RtlCreateUnicodeStringFromAsciiz( &us_name, name );
254 UNICODE_STRING us_value;
256 RtlCreateUnicodeStringFromAsciiz( &us_value, value );
257 ret = SetEnvironmentVariableW( us_name.Buffer, us_value.Buffer );
258 RtlFreeUnicodeString( &us_value );
260 else ret = SetEnvironmentVariableW( us_name.Buffer, NULL );
262 RtlFreeUnicodeString( &us_name );
268 /***********************************************************************
269 * SetEnvironmentVariableW (KERNEL32.@)
271 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
273 UNICODE_STRING us_name;
276 TRACE("(%s %s)\n", debugstr_w(name), debugstr_w(value));
280 SetLastError(ERROR_ENVVAR_NOT_FOUND);
284 RtlInitUnicodeString(&us_name, name);
287 UNICODE_STRING us_value;
289 RtlInitUnicodeString(&us_value, value);
290 status = RtlSetEnvironmentVariable(NULL, &us_name, &us_value);
292 else status = RtlSetEnvironmentVariable(NULL, &us_name, NULL);
294 if (status != STATUS_SUCCESS)
296 SetLastError( RtlNtStatusToDosError(status) );
303 /***********************************************************************
304 * ExpandEnvironmentStringsA (KERNEL32.@)
306 * Note: overlapping buffers are not supported; this is how it should be.
307 * FIXME: return value is wrong for MBCS
309 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
311 UNICODE_STRING us_src;
315 RtlCreateUnicodeStringFromAsciiz( &us_src, src );
318 if (!(dstW = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR))))
320 ret = ExpandEnvironmentStringsW( us_src.Buffer, dstW, count);
322 WideCharToMultiByte( CP_ACP, 0, dstW, ret, dst, count, NULL, NULL );
324 else ret = ExpandEnvironmentStringsW( us_src.Buffer, NULL, 0);
326 RtlFreeUnicodeString( &us_src );
327 HeapFree(GetProcessHeap(), 0, dstW);
333 /***********************************************************************
334 * ExpandEnvironmentStringsW (KERNEL32.@)
336 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
338 UNICODE_STRING us_src;
339 UNICODE_STRING us_dst;
343 TRACE("(%s %p %lu)\n", debugstr_w(src), dst, len);
345 RtlInitUnicodeString(&us_src, src);
347 us_dst.MaximumLength = len * sizeof(WCHAR);
351 status = RtlExpandEnvironmentStrings_U(NULL, &us_src, &us_dst, &res);
352 res /= sizeof(WCHAR);
353 if (status != STATUS_SUCCESS)
355 SetLastError( RtlNtStatusToDosError(status) );
356 if (status != STATUS_BUFFER_TOO_SMALL) return 0;
357 if (len && dst) dst[len - 1] = '\0';
364 /***********************************************************************
365 * GetStdHandle (KERNEL32.@)
367 HANDLE WINAPI GetStdHandle( DWORD std_handle )
371 case STD_INPUT_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdInput;
372 case STD_OUTPUT_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdOutput;
373 case STD_ERROR_HANDLE: return NtCurrentTeb()->Peb->ProcessParameters->hStdError;
375 SetLastError( ERROR_INVALID_PARAMETER );
376 return INVALID_HANDLE_VALUE;
380 /***********************************************************************
381 * SetStdHandle (KERNEL32.@)
383 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
387 case STD_INPUT_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdInput = handle; return TRUE;
388 case STD_OUTPUT_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdOutput = handle; return TRUE;
389 case STD_ERROR_HANDLE: NtCurrentTeb()->Peb->ProcessParameters->hStdError = handle; return TRUE;
391 SetLastError( ERROR_INVALID_PARAMETER );
395 /***********************************************************************
396 * GetStartupInfoA (KERNEL32.@)
398 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
400 assert(startup_infoA.cb);
401 memcpy(info, &startup_infoA, sizeof(startup_infoA));
405 /***********************************************************************
406 * GetStartupInfoW (KERNEL32.@)
408 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
410 assert(startup_infoW.cb);
411 memcpy(info, &startup_infoW, sizeof(startup_infoW));
414 /******************************************************************
415 * ENV_CopyStartupInformation (internal)
417 * Creates the STARTUPINFO information from the ntdll information
419 void ENV_CopyStartupInformation(void)
421 RTL_USER_PROCESS_PARAMETERS* rupp;
426 rupp = NtCurrentTeb()->Peb->ProcessParameters;
428 startup_infoW.cb = sizeof(startup_infoW);
429 startup_infoW.lpReserved = NULL;
430 startup_infoW.lpDesktop = rupp->Desktop.Buffer;
431 startup_infoW.lpTitle = rupp->WindowTitle.Buffer;
432 startup_infoW.dwX = rupp->dwX;
433 startup_infoW.dwY = rupp->dwY;
434 startup_infoW.dwXSize = rupp->dwXSize;
435 startup_infoW.dwYSize = rupp->dwYSize;
436 startup_infoW.dwXCountChars = rupp->dwXCountChars;
437 startup_infoW.dwYCountChars = rupp->dwYCountChars;
438 startup_infoW.dwFillAttribute = rupp->dwFillAttribute;
439 startup_infoW.dwFlags = rupp->dwFlags;
440 startup_infoW.wShowWindow = rupp->wShowWindow;
441 startup_infoW.cbReserved2 = rupp->RuntimeInfo.MaximumLength;
442 startup_infoW.lpReserved2 = rupp->RuntimeInfo.MaximumLength ? (void*)rupp->RuntimeInfo.Buffer : NULL;
443 startup_infoW.hStdInput = rupp->hStdInput;
444 startup_infoW.hStdOutput = rupp->hStdOutput;
445 startup_infoW.hStdError = rupp->hStdError;
447 startup_infoA.cb = sizeof(startup_infoA);
448 startup_infoA.lpReserved = NULL;
449 startup_infoA.lpDesktop = (rupp->Desktop.Length &&
450 RtlUnicodeStringToAnsiString( &ansi, &rupp->Desktop, TRUE) == STATUS_SUCCESS) ?
452 startup_infoA.lpTitle = (rupp->WindowTitle.Length &&
453 RtlUnicodeStringToAnsiString( &ansi, &rupp->WindowTitle, TRUE) == STATUS_SUCCESS) ?
455 startup_infoA.dwX = rupp->dwX;
456 startup_infoA.dwY = rupp->dwY;
457 startup_infoA.dwXSize = rupp->dwXSize;
458 startup_infoA.dwYSize = rupp->dwYSize;
459 startup_infoA.dwXCountChars = rupp->dwXCountChars;
460 startup_infoA.dwYCountChars = rupp->dwYCountChars;
461 startup_infoA.dwFillAttribute = rupp->dwFillAttribute;
462 startup_infoA.dwFlags = rupp->dwFlags;
463 startup_infoA.wShowWindow = rupp->wShowWindow;
464 startup_infoA.cbReserved2 = rupp->RuntimeInfo.MaximumLength;
465 startup_infoA.lpReserved2 = rupp->RuntimeInfo.MaximumLength ? (void*)rupp->RuntimeInfo.Buffer : NULL;
466 startup_infoA.hStdInput = rupp->hStdInput;
467 startup_infoA.hStdOutput = rupp->hStdOutput;
468 startup_infoA.hStdError = rupp->hStdError;