makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[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 "userenv.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL( userenv );
34
35 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
36 {
37     TRACE("%p %d %p\n", hinstDLL, fdwReason, lpvReserved);
38
39     switch (fdwReason)
40     {
41     case DLL_WINE_PREATTACH:
42         return FALSE;  /* prefer native version */
43     case DLL_PROCESS_ATTACH:
44         DisableThreadLibraryCalls(hinstDLL);
45         break;
46     case DLL_PROCESS_DETACH:
47         break;
48     }
49     return TRUE;
50 }
51
52 BOOL WINAPI CreateEnvironmentBlock( LPVOID* lpEnvironment,
53                      HANDLE hToken, BOOL bInherit )
54 {
55     NTSTATUS r;
56
57     TRACE("%p %p %d\n", lpEnvironment, hToken, bInherit );
58
59     if (!lpEnvironment)
60         return FALSE;
61
62     r = RtlCreateEnvironment(bInherit, (WCHAR **)lpEnvironment);
63     if (r == STATUS_SUCCESS)
64         return TRUE;
65     return FALSE;
66 }
67
68 BOOL WINAPI DestroyEnvironmentBlock(LPVOID lpEnvironment)
69 {
70     NTSTATUS r;
71
72     TRACE("%p\n", lpEnvironment);
73     r = RtlDestroyEnvironment(lpEnvironment);
74     if (r == STATUS_SUCCESS)
75         return TRUE;
76     return FALSE;
77 }
78
79 BOOL WINAPI ExpandEnvironmentStringsForUserA( HANDLE hToken, LPCSTR lpSrc,
80                      LPSTR lpDest, DWORD dwSize )
81 {
82     BOOL ret;
83
84     TRACE("%p %s %p %d\n", hToken, debugstr_a(lpSrc), lpDest, dwSize);
85
86     ret = ExpandEnvironmentStringsA( lpSrc, lpDest, dwSize );
87     TRACE("<- %s\n", debugstr_a(lpDest));
88     return ret;
89 }
90
91 BOOL WINAPI ExpandEnvironmentStringsForUserW( HANDLE hToken, LPCWSTR lpSrc,
92                      LPWSTR lpDest, DWORD dwSize )
93 {
94     BOOL ret;
95
96     TRACE("%p %s %p %d\n", hToken, debugstr_w(lpSrc), lpDest, dwSize);
97
98     ret = ExpandEnvironmentStringsW( lpSrc, lpDest, dwSize );
99     TRACE("<- %s\n", debugstr_w(lpDest));
100     return ret;
101 }
102
103 BOOL WINAPI GetUserProfileDirectoryA( HANDLE hToken, LPSTR lpProfileDir,
104                      LPDWORD lpcchSize )
105 {
106     FIXME("%p %p %p\n", hToken, lpProfileDir, lpcchSize );
107     return FALSE;
108 }
109
110 BOOL WINAPI GetUserProfileDirectoryW( HANDLE hToken, LPWSTR lpProfileDir,
111                      LPDWORD lpcchSize )
112 {
113     FIXME("%p %p %p\n", hToken, lpProfileDir, lpcchSize );
114     return FALSE;
115 }
116
117 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
118
119 BOOL WINAPI GetProfilesDirectoryA( LPSTR lpProfilesDir, LPDWORD lpcchSize )
120 {
121     static const char ProfilesDirectory[] = "ProfilesDirectory";
122     LONG l;
123     HKEY key;
124     BOOL ret = FALSE;
125     DWORD len = 0, expanded_len;
126     LPSTR unexpanded_profiles_dir = NULL;
127
128     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
129
130     if (!lpProfilesDir || !lpcchSize)
131     {
132         SetLastError(ERROR_INVALID_PARAMETER);
133         return FALSE;
134     }
135
136     l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
137     if (l)
138     {
139         SetLastError(l);
140         return FALSE;
141     }
142     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &len);
143     if (l)
144     {
145         SetLastError(l);
146         goto end;
147     }
148     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
149     if (!unexpanded_profiles_dir)
150     {
151         SetLastError(ERROR_OUTOFMEMORY);
152         goto end;
153     }
154     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL,
155                              (BYTE *)unexpanded_profiles_dir, &len);
156     if (l)
157     {
158         SetLastError(l);
159         goto end;
160     }
161     expanded_len = ExpandEnvironmentStringsA(unexpanded_profiles_dir, NULL, 0);
162     /* The returned length doesn't include the NULL terminator. */
163     if (*lpcchSize < expanded_len - 1)
164     {
165         *lpcchSize = expanded_len - 1;
166         SetLastError(ERROR_INSUFFICIENT_BUFFER);
167         goto end;
168     }
169     *lpcchSize = expanded_len - 1;
170     /* The return value is also the expected length. */
171     ret = ExpandEnvironmentStringsA(unexpanded_profiles_dir, lpProfilesDir,
172                                     expanded_len) - 1;
173 end:
174     HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
175     RegCloseKey(key);
176     return ret;
177 }
178
179 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};
180
181 BOOL WINAPI GetProfilesDirectoryW( LPWSTR lpProfilesDir, LPDWORD lpcchSize )
182 {
183     static const WCHAR ProfilesDirectory[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
184     LONG l;
185     HKEY key;
186     BOOL ret = FALSE;
187     DWORD len = 0, expanded_len;
188     LPWSTR unexpanded_profiles_dir = NULL;
189
190     TRACE("%p %p\n", lpProfilesDir, lpcchSize );
191
192     if (!lpProfilesDir || !lpcchSize)
193     {
194         SetLastError(ERROR_INVALID_PARAMETER);
195         return FALSE;
196     }
197
198     l = RegOpenKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, KEY_READ, &key);
199     if (l)
200     {
201         SetLastError(l);
202         return FALSE;
203     }
204     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL, NULL, &len);
205     if (l)
206     {
207         SetLastError(l);
208         goto end;
209     }
210     unexpanded_profiles_dir = HeapAlloc(GetProcessHeap(), 0, len);
211     if (!unexpanded_profiles_dir)
212     {
213         SetLastError(ERROR_OUTOFMEMORY);
214         goto end;
215     }
216     l = RegQueryValueExW(key, ProfilesDirectory, NULL, NULL,
217                              (BYTE *)unexpanded_profiles_dir, &len);
218     if (l)
219     {
220         SetLastError(l);
221         goto end;
222     }
223     expanded_len = ExpandEnvironmentStringsW(unexpanded_profiles_dir, NULL, 0);
224     /* The returned length doesn't include the NULL terminator. */
225     if (*lpcchSize < expanded_len - 1)
226     {
227         *lpcchSize = expanded_len - 1;
228         SetLastError(ERROR_INSUFFICIENT_BUFFER);
229         goto end;
230     }
231     *lpcchSize = expanded_len - 1;
232     /* The return value is also the expected length. */
233     ret = ExpandEnvironmentStringsW(unexpanded_profiles_dir, lpProfilesDir,
234                                     expanded_len) - 1;
235 end:
236     HeapFree(GetProcessHeap(), 0, unexpanded_profiles_dir);
237     RegCloseKey(key);
238     return ret;
239 }
240
241 BOOL WINAPI GetAllUsersProfileDirectoryA( LPSTR lpProfileDir, LPDWORD lpcchSize )
242 {
243     FIXME("%p %p\n", lpProfileDir, lpcchSize);
244     return FALSE;
245 }
246
247 BOOL WINAPI GetAllUsersProfileDirectoryW( LPWSTR lpProfileDir, LPDWORD lpcchSize )
248 {
249     FIXME("%p %p\n", lpProfileDir, lpcchSize);
250     return FALSE;
251 }
252
253 BOOL WINAPI GetProfileType( DWORD *pdwFlags )
254 {
255     FIXME("%p\n", pdwFlags );
256     *pdwFlags = 0;
257     return TRUE;
258 }
259
260 BOOL WINAPI LoadUserProfileA( HANDLE hToken, LPPROFILEINFOA lpProfileInfo )
261 {
262     FIXME("%p %p\n", hToken, lpProfileInfo );
263     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
264     return TRUE;
265 }
266
267 BOOL WINAPI LoadUserProfileW( HANDLE hToken, LPPROFILEINFOW lpProfileInfo )
268 {
269     FIXME("%p %p\n", hToken, lpProfileInfo );
270     lpProfileInfo->hProfile = HKEY_CURRENT_USER;
271     return TRUE;
272 }
273
274 BOOL WINAPI RegisterGPNotification( HANDLE event, BOOL machine )
275 {
276     FIXME("%p %d\n", event, machine );
277     return TRUE;
278 }
279
280 BOOL WINAPI UnregisterGPNotification( HANDLE event )
281 {
282     FIXME("%p\n", event );
283     return TRUE;
284 }
285
286 BOOL WINAPI UnloadUserProfile( HANDLE hToken, HANDLE hProfile )
287 {
288     FIXME("(%p, %p): stub\n", hToken, hProfile);
289     return FALSE;
290 }
291
292 /******************************************************************************
293  *              USERENV.138
294  *
295  * Create .lnk file
296  *
297  * PARAMETERS
298  *   int     csidl               [in] well-known directory location to create link in
299  *   LPCSTR lnk_dir              [in] directory (relative to directory specified by csidl) to create link in
300  *   LPCSTR lnk_filename         [in] filename of the link file without .lnk extension
301  *   LPCSTR lnk_target           [in] file/directory pointed to by link
302  *   LPCSTR lnk_iconfile         [in] link icon resource filename
303  *   DWORD   lnk_iconid          [in] link icon resource id in file referred by lnk_iconfile
304  *   LPCSTR work_directory       [in] link target's work directory
305  *   WORD    hotkey              [in] link hotkey (virtual key id)
306  *   DWORD   win_state           [in] initial window size (SW_SHOWMAXIMIZED to start maximized,
307  *                                     SW_SHOWMINNOACTIVE to start minimized, everything else is default state)
308  *   LPCSTR comment              [in] comment - link's comment
309  *   LPCSTR loc_filename_resfile [in] resource file which holds localized filename for this link file
310  *   DWORD   loc_filename_resid  [in] resource id for this link file's localized filename
311  *
312  * RETURNS
313  *    TRUE:  Link file was successfully created
314  *    FALSE: Link file was not created
315  */
316 BOOL WINAPI USERENV_138( int csidl, LPCSTR lnk_dir, LPCSTR lnk_filename,
317             LPCSTR lnk_target, LPCSTR lnk_iconfile, DWORD lnk_iconid,
318             LPCSTR work_directory, WORD hotkey, DWORD win_state, LPCSTR comment,
319             LPCSTR loc_filename_resfile, DWORD loc_filename_resid)
320 {
321     FIXME("(%d,%s,%s,%s,%s,%d,%s,0x%x,%d,%s,%s,%d) - stub\n", csidl, debugstr_a(lnk_dir),
322             debugstr_a(lnk_filename), debugstr_a(lnk_target), debugstr_a(lnk_iconfile),
323             lnk_iconid, debugstr_a(work_directory), hotkey, win_state,
324             debugstr_a(comment), debugstr_a(loc_filename_resfile), loc_filename_resid );
325
326     return FALSE;
327 }