gdi32: Simplify the code a little by using a separate variable for the return value.
[wine/multimedia.git] / dlls / userenv / userenv_main.c
blob3a02149688852077d5bdb59ed7d9c798845ccacf
1 /*
2 * Implementation of userenv.dll
4 * Copyright 2006 Mike McCormack for CodeWeavers
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
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 "userenv.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL( userenv );
37 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
39 TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
41 switch (fdwReason)
43 case DLL_WINE_PREATTACH:
44 return FALSE; /* prefer native version */
45 case DLL_PROCESS_ATTACH:
46 DisableThreadLibraryCalls(hinstDLL);
47 break;
48 case DLL_PROCESS_DETACH:
49 break;
51 return TRUE;
54 BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
55 HANDLE hToken, BOOL bInherit )
57 NTSTATUS r;
59 TRACE("%p %p %d\n", lpEnvironment, hToken, bInherit );
61 if (!lpEnvironment)
62 return FALSE;
64 r = RtlCreateEnvironment(bInherit, (WCHAR **)lpEnvironment);
65 if (r == STATUS_SUCCESS)
66 return TRUE;
67 return FALSE;
70 BOOL WINAPI DestroyEnvironmentBlock(LPVOID lpEnvironment)
72 NTSTATUS r;
74 TRACE("%p\n", lpEnvironment);
75 r = RtlDestroyEnvironment(lpEnvironment);
76 if (r == STATUS_SUCCESS)
77 return TRUE;
78 return FALSE;
81 BOOL WINAPI ExpandEnvironmentStringsForUserA( HANDLE hToken, LPCSTR lpSrc,
82 LPSTR lpDest, DWORD dwSize )
84 BOOL ret;
86 TRACE("%p %s %p %d\n", hToken, debugstr_a(lpSrc), lpDest, dwSize);
88 ret = ExpandEnvironmentStringsA( lpSrc, lpDest, dwSize );
89 TRACE("<- %s\n", debugstr_a(lpDest));
90 return ret;
93 BOOL WINAPI ExpandEnvironmentStringsForUserW( HANDLE hToken, LPCWSTR lpSrc,
94 LPWSTR lpDest, DWORD dwSize )
96 BOOL ret;
98 TRACE("%p %s %p %d\n", hToken, debugstr_w(lpSrc), lpDest, dwSize);
100 ret = ExpandEnvironmentStringsW( lpSrc, lpDest, dwSize );
101 TRACE("<- %s\n", debugstr_w(lpDest));
102 return ret;
105 BOOL WINAPI GetDefaultUserProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
107 FIXME("%p %p\n", lpProfileDir, lpcchSize );
108 return FALSE;
111 BOOL WINAPI GetDefaultUserProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
113 FIXME("%p %p\n", lpProfileDir, lpcchSize );
114 return FALSE;
117 BOOL WINAPI GetUserProfileDirectoryA( HANDLE hToken, LPSTR lpProfileDir,
118 LPDWORD lpcchSize )
120 BOOL ret;
121 WCHAR *dirW = NULL;
123 TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
125 if (!lpProfileDir || !lpcchSize)
127 SetLastError( ERROR_INVALID_PARAMETER );
128 return FALSE;
130 if (!(dirW = HeapAlloc( GetProcessHeap(), 0, *lpcchSize * sizeof(WCHAR) )))
131 return FALSE;
133 if ((ret = GetUserProfileDirectoryW( hToken, dirW, lpcchSize )))
134 WideCharToMultiByte( CP_ACP, 0, dirW, *lpcchSize, lpProfileDir, *lpcchSize, NULL, NULL );
136 HeapFree( GetProcessHeap(), 0, dirW );
137 return ret;
140 BOOL WINAPI GetUserProfileDirectoryW( HANDLE hToken, LPWSTR lpProfileDir,
141 LPDWORD lpcchSize )
143 static const WCHAR slashW[] = {'\\',0};
144 TOKEN_USER *t;
145 WCHAR *userW = NULL, *dirW = NULL;
146 DWORD len, dir_len, domain_len;
147 SID_NAME_USE use;
148 BOOL ret = FALSE;
150 TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
152 if (!lpcchSize)
154 SetLastError( ERROR_INVALID_PARAMETER );
155 return FALSE;
158 len = 0;
159 GetTokenInformation( hToken, TokenUser, NULL, 0, &len );
160 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return FALSE;
161 if (!(t = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
162 if (!GetTokenInformation( hToken, TokenUser, t, len, &len )) goto done;
164 len = domain_len = 0;
165 LookupAccountSidW( NULL, t->User.Sid, NULL, &len, NULL, &domain_len, NULL );
166 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
167 if (!(userW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) goto done;
168 if (!LookupAccountSidW( NULL, t->User.Sid, userW, &len, NULL, &domain_len, &use )) goto done;
170 dir_len = 0;
171 GetProfilesDirectoryW( NULL, &dir_len );
172 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) goto done;
173 if (!(dirW = HeapAlloc( GetProcessHeap(), 0, (dir_len + 1) * sizeof(WCHAR) ))) goto done;
174 if (!GetProfilesDirectoryW( dirW, &dir_len )) goto done;
176 len += dir_len + 2;
177 if (*lpcchSize < len)
179 SetLastError( ERROR_INSUFFICIENT_BUFFER );
180 *lpcchSize = len;
181 goto done;
183 strcpyW( lpProfileDir, dirW );
184 strcatW( lpProfileDir, slashW );
185 strcatW( lpProfileDir, userW );
186 *lpcchSize = len;
187 ret = TRUE;
189 done:
190 HeapFree( GetProcessHeap(), 0, t );
191 HeapFree( GetProcessHeap(), 0, userW );
192 HeapFree( GetProcessHeap(), 0, dirW );
193 return ret;
196 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
198 BOOL WINAPI GetProfilesDirectoryA( LPSTR lpProfilesDir, LPDWORD lpcchSize )
200 static const char ProfilesDirectory[] = "ProfilesDirectory";
201 LONG l;
202 HKEY key;
203 BOOL ret = FALSE;
204 DWORD len = 0, expanded_len;
205 LPSTR unexpanded_profiles_dir = NULL;
207 TRACE("%p %p\n", lpProfilesDir, lpcchSize );
209 if (!lpProfilesDir || !lpcchSize)
211 SetLastError(ERROR_INVALID_PARAMETER);
212 return FALSE;
215 l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
216 if (l)
218 SetLastError(l);
219 return FALSE;
221 l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &len);
222 if (l)
224 SetLastError(l);
225 goto end;
227 unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
228 if (!unexpanded_profiles_dir)
230 SetLastError(ERROR_OUTOFMEMORY);
231 goto end;
233 l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL,
234 (BYTE *)unexpanded_profiles_dir, &len);
235 if (l)
237 SetLastError(l);
238 goto end;
240 expanded_len = ExpandEnvironmentStringsA(unexpanded_profiles_dir, NULL, 0);
241 /* The returned length doesn't include the NULL terminator. */
242 if (*lpcchSize < expanded_len - 1)
244 *lpcchSize = expanded_len - 1;
245 SetLastError(ERROR_INSUFFICIENT_BUFFER);
246 goto end;
248 *lpcchSize = expanded_len - 1;
249 /* The return value is also the expected length. */
250 ret = ExpandEnvironmentStringsA(unexpanded_profiles_dir, lpProfilesDir,
251 expanded_len) - 1;
252 end:
253 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
254 RegCloseKey(key);
255 return ret;
258 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};
260 BOOL WINAPI GetProfilesDirectoryW( LPWSTR lpProfilesDir, LPDWORD lpcchSize )
262 static const WCHAR ProfilesDirectory[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
263 LONG l;
264 HKEY key;
265 BOOL ret = FALSE;
266 DWORD len = 0, expanded_len;
267 LPWSTR unexpanded_profiles_dir = NULL;
269 TRACE("%p %p\n", lpProfilesDir, lpcchSize );
271 if (!lpcchSize)
273 SetLastError(ERROR_INVALID_PARAMETER);
274 return FALSE;
277 l = RegOpenKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, KEY_READ, &key);
278 if (l)
280 SetLastError(l);
281 return FALSE;
283 l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL, NULL, &len);
284 if (l)
286 SetLastError(l);
287 goto end;
289 unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
290 if (!unexpanded_profiles_dir)
292 SetLastError(ERROR_OUTOFMEMORY);
293 goto end;
295 l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL,
296 (BYTE *)unexpanded_profiles_dir, &len);
297 if (l)
299 SetLastError(l);
300 goto end;
302 expanded_len = ExpandEnvironmentStringsW(unexpanded_profiles_dir, NULL, 0);
303 /* The returned length doesn't include the NULL terminator. */
304 if (*lpcchSize < expanded_len - 1 || !lpProfilesDir)
306 *lpcchSize = expanded_len - 1;
307 SetLastError(ERROR_INSUFFICIENT_BUFFER);
308 goto end;
310 *lpcchSize = expanded_len - 1;
311 /* The return value is also the expected length. */
312 ret = ExpandEnvironmentStringsW(unexpanded_profiles_dir, lpProfilesDir,
313 expanded_len) - 1;
314 end:
315 HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
316 RegCloseKey(key);
317 return ret;
320 BOOL WINAPI GetAllUsersProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
322 FIXME("%p %p\n", lpProfileDir, lpcchSize);
323 return FALSE;
326 BOOL WINAPI GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
328 FIXME("%p %p\n", lpProfileDir, lpcchSize);
329 return FALSE;
332 BOOL WINAPI GetProfileType( DWORD *pdwFlags )
334 FIXME("%p\n", pdwFlags );
335 *pdwFlags = 0;
336 return TRUE;
339 BOOL WINAPI LoadUserProfileA( HANDLE hToken, LPPROFILEINFOA lpProfileInfo )
341 FIXME("%p %p\n", hToken, lpProfileInfo );
342 lpProfileInfo->hProfile = HKEY_CURRENT_USER;
343 return TRUE;
346 BOOL WINAPI LoadUserProfileW( HANDLE hToken, LPPROFILEINFOW lpProfileInfo )
348 FIXME("%p %p\n", hToken, lpProfileInfo );
349 lpProfileInfo->hProfile = HKEY_CURRENT_USER;
350 return TRUE;
353 BOOL WINAPI RegisterGPNotification( HANDLE event, BOOL machine )
355 FIXME("%p %d\n", event, machine );
356 return TRUE;
359 BOOL WINAPI UnregisterGPNotification( HANDLE event )
361 FIXME("%p\n", event );
362 return TRUE;
365 BOOL WINAPI UnloadUserProfile( HANDLE hToken, HANDLE hProfile )
367 FIXME("(%p, %p): stub\n", hToken, hProfile);
368 return FALSE;
371 /******************************************************************************
372 * USERENV.138
374 * Create .lnk file
376 * PARAMETERS
377 * int csidl [in] well-known directory location to create link in
378 * LPCSTR lnk_dir [in] directory (relative to directory specified by csidl) to create link in
379 * LPCSTR lnk_filename [in] filename of the link file without .lnk extension
380 * LPCSTR lnk_target [in] file/directory pointed to by link
381 * LPCSTR lnk_iconfile [in] link icon resource filename
382 * DWORD lnk_iconid [in] link icon resource id in file referred by lnk_iconfile
383 * LPCSTR work_directory [in] link target's work directory
384 * WORD hotkey [in] link hotkey (virtual key id)
385 * DWORD win_state [in] initial window size (SW_SHOWMAXIMIZED to start maximized,
386 * SW_SHOWMINNOACTIVE to start minimized, everything else is default state)
387 * LPCSTR comment [in] comment - link's comment
388 * LPCSTR loc_filename_resfile [in] resource file which holds localized filename for this link file
389 * DWORD loc_filename_resid [in] resource id for this link file's localized filename
391 * RETURNS
392 * TRUE: Link file was successfully created
393 * FALSE: Link file was not created
395 BOOL WINAPI USERENV_138( int csidl, LPCSTR lnk_dir, LPCSTR lnk_filename,
396 LPCSTR lnk_target, LPCSTR lnk_iconfile, DWORD lnk_iconid,
397 LPCSTR work_directory, WORD hotkey, DWORD win_state, LPCSTR comment,
398 LPCSTR loc_filename_resfile, DWORD loc_filename_resid)
400 FIXME("(%d,%s,%s,%s,%s,%d,%s,0x%x,%d,%s,%s,%d) - stub\n", csidl, debugstr_a(lnk_dir),
401 debugstr_a(lnk_filename), debugstr_a(lnk_target), debugstr_a(lnk_iconfile),
402 lnk_iconid, debugstr_a(work_directory), hotkey, win_state,
403 debugstr_a(comment), debugstr_a(loc_filename_resfile), loc_filename_resid );
405 return FALSE;