winmm: Rearrange device mapping when a new default device is chosen.
[wine] / dlls / userenv / userenv_main.c
1 /*
2  * Implementation of userenv.dll
3  *
4  * Copyright 2006 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winternl.h"
29 #include "winnls.h"
30 #include "sddl.h"
31 #include "objbase.h"
32 #include "userenv.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL( userenv );
38
39 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
40 {
41     TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
42
43     switch (fdwReason)
44     {
45     case DLL_WINE_PREATTACH:
46         return FALSE;  /* prefer native version */
47     case DLL_PROCESS_ATTACH:
48         DisableThreadLibraryCalls(hinstDLL);
49         break;
50     case DLL_PROCESS_DETACH:
51         break;
52     }
53     return TRUE;
54 }
55
56 static BOOL get_reg_value(WCHAR *env, HKEY hkey, const WCHAR *name, WCHAR *val, DWORD size)
57 {
58     DWORD type, res_size=0;
59
60     if (RegQueryValueExW(hkey, name, 0, &type, NULL, &res_size) != ERROR_SUCCESS)
61         return FALSE;
62
63     if (type == REG_SZ)
64     {
65         if (res_size > size)
66             return FALSE;
67
68         return RegQueryValueExW(hkey, name, 0, NULL, (BYTE*)val, &size) == ERROR_SUCCESS;
69     }
70     else if (type == REG_EXPAND_SZ)
71     {
72         UNICODE_STRING us_buf, us_expanded;
73         WCHAR *buf = HeapAlloc(GetProcessHeap(), 0, res_size);
74         if (!buf)
75             return FALSE;
76
77         if (RegQueryValueExW(hkey, name, 0, NULL, (BYTE*)buf, &res_size) != ERROR_SUCCESS)
78         {
79             HeapFree(GetProcessHeap(), 0, buf);
80             return FALSE;
81         }
82
83         RtlInitUnicodeString(&us_buf, buf);
84         us_expanded.Buffer = val;
85         us_expanded.MaximumLength = size;
86         if (RtlExpandEnvironmentStrings_U(env, &us_buf, &us_expanded, &size) != STATUS_SUCCESS)
87         {
88             HeapFree(GetProcessHeap(), 0, buf);
89             return FALSE;
90         }
91
92         HeapFree(GetProcessHeap(), 0, buf);
93         return TRUE;
94     }
95
96     return FALSE;
97 }
98
99 static void set_registry_variables(WCHAR **env, HKEY hkey, DWORD type, BOOL set_path)
100 {
101     static const WCHAR SystemRootW[] = {'S','y','s','t','e','m','R','o','o','t',0};
102     static const WCHAR SystemDriveW[] = {'S','y','s','t','e','m','D','r','i','v','e',0};
103     static const WCHAR PATHW[] = {'P','A','T','H'};
104
105     UNICODE_STRING us_name, us_value;
106     WCHAR name[1024], value[1024];
107     DWORD ret, index, size;
108
109     for (index = 0; ; index++)
110     {
111         size = sizeof(name)/sizeof(WCHAR);
112         ret = RegEnumValueW(hkey, index, name, &size, NULL, NULL, NULL, NULL);
113         if (ret != ERROR_SUCCESS)
114             break;
115
116         if (!memicmpW(name, SystemRootW, sizeof(SystemRootW)/sizeof(WCHAR)))
117             continue;
118         if (!memicmpW(name, SystemDriveW, sizeof(SystemDriveW)/sizeof(WCHAR)))
119             continue;
120
121         RtlInitUnicodeString(&us_name, name);
122         us_value.Buffer = value;
123         us_value.MaximumLength = sizeof(value);
124         if (!memicmpW(name, PATHW, sizeof(PATHW)/sizeof(WCHAR)) &&
125                 !RtlQueryEnvironmentVariable_U(*env, &us_name, &us_value))
126         {
127             if (!set_path)
128                 continue;
129
130             size = strlenW(value)+1;
131             if (!get_reg_value(*env, hkey, name, value+size,
132                         sizeof(value)-size*sizeof(WCHAR)))
133                 continue;
134
135             value[size] = ';';
136             RtlInitUnicodeString(&us_value, value);
137             RtlSetEnvironmentVariable(env, &us_name, &us_value);
138             continue;
139         }
140
141         if (!get_reg_value(*env, hkey, name, value, sizeof(value)))
142             continue;
143
144         if(!value[0])
145             continue;
146
147         RtlInitUnicodeString(&us_value, value);
148         RtlSetEnvironmentVariable(env, &us_name, &us_value);
149     }
150 }
151
152 static void set_wow64_environment(WCHAR **env)
153 {
154     static const WCHAR versionW[] = {'S','o','f','t','w','a','r','e','\\',
155         'M','i','c','r','o','s','o','f','t','\\',
156         'W','i','n','d','o','w','s','\\',
157         'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0};
158     static const WCHAR progdirW[]   = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',0};
159     static const WCHAR progdir86W[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r',' ','(','x','8','6',')',0};
160     static const WCHAR progfilesW[] = {'P','r','o','g','r','a','m','F','i','l','e','s',0};
161     static const WCHAR progw6432W[] = {'P','r','o','g','r','a','m','W','6','4','3','2',0};
162     static const WCHAR commondirW[]   = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',0};
163     static const WCHAR commondir86W[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r',' ','(','x','8','6',')',0};
164     static const WCHAR commonfilesW[] = {'C','o','m','m','o','n','P','r','o','g','r','a','m','F','i','l','e','s',0};
165     static const WCHAR commonw6432W[] = {'C','o','m','m','o','n','P','r','o','g','r','a','m','W','6','4','3','2',0};
166
167     UNICODE_STRING nameW, valueW;
168     WCHAR buf[64];
169     HKEY hkey;
170     BOOL is_win64 = (sizeof(void *) > sizeof(int));
171     BOOL is_wow64;
172
173     IsWow64Process( GetCurrentProcess(), &is_wow64 );
174
175     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, versionW, 0,
176                 KEY_READ|KEY_WOW64_64KEY, &hkey))
177         return;
178
179     /* set the ProgramFiles variables */
180
181     if (get_reg_value(*env, hkey, progdirW, buf, sizeof(buf)))
182     {
183         if (is_win64 || is_wow64)
184         {
185             RtlInitUnicodeString(&nameW, progw6432W);
186             RtlInitUnicodeString(&valueW, buf);
187             RtlSetEnvironmentVariable(env, &nameW, &valueW);
188         }
189         if (is_win64 || !is_wow64)
190         {
191             RtlInitUnicodeString(&nameW, progfilesW);
192             RtlInitUnicodeString(&valueW, buf);
193             RtlSetEnvironmentVariable(env, &nameW, &valueW);
194         }
195     }
196     if (is_wow64 && get_reg_value(*env, hkey, progdir86W, buf, sizeof(buf)))
197     {
198         RtlInitUnicodeString(&nameW, progfilesW);
199         RtlInitUnicodeString(&valueW, buf);
200         RtlSetEnvironmentVariable(env, &nameW, &valueW);
201     }
202
203     /* set the CommonProgramFiles variables */
204
205     if (get_reg_value(*env, hkey, commondirW, buf, sizeof(buf)))
206     {
207         if (is_win64 || is_wow64)
208         {
209             RtlInitUnicodeString(&nameW, commonw6432W);
210             RtlInitUnicodeString(&valueW, buf);
211             RtlSetEnvironmentVariable(env, &nameW, &valueW);
212         }
213         if (is_win64 || !is_wow64)
214         {
215             RtlInitUnicodeString(&nameW, commonfilesW);
216             RtlInitUnicodeString(&valueW, buf);
217             RtlSetEnvironmentVariable(env, &nameW, &valueW);
218         }
219     }
220     if (is_wow64 && get_reg_value(*env, hkey, commondir86W, buf, sizeof(buf)))
221     {
222         RtlInitUnicodeString(&nameW, commonfilesW);
223         RtlInitUnicodeString(&valueW, buf);
224         RtlSetEnvironmentVariable(env, &nameW, &valueW);
225     }
226
227     RegCloseKey(hkey);
228 }
229
230 BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
231                      HANDLE hToken, BOOL bInherit )
232 {
233     static const WCHAR env_keyW[] = {'S','y','s','t','e','m','\\',
234         'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
235         'C','o','n','t','r','o','l','\\',
236         'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
237         'E','n','v','i','r','o','n','m','e','n','t',0};
238     static const WCHAR profile_keyW[] = {'S','o','f','t','w','a','r','e','\\',
239         'M','i','c','r','o','s','o','f','t','\\',
240         'W','i','n','d','o','w','s',' ','N','T','\\',
241         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
242         'P','r','o','f','i','l','e','L','i','s','t',0};
243     static const WCHAR envW[] = {'E','n','v','i','r','o','n','m','e','n','t',0};
244     static const WCHAR volatile_envW[] = {'V','o','l','a','t','i','l','e',' ','E','n','v','i','r','o','n','m','e','n','t',0};
245     static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
246
247     static const WCHAR SystemRootW[] = {'S','y','s','t','e','m','R','o','o','t',0};
248     static const WCHAR SystemDriveW[] = {'S','y','s','t','e','m','D','r','i','v','e',0};
249     static const WCHAR AllUsersProfileW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e',0};
250     static const WCHAR ALLUSERSPROFILEW[] = {'A','L','L','U','S','E','R','S','P','R','O','F','I','L','E',0};
251     static const WCHAR USERNAMEW[] = {'U','S','E','R','N','A','M','E',0};
252     static const WCHAR USERPROFILEW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
253     static const WCHAR DefaultW[] = {'D','e','f','a','u','l','t',0};
254     static const WCHAR COMPUTERNAMEW[] = {'C','O','M','P','U','T','E','R','N','A','M','E',0};
255
256     WCHAR *env, buf[UNICODE_STRING_MAX_CHARS], profiles_dir[MAX_PATH];
257     UNICODE_STRING us_name, us_val;
258     DWORD len;
259     HKEY hkey, hsubkey;
260
261     TRACE("%p %p %d\n", lpEnvironment, hToken, bInherit );
262
263     if (!lpEnvironment)
264         return FALSE;
265
266     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, env_keyW, 0, KEY_READ, &hkey) != ERROR_SUCCESS)
267         return FALSE;
268
269     if (RtlCreateEnvironment(bInherit, &env) != STATUS_SUCCESS)
270     {
271         RegCloseKey(hkey);
272         return FALSE;
273     }
274
275     if (!GetEnvironmentVariableW(SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
276     {
277         if (!get_reg_value(env, hkey, SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
278         {
279             buf[0] = 0;
280             WARN("SystemRoot variable not set\n");
281         }
282     }
283     RtlInitUnicodeString(&us_name, SystemRootW);
284     RtlInitUnicodeString(&us_val, buf);
285     RtlSetEnvironmentVariable(&env, &us_name, &us_val);
286
287     if (!GetEnvironmentVariableW(SystemDriveW, buf, UNICODE_STRING_MAX_CHARS))
288     {
289         if (!get_reg_value(env, hkey, SystemRootW, buf, UNICODE_STRING_MAX_CHARS))
290         {
291             buf[0] = 0;
292             WARN("SystemDrive variable not set\n");
293         }
294     }
295     RtlInitUnicodeString(&us_name, SystemDriveW);
296     RtlInitUnicodeString(&us_val, buf);
297     RtlSetEnvironmentVariable(&env, &us_name, &us_val);
298
299     set_registry_variables(&env, hkey, REG_SZ, !bInherit);
300     set_registry_variables(&env, hkey, REG_EXPAND_SZ, !bInherit);
301
302     if (RegOpenKeyExW(hkey, envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
303     {
304         set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
305         set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
306         RegCloseKey(hsubkey);
307     }
308
309     if (RegOpenKeyExW(hkey, volatile_envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
310     {
311         set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
312         set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
313         RegCloseKey(hsubkey);
314     }
315     RegCloseKey(hkey);
316
317     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, profile_keyW, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
318     {
319         if (get_reg_value(env, hkey, ProfilesDirectoryW, profiles_dir, MAX_PATH-sizeof(WCHAR)))
320         {
321             len = strlenW(profiles_dir);
322             if (profiles_dir[len-1] != '\\')
323             {
324                 profiles_dir[len++] = '\\';
325                 profiles_dir[len] = '\0';
326             }
327
328             memcpy(buf, profiles_dir, len*sizeof(WCHAR));
329             if (get_reg_value(env, hkey, AllUsersProfileW, buf+len, UNICODE_STRING_MAX_CHARS-len))
330             {
331                 RtlInitUnicodeString(&us_name, ALLUSERSPROFILEW);
332                 RtlInitUnicodeString(&us_val, buf);
333                 RtlSetEnvironmentVariable(&env, &us_name, &us_val);
334             }
335         }
336         else
337         {
338             profiles_dir[0] = 0;
339         }
340
341         RegCloseKey(hkey);
342     }
343
344     len = sizeof(buf)/sizeof(WCHAR);
345     if (GetComputerNameW(buf, &len))
346     {
347         RtlInitUnicodeString(&us_name, COMPUTERNAMEW);
348         RtlInitUnicodeString(&us_val, buf);
349         RtlSetEnvironmentVariable(&env, &us_name, &us_val);
350     }
351
352     set_wow64_environment(&env);
353
354     if (!hToken)
355     {
356         if (profiles_dir[0])
357         {
358             len = strlenW(profiles_dir);
359             if (len*sizeof(WCHAR)+sizeof(DefaultW) < sizeof(buf))
360             {
361                 memcpy(buf, profiles_dir, len*sizeof(WCHAR));
362                 memcpy(buf+len, DefaultW, sizeof(DefaultW));
363                 RtlInitUnicodeString(&us_name, USERPROFILEW);
364                 RtlInitUnicodeString(&us_val, buf);
365                 RtlSetEnvironmentVariable(&env, &us_name, &us_val);
366             }
367         }
368
369         buf[0] = '.';
370         memcpy(buf+1, DefaultW, sizeof(DefaultW));
371     }
372     else
373     {
374         TOKEN_USER *token_user = NULL;
375         SID_NAME_USE use;
376         WCHAR *sidW;
377         DWORD size, tmp=0;
378
379         if (GetTokenInformation(hToken, TokenUser, NULL, 0, &len) ||
380                 GetLastError()!=ERROR_INSUFFICIENT_BUFFER ||
381                 !(token_user = HeapAlloc(GetProcessHeap(), 0, len)) ||
382                 !GetTokenInformation(hToken, TokenUser, token_user, len, &len) ||
383                 !ConvertSidToStringSidW(token_user->User.Sid, &sidW))
384         {
385             HeapFree(GetProcessHeap(), 0, token_user);
386             RtlDestroyEnvironment(env);
387             return FALSE;
388         }
389
390         len = strlenW(profiles_dir);
391         memcpy(buf, profiles_dir, len*sizeof(WCHAR));
392
393         size = UNICODE_STRING_MAX_CHARS-len;
394         if (LookupAccountSidW(NULL, token_user->User.Sid,
395                     buf+len, &size, NULL, &tmp, &use))
396         {
397             RtlInitUnicodeString(&us_name, USERNAMEW);
398             RtlInitUnicodeString(&us_val, buf+len);
399             RtlSetEnvironmentVariable(&env, &us_name, &us_val);
400
401             if (len)
402             {
403                 RtlInitUnicodeString(&us_name, USERPROFILEW);
404                 RtlInitUnicodeString(&us_val, buf);
405                 RtlSetEnvironmentVariable(&env, &us_name, &us_val);
406             }
407         }
408
409         HeapFree(GetProcessHeap(), 0, token_user);
410         strcpyW(buf, sidW);
411         LocalFree(sidW);
412     }
413
414     if (RegOpenKeyExW(HKEY_USERS, buf, 0, KEY_READ, &hkey) == ERROR_SUCCESS)
415     {
416         if (RegOpenKeyExW(hkey, envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
417         {
418             set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
419             set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
420             RegCloseKey(hsubkey);
421         }
422
423         if (RegOpenKeyExW(hkey, volatile_envW, 0, KEY_READ, &hsubkey) == ERROR_SUCCESS)
424         {
425             set_registry_variables(&env, hsubkey, REG_SZ, !bInherit);
426             set_registry_variables(&env, hsubkey, REG_EXPAND_SZ, !bInherit);
427             RegCloseKey(hsubkey);
428         }
429         RegCloseKey(hkey);
430     }
431
432     *lpEnvironment = env;
433     return TRUE;
434 }
435
436 BOOL WINAPI DestroyEnvironmentBlock(LPVOID lpEnvironment)
437 {
438     NTSTATUS r;
439
440     TRACE("%p\n", lpEnvironment);
441     r = RtlDestroyEnvironment(lpEnvironment);
442     if (r == STATUS_SUCCESS)
443         return TRUE;
444     return FALSE;
445 }
446
447 BOOL WINAPI ExpandEnvironmentStringsForUserA( HANDLE hToken, LPCSTR lpSrc,
448                      LPSTR lpDest, DWORD dwSize )
449 {
450     BOOL ret;
451
452     TRACE("%p %s %p %d\n", hToken, debugstr_a(lpSrc), lpDest, dwSize);
453
454     ret = ExpandEnvironmentStringsA( lpSrc, lpDest, dwSize );
455     TRACE("<- %s\n", debugstr_a(lpDest));
456     return ret;
457 }
458
459 BOOL WINAPI ExpandEnvironmentStringsForUserW( HANDLE hToken, LPCWSTR lpSrc,
460                      LPWSTR lpDest, DWORD dwSize )
461 {
462     BOOL ret;
463
464     TRACE("%p %s %p %d\n", hToken, debugstr_w(lpSrc), lpDest, dwSize);
465
466     ret = ExpandEnvironmentStringsW( lpSrc, lpDest, dwSize );
467     TRACE("<- %s\n", debugstr_w(lpDest));
468     return ret;
469 }
470
471 BOOL WINAPI GetDefaultUserProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
472 {
473     FIXME("%p %p\n", lpProfileDir, lpcchSize );
474     return FALSE;
475 }
476
477 BOOL WINAPI GetDefaultUserProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
478 {
479     FIXME("%p %p\n", lpProfileDir, lpcchSize );
480     return FALSE;
481 }
482
483 BOOL WINAPI GetUserProfileDirectoryA( HANDLE hToken, LPSTR lpProfileDir,
484                      LPDWORD lpcchSize )
485 {
486     BOOL ret;
487     WCHAR *dirW = NULL;
488
489     TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
490
491     if (!lpProfileDir || !lpcchSize)
492     {
493         SetLastError( ERROR_INVALID_PARAMETER );
494         return FALSE;
495     }
496     if (!(dirW = HeapAlloc( GetProcessHeap(), 0, *lpcchSize * sizeof(WCHAR) )))
497         return FALSE;
498
499     if ((ret = GetUserProfileDirectoryW( hToken, dirW, lpcchSize )))
500         WideCharToMultiByte( CP_ACP, 0, dirW, *lpcchSize, lpProfileDir, *lpcchSize, NULL, NULL );
501
502     HeapFree( GetProcessHeap(), 0, dirW );
503     return ret;
504 }
505
506 BOOL WINAPI GetUserProfileDirectoryW( HANDLE hToken, LPWSTR lpProfileDir,
507                      LPDWORD lpcchSize )
508 {
509     static const WCHAR slashW[] = {'\\',0};
510     TOKEN_USER *t;
511     WCHAR *userW = NULL, *dirW = NULL;
512     DWORD len, dir_len, domain_len;
513     SID_NAME_USE use;
514     BOOL ret = FALSE;
515
516     TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
517
518     if (!lpcchSize)
519     {
520         SetLastError( ERROR_INVALID_PARAMETER );
521         return FALSE;
522     }
523
524     len = 0;
525     GetTokenInformation( hToken, TokenUser, NULL, 0, &len );
526     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
527     if (!(t = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
528     if (!GetTokenInformation( hToken, TokenUser, t, len, &len )) goto done;
529
530     len = domain_len = 0;
531     LookupAccountSidW( NULL, t->User.Sid, NULL, &len, NULL, &domain_len, NULL );
532     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
533     if (!(userW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) goto done;
534     if (!LookupAccountSidW( NULL, t->User.Sid, userW, &len, NULL, &domain_len, &use )) goto done;
535
536     dir_len = 0;
537     GetProfilesDirectoryW( NULL, &dir_len );
538     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
539     if (!(dirW = HeapAlloc( GetProcessHeap(), 0, (dir_len + 1) * sizeof(WCHAR) ))) goto done;
540     if (!GetProfilesDirectoryW( dirW, &dir_len )) goto done;
541
542     len += dir_len + 2;
543     if (*lpcchSize < len)
544     {
545         SetLastError( ERROR_INSUFFICIENT_BUFFER );
546         *lpcchSize = len;
547         goto done;
548     }
549     strcpyW( lpProfileDir, dirW );
550     strcatW( lpProfileDir, slashW );
551     strcatW( lpProfileDir, userW );
552     *lpcchSize = len;
553     ret = TRUE;
554
555 done:
556     HeapFree( GetProcessHeap(), 0, t );
557     HeapFree( GetProcessHeap(), 0, userW );
558     HeapFree( GetProcessHeap(), 0, dirW );
559     return ret;
560 }
561
562 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
563
564 BOOL WINAPI GetProfilesDirectoryA( LPSTR lpProfilesDir, LPDWORD lpcchSize )
565 {
566     static const char ProfilesDirectory[] = "ProfilesDirectory";
567     LONG l;
568     HKEY key;
569     BOOL ret = FALSE;
570     DWORD len = 0, expanded_len;
571     LPSTR unexpanded_profiles_dir = NULL;
572
573     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
574
575     if (!lpProfilesDir || !lpcchSize)
576     {
577         SetLastError(ERROR_INVALID_PARAMETER);
578         return FALSE;
579     }
580
581     l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
582     if (l)
583     {
584         SetLastError(l);
585         return FALSE;
586     }
587     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &len);
588     if (l)
589     {
590         SetLastError(l);
591         goto end;
592     }
593     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
594     if (!unexpanded_profiles_dir)
595     {
596         SetLastError(ERROR_OUTOFMEMORY);
597         goto end;
598     }
599     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL,
600                              (BYTE *)unexpanded_profiles_dir, &len);
601     if (l)
602     {
603         SetLastError(l);
604         goto end;
605     }
606     expanded_len = ExpandEnvironmentStringsA(unexpanded_profiles_dir, NULL, 0);
607     /* The returned length doesn't include the NULL terminator. */
608     if (*lpcchSize < expanded_len - 1)
609     {
610         *lpcchSize = expanded_len - 1;
611         SetLastError(ERROR_INSUFFICIENT_BUFFER);
612         goto end;
613     }
614     *lpcchSize = expanded_len - 1;
615     /* The return value is also the expected length. */
616     ret = ExpandEnvironmentStringsA(unexpanded_profiles_dir, lpProfilesDir,
617                                     expanded_len) - 1;
618 end:
619     HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
620     RegCloseKey(key);
621     return ret;
622 }
623
624 static const WCHAR ProfileListW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','P','r','o','f','i','l','e','L','i','s','t',0};
625
626 BOOL WINAPI GetProfilesDirectoryW( LPWSTR lpProfilesDir, LPDWORD lpcchSize )
627 {
628     static const WCHAR ProfilesDirectory[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
629     LONG l;
630     HKEY key;
631     BOOL ret = FALSE;
632     DWORD len = 0, expanded_len;
633     LPWSTR unexpanded_profiles_dir = NULL;
634
635     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
636
637     if (!lpcchSize)
638     {
639         SetLastError(ERROR_INVALID_PARAMETER);
640         return FALSE;
641     }
642
643     l = RegOpenKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, KEY_READ, &key);
644     if (l)
645     {
646         SetLastError(l);
647         return FALSE;
648     }
649     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL, NULL, &len);
650     if (l)
651     {
652         SetLastError(l);
653         goto end;
654     }
655     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
656     if (!unexpanded_profiles_dir)
657     {
658         SetLastError(ERROR_OUTOFMEMORY);
659         goto end;
660     }
661     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL,
662                              (BYTE *)unexpanded_profiles_dir, &len);
663     if (l)
664     {
665         SetLastError(l);
666         goto end;
667     }
668     expanded_len = ExpandEnvironmentStringsW(unexpanded_profiles_dir, NULL, 0);
669     /* The returned length doesn't include the NULL terminator. */
670     if (*lpcchSize < expanded_len - 1 || !lpProfilesDir)
671     {
672         *lpcchSize = expanded_len - 1;
673         SetLastError(ERROR_INSUFFICIENT_BUFFER);
674         goto end;
675     }
676     *lpcchSize = expanded_len - 1;
677     /* The return value is also the expected length. */
678     ret = ExpandEnvironmentStringsW(unexpanded_profiles_dir, lpProfilesDir,
679                                     expanded_len) - 1;
680 end:
681     HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
682     RegCloseKey(key);
683     return ret;
684 }
685
686 BOOL WINAPI GetAllUsersProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
687 {
688     FIXME("%p %p\n", lpProfileDir, lpcchSize);
689     return FALSE;
690 }
691
692 BOOL WINAPI GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
693 {
694     FIXME("%p %p\n", lpProfileDir, lpcchSize);
695     return FALSE;
696 }
697
698 BOOL WINAPI GetProfileType( DWORD *pdwFlags )
699 {
700     FIXME("%p\n", pdwFlags );
701     *pdwFlags = 0;
702     return TRUE;
703 }
704
705 BOOL WINAPI LoadUserProfileA( HANDLE hToken, LPPROFILEINFOA lpProfileInfo )
706 {
707     FIXME("%p %p\n", hToken, lpProfileInfo );
708     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
709     return TRUE;
710 }
711
712 BOOL WINAPI LoadUserProfileW( HANDLE hToken, LPPROFILEINFOW lpProfileInfo )
713 {
714     FIXME("%p %p\n", hToken, lpProfileInfo );
715     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
716     return TRUE;
717 }
718
719 BOOL WINAPI RegisterGPNotification( HANDLE event, BOOL machine )
720 {
721     FIXME("%p %d\n", event, machine );
722     return TRUE;
723 }
724
725 BOOL WINAPI UnregisterGPNotification( HANDLE event )
726 {
727     FIXME("%p\n", event );
728     return TRUE;
729 }
730
731 BOOL WINAPI UnloadUserProfile( HANDLE hToken, HANDLE hProfile )
732 {
733     FIXME("(%p, %p): stub\n", hToken, hProfile);
734     return FALSE;
735 }
736
737 /******************************************************************************
738  *              USERENV.138
739  *
740  * Create .lnk file
741  *
742  * PARAMETERS
743  *   int     csidl               [in] well-known directory location to create link in
744  *   LPCSTR lnk_dir              [in] directory (relative to directory specified by csidl) to create link in
745  *   LPCSTR lnk_filename         [in] filename of the link file without .lnk extension
746  *   LPCSTR lnk_target           [in] file/directory pointed to by link
747  *   LPCSTR lnk_iconfile         [in] link icon resource filename
748  *   DWORD   lnk_iconid          [in] link icon resource id in file referred by lnk_iconfile
749  *   LPCSTR work_directory       [in] link target's work directory
750  *   WORD    hotkey              [in] link hotkey (virtual key id)
751  *   DWORD   win_state           [in] initial window size (SW_SHOWMAXIMIZED to start maximized,
752  *                                     SW_SHOWMINNOACTIVE to start minimized, everything else is default state)
753  *   LPCSTR comment              [in] comment - link's comment
754  *   LPCSTR loc_filename_resfile [in] resource file which holds localized filename for this link file
755  *   DWORD   loc_filename_resid  [in] resource id for this link file's localized filename
756  *
757  * RETURNS
758  *    TRUE:  Link file was successfully created
759  *    FALSE: Link file was not created
760  */
761 BOOL WINAPI USERENV_138( int csidl, LPCSTR lnk_dir, LPCSTR lnk_filename,
762             LPCSTR lnk_target, LPCSTR lnk_iconfile, DWORD lnk_iconid,
763             LPCSTR work_directory, WORD hotkey, DWORD win_state, LPCSTR comment,
764             LPCSTR loc_filename_resfile, DWORD loc_filename_resid)
765 {
766     FIXME("(%d,%s,%s,%s,%s,%d,%s,0x%x,%d,%s,%s,%d) - stub\n", csidl, debugstr_a(lnk_dir),
767             debugstr_a(lnk_filename), debugstr_a(lnk_target), debugstr_a(lnk_iconfile),
768             lnk_iconid, debugstr_a(work_directory), hotkey, win_state,
769             debugstr_a(comment), debugstr_a(loc_filename_resfile), loc_filename_resid );
770
771     return FALSE;
772 }