ddraw: COM cleanup for IDirect3DDevice2 interface.
[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 "userenv.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL( userenv );
36
37 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
38 {
39     TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
40
41     switch (fdwReason)
42     {
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;
50     }
51     return TRUE;
52 }
53
54 BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
55                      HANDLE hToken, BOOL bInherit )
56 {
57     NTSTATUS r;
58
59     TRACE("%p %p %d\n", lpEnvironment, hToken, bInherit );
60
61     if (!lpEnvironment)
62         return FALSE;
63
64     r = RtlCreateEnvironment(bInherit, (WCHAR **)lpEnvironment);
65     if (r == STATUS_SUCCESS)
66         return TRUE;
67     return FALSE;
68 }
69
70 BOOL WINAPI DestroyEnvironmentBlock(LPVOID lpEnvironment)
71 {
72     NTSTATUS r;
73
74     TRACE("%p\n", lpEnvironment);
75     r = RtlDestroyEnvironment(lpEnvironment);
76     if (r == STATUS_SUCCESS)
77         return TRUE;
78     return FALSE;
79 }
80
81 BOOL WINAPI ExpandEnvironmentStringsForUserA( HANDLE hToken, LPCSTR lpSrc,
82                      LPSTR lpDest, DWORD dwSize )
83 {
84     BOOL ret;
85
86     TRACE("%p %s %p %d\n", hToken, debugstr_a(lpSrc), lpDest, dwSize);
87
88     ret = ExpandEnvironmentStringsA( lpSrc, lpDest, dwSize );
89     TRACE("<- %s\n", debugstr_a(lpDest));
90     return ret;
91 }
92
93 BOOL WINAPI ExpandEnvironmentStringsForUserW( HANDLE hToken, LPCWSTR lpSrc,
94                      LPWSTR lpDest, DWORD dwSize )
95 {
96     BOOL ret;
97
98     TRACE("%p %s %p %d\n", hToken, debugstr_w(lpSrc), lpDest, dwSize);
99
100     ret = ExpandEnvironmentStringsW( lpSrc, lpDest, dwSize );
101     TRACE("<- %s\n", debugstr_w(lpDest));
102     return ret;
103 }
104
105 BOOL WINAPI GetDefaultUserProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
106 {
107     FIXME("%p %p\n", lpProfileDir, lpcchSize );
108     return FALSE;
109 }
110
111 BOOL WINAPI GetDefaultUserProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
112 {
113     FIXME("%p %p\n", lpProfileDir, lpcchSize );
114     return FALSE;
115 }
116
117 BOOL WINAPI GetUserProfileDirectoryA( HANDLE hToken, LPSTR lpProfileDir,
118                      LPDWORD lpcchSize )
119 {
120     BOOL ret;
121     WCHAR *dirW = NULL;
122
123     TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
124
125     if (!lpProfileDir || !lpcchSize)
126     {
127         SetLastError( ERROR_INVALID_PARAMETER );
128         return FALSE;
129     }
130     if (!(dirW = HeapAlloc( GetProcessHeap(), 0, *lpcchSize * sizeof(WCHAR) )))
131         return FALSE;
132
133     if ((ret = GetUserProfileDirectoryW( hToken, dirW, lpcchSize )))
134         WideCharToMultiByte( CP_ACP, 0, dirW, *lpcchSize, lpProfileDir, *lpcchSize, NULL, NULL );
135
136     HeapFree( GetProcessHeap(), 0, dirW );
137     return ret;
138 }
139
140 BOOL WINAPI GetUserProfileDirectoryW( HANDLE hToken, LPWSTR lpProfileDir,
141                      LPDWORD lpcchSize )
142 {
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;
149
150     TRACE( "%p %p %p\n", hToken, lpProfileDir, lpcchSize );
151
152     if (!lpcchSize)
153     {
154         SetLastError( ERROR_INVALID_PARAMETER );
155         return FALSE;
156     }
157
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;
163
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;
169
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;
175
176     len += dir_len + 2;
177     if (*lpcchSize < len)
178     {
179         SetLastError( ERROR_INSUFFICIENT_BUFFER );
180         *lpcchSize = len;
181         goto done;
182     }
183     strcpyW( lpProfileDir, dirW );
184     strcatW( lpProfileDir, slashW );
185     strcatW( lpProfileDir, userW );
186     *lpcchSize = len;
187     ret = TRUE;
188
189 done:
190     HeapFree( GetProcessHeap(), 0, t );
191     HeapFree( GetProcessHeap(), 0, userW );
192     HeapFree( GetProcessHeap(), 0, dirW );
193     return ret;
194 }
195
196 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
197
198 BOOL WINAPI GetProfilesDirectoryA( LPSTR lpProfilesDir, LPDWORD lpcchSize )
199 {
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;
206
207     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
208
209     if (!lpProfilesDir || !lpcchSize)
210     {
211         SetLastError(ERROR_INVALID_PARAMETER);
212         return FALSE;
213     }
214
215     l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
216     if (l)
217     {
218         SetLastError(l);
219         return FALSE;
220     }
221     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &len);
222     if (l)
223     {
224         SetLastError(l);
225         goto end;
226     }
227     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
228     if (!unexpanded_profiles_dir)
229     {
230         SetLastError(ERROR_OUTOFMEMORY);
231         goto end;
232     }
233     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL,
234                              (BYTE *)unexpanded_profiles_dir, &len);
235     if (l)
236     {
237         SetLastError(l);
238         goto end;
239     }
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)
243     {
244         *lpcchSize = expanded_len - 1;
245         SetLastError(ERROR_INSUFFICIENT_BUFFER);
246         goto end;
247     }
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;
256 }
257
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};
259
260 BOOL WINAPI GetProfilesDirectoryW( LPWSTR lpProfilesDir, LPDWORD lpcchSize )
261 {
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;
268
269     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
270
271     if (!lpcchSize)
272     {
273         SetLastError(ERROR_INVALID_PARAMETER);
274         return FALSE;
275     }
276
277     l = RegOpenKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, KEY_READ, &key);
278     if (l)
279     {
280         SetLastError(l);
281         return FALSE;
282     }
283     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL, NULL, &len);
284     if (l)
285     {
286         SetLastError(l);
287         goto end;
288     }
289     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
290     if (!unexpanded_profiles_dir)
291     {
292         SetLastError(ERROR_OUTOFMEMORY);
293         goto end;
294     }
295     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL,
296                              (BYTE *)unexpanded_profiles_dir, &len);
297     if (l)
298     {
299         SetLastError(l);
300         goto end;
301     }
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)
305     {
306         *lpcchSize = expanded_len - 1;
307         SetLastError(ERROR_INSUFFICIENT_BUFFER);
308         goto end;
309     }
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;
318 }
319
320 BOOL WINAPI GetAllUsersProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
321 {
322     FIXME("%p %p\n", lpProfileDir, lpcchSize);
323     return FALSE;
324 }
325
326 BOOL WINAPI GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
327 {
328     FIXME("%p %p\n", lpProfileDir, lpcchSize);
329     return FALSE;
330 }
331
332 BOOL WINAPI GetProfileType( DWORD *pdwFlags )
333 {
334     FIXME("%p\n", pdwFlags );
335     *pdwFlags = 0;
336     return TRUE;
337 }
338
339 BOOL WINAPI LoadUserProfileA( HANDLE hToken, LPPROFILEINFOA lpProfileInfo )
340 {
341     FIXME("%p %p\n", hToken, lpProfileInfo );
342     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
343     return TRUE;
344 }
345
346 BOOL WINAPI LoadUserProfileW( HANDLE hToken, LPPROFILEINFOW lpProfileInfo )
347 {
348     FIXME("%p %p\n", hToken, lpProfileInfo );
349     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
350     return TRUE;
351 }
352
353 BOOL WINAPI RegisterGPNotification( HANDLE event, BOOL machine )
354 {
355     FIXME("%p %d\n", event, machine );
356     return TRUE;
357 }
358
359 BOOL WINAPI UnregisterGPNotification( HANDLE event )
360 {
361     FIXME("%p\n", event );
362     return TRUE;
363 }
364
365 BOOL WINAPI UnloadUserProfile( HANDLE hToken, HANDLE hProfile )
366 {
367     FIXME("(%p, %p): stub\n", hToken, hProfile);
368     return FALSE;
369 }
370
371 /******************************************************************************
372  *              USERENV.138
373  *
374  * Create .lnk file
375  *
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
390  *
391  * RETURNS
392  *    TRUE:  Link file was successfully created
393  *    FALSE: Link file was not created
394  */
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)
399 {
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 );
404
405     return FALSE;
406 }