3 * Copyright (C) 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/setup.c
24 * PURPOSE: Profile setup functions
25 * PROGRAMMER: Eric Kohl
34 typedef struct _FOLDERDATA
41 BOOL bUserShellFolder
;
42 } FOLDERDATA
, *PFOLDERDATA
;
48 {L
"AppData", L
"Application Data", IDS_APPDATA
, TRUE
, TRUE
, TRUE
},
49 {L
"Desktop", L
"Desktop", IDS_DESKTOP
, FALSE
, TRUE
, TRUE
},
50 {L
"Favorites", L
"Favorites", IDS_FAVORITES
, FALSE
, TRUE
, TRUE
},
51 {L
"Personal", L
"My Documents", IDS_MYDOCUMENTS
, FALSE
, TRUE
, TRUE
},
52 {L
"My Pictures", L
"My Documents\\My Pictures", IDS_MYPICTURES
, FALSE
, TRUE
, TRUE
},
53 {L
"My Music", L
"My Documents\\My Music", IDS_MYMUSIC
, FALSE
, TRUE
, TRUE
},
54 {L
"My Video", L
"My Documents\\My Videos", IDS_MYVIDEOS
, FALSE
, TRUE
, TRUE
},
55 {L
"NetHood", L
"NetHood", IDS_NETHOOD
, TRUE
, TRUE
, TRUE
},
56 {L
"PrintHood", L
"PrintHood", IDS_PRINTHOOD
, TRUE
, TRUE
, TRUE
},
57 {L
"Recent", L
"Recent", IDS_RECENT
, TRUE
, TRUE
, TRUE
},
58 {L
"SendTo", L
"SendTo", IDS_SENDTO
, FALSE
, TRUE
, TRUE
},
59 {L
"Templates", L
"Templates", IDS_TEMPLATES
, FALSE
, TRUE
, TRUE
},
60 {L
"Start Menu", L
"Start Menu", IDS_STARTMENU
, FALSE
, TRUE
, TRUE
},
61 {L
"Programs", L
"Start Menu\\Programs", IDS_PROGRAMS
, FALSE
, TRUE
, TRUE
},
62 {L
"Startup", L
"Start Menu\\Programs\\Startup", IDS_STARTUP
, FALSE
, TRUE
, TRUE
},
63 {L
"Local Settings", L
"Local Settings", IDS_LOCALSETTINGS
, TRUE
, TRUE
, TRUE
},
64 {L
"Local AppData", L
"Local Settings\\Application Data", IDS_LOCALAPPDATA
, TRUE
, TRUE
, TRUE
},
65 {L
"Temp", L
"Local Settings\\Temp", IDS_TEMP
, FALSE
, FALSE
, FALSE
},
66 {L
"Cache", L
"Local Settings\\Temporary Internet Files", IDS_CACHE
, FALSE
, TRUE
, TRUE
},
67 {L
"History", L
"Local Settings\\History", IDS_HISTORY
, FALSE
, TRUE
, TRUE
},
68 {L
"Cookies", L
"Cookies", IDS_COOKIES
, FALSE
, TRUE
, TRUE
},
69 {NULL
, NULL
, -1, FALSE
, FALSE
, FALSE
}
74 CommonShellFolders
[] =
76 {L
"Common AppData", L
"Application Data", IDS_APPDATA
, TRUE
, TRUE
, TRUE
},
77 {L
"Common Desktop", L
"Desktop", IDS_DESKTOP
, FALSE
, TRUE
, TRUE
},
78 {L
"Common Favorites", L
"Favorites", IDS_FAVORITES
, FALSE
, TRUE
, TRUE
},
79 {L
"Common Start Menu", L
"Start Menu", IDS_STARTMENU
, FALSE
, TRUE
, TRUE
},
80 {L
"Common Programs", L
"Start Menu\\Programs", IDS_PROGRAMS
, FALSE
, TRUE
, TRUE
},
81 {L
"Common Administrative Tools", L
"Start Menu\\Programs\\Administrative Tools", IDS_ADMINTOOLS
, FALSE
, TRUE
, FALSE
},
82 {L
"Common Startup", L
"Start Menu\\Programs\\Startup", IDS_STARTUP
, FALSE
, TRUE
, TRUE
},
83 {L
"Common Templates", L
"Templates", IDS_TEMPLATES
, TRUE
, TRUE
, TRUE
},
84 {L
"Common Documents", L
"My Documents", IDS_MYDOCUMENTS
, FALSE
, TRUE
, TRUE
},
85 {L
"CommonPictures", L
"My Documents\\My Pictures", IDS_MYPICTURES
, FALSE
, TRUE
, TRUE
},
86 {L
"CommonMusic", L
"My Documents\\My Music", IDS_MYMUSIC
, FALSE
, TRUE
, TRUE
},
87 {L
"CommonVideo", L
"My Documents\\My Videos", IDS_MYVIDEOS
, FALSE
, TRUE
, TRUE
},
88 {NULL
, NULL
, -1, FALSE
, FALSE
, FALSE
}
93 DebugPrint(char* fmt
,...)
99 vsprintf(buffer
, fmt
, ap
);
102 OutputDebugStringA(buffer
);
107 InitializeProfiles (VOID
)
109 WCHAR szProfilesPath
[MAX_PATH
];
110 WCHAR szProfilePath
[MAX_PATH
];
111 WCHAR szBuffer
[MAX_PATH
];
113 PFOLDERDATA lpFolderData
;
117 /* Load profiles directory path */
118 if (!LoadStringW(hInstance
,
123 DPRINT1("Error: %lu\n", GetLastError());
127 Error
= RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
128 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
132 if (Error
!= ERROR_SUCCESS
)
134 DPRINT1("Error: %lu\n", Error
);
135 SetLastError((DWORD
)Error
);
139 /* Store profiles directory path */
140 dwLength
= (wcslen (szBuffer
) + 1) * sizeof(WCHAR
);
141 Error
= RegSetValueExW (hKey
,
142 L
"ProfilesDirectory",
147 if (Error
!= ERROR_SUCCESS
)
149 DPRINT1("Error: %lu\n", Error
);
151 SetLastError((DWORD
)Error
);
156 if (!ExpandEnvironmentStringsW (szBuffer
,
160 DPRINT1("Error: %lu\n", GetLastError());
165 /* Create profiles directory */
166 if (!CreateDirectoryW (szProfilesPath
, NULL
))
168 if (GetLastError () != ERROR_ALREADY_EXISTS
)
170 DPRINT1("Error: %lu\n", GetLastError());
176 /* Set 'DefaultUserProfile' value */
177 wcscpy (szBuffer
, L
"Default User");
178 if (!AppendSystemPostfix (szBuffer
, MAX_PATH
))
180 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
185 dwLength
= (wcslen (szBuffer
) + 1) * sizeof(WCHAR
);
186 Error
= RegSetValueExW (hKey
,
187 L
"DefaultUserProfile",
192 if (Error
!= ERROR_SUCCESS
)
194 DPRINT1("Error: %lu\n", Error
);
196 SetLastError((DWORD
)Error
);
202 /* Create 'Default User' profile directory */
203 wcscpy (szProfilePath
, szProfilesPath
);
204 wcscat (szProfilePath
, L
"\\");
205 wcscat (szProfilePath
, szBuffer
);
206 if (!CreateDirectoryW (szProfilePath
, NULL
))
208 if (GetLastError () != ERROR_ALREADY_EXISTS
)
210 DPRINT1("Error: %lu\n", GetLastError());
215 /* Set current user profile */
216 SetEnvironmentVariableW(L
"USERPROFILE", szProfilePath
);
218 /* Create 'Default User' subdirectories */
219 /* FIXME: Get these paths from the registry */
220 lpFolderData
= &UserShellFolders
[0];
221 while (lpFolderData
->lpValueName
!= NULL
)
223 wcscpy(szBuffer
, szProfilePath
);
224 wcscat(szBuffer
, L
"\\");
226 /* Append the folder name */
227 dwLength
= wcslen(szBuffer
);
228 if (!LoadStringW(hInstance
,
231 MAX_PATH
- dwLength
))
233 /* Use the default name instead */
234 wcscat(szBuffer
, lpFolderData
->lpPath
);
237 if (!CreateDirectoryW(szBuffer
, NULL
))
239 if (GetLastError() != ERROR_ALREADY_EXISTS
)
241 DPRINT1("Error: %lu\n", GetLastError());
246 if (lpFolderData
->bHidden
== TRUE
)
248 SetFileAttributesW(szBuffer
,
249 FILE_ATTRIBUTE_HIDDEN
);
255 /* Set default 'Shell Folders' values */
256 Error
= RegOpenKeyExW(HKEY_USERS
,
257 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
261 if (Error
!= ERROR_SUCCESS
)
263 DPRINT1("Error: %lu\n", Error
);
264 SetLastError((DWORD
)Error
);
268 lpFolderData
= &UserShellFolders
[0];
269 while (lpFolderData
->lpValueName
!= NULL
)
271 if (lpFolderData
->bShellFolder
)
273 wcscpy(szBuffer
, szProfilePath
);
274 wcscat(szBuffer
, L
"\\");
276 /* Append the folder name */
277 dwLength
= wcslen(szBuffer
);
278 if (!LoadStringW(hInstance
,
281 MAX_PATH
- dwLength
))
283 /* Use the default name instead */
284 wcscat(szBuffer
, lpFolderData
->lpPath
);
287 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
288 Error
= RegSetValueExW(hKey
,
289 lpFolderData
->lpValueName
,
294 if (Error
!= ERROR_SUCCESS
)
296 DPRINT1("Error: %lu\n", Error
);
298 SetLastError((DWORD
)Error
);
306 /* Set 'Fonts' folder path */
307 GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
308 wcscat(szBuffer
, L
"\\media\\fonts");
310 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
311 Error
= RegSetValueExW(hKey
,
317 if (Error
!= ERROR_SUCCESS
)
319 DPRINT1("Error: %lu\n", Error
);
321 SetLastError((DWORD
)Error
);
327 /* Set default 'User Shell Folders' values */
328 Error
= RegOpenKeyExW(HKEY_USERS
,
329 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
333 if (Error
!= ERROR_SUCCESS
)
335 DPRINT1("Error: %lu\n", Error
);
336 SetLastError((DWORD
)Error
);
340 lpFolderData
= &UserShellFolders
[0];
341 while (lpFolderData
->lpValueName
!= NULL
)
343 if (lpFolderData
->bUserShellFolder
)
345 wcscpy(szBuffer
, L
"%USERPROFILE%\\");
347 /* Append the folder name */
348 dwLength
= wcslen(szBuffer
);
349 if (!LoadStringW(hInstance
,
352 MAX_PATH
- dwLength
))
354 /* Use the default name instead */
355 wcscat(szBuffer
, lpFolderData
->lpPath
);
358 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
359 Error
= RegSetValueExW(hKey
,
360 lpFolderData
->lpValueName
,
365 if (Error
!= ERROR_SUCCESS
)
367 DPRINT1("Error: %lu\n", Error
);
369 SetLastError((DWORD
)Error
);
380 /* Set 'AllUsersProfile' value */
381 wcscpy(szBuffer
, L
"All Users");
382 if (!AppendSystemPostfix(szBuffer
, MAX_PATH
))
384 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
388 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
389 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
393 if (Error
!= ERROR_SUCCESS
)
395 DPRINT1("Error: %lu\n", Error
);
396 SetLastError((DWORD
)Error
);
400 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
401 Error
= RegSetValueExW(hKey
,
407 if (Error
!= ERROR_SUCCESS
)
409 DPRINT1("Error: %lu\n", Error
);
411 SetLastError((DWORD
)Error
);
418 /* Create 'All Users' profile directory */
419 wcscpy(szProfilePath
, szProfilesPath
);
420 wcscat(szProfilePath
, L
"\\");
421 wcscat(szProfilePath
, szBuffer
);
422 if (!CreateDirectoryW(szProfilePath
, NULL
))
424 if (GetLastError() != ERROR_ALREADY_EXISTS
)
426 DPRINT1("Error: %lu\n", GetLastError());
431 /* Set 'All Users' profile */
432 SetEnvironmentVariableW(L
"ALLUSERSPROFILE", szProfilePath
);
434 /* Create 'All Users' subdirectories */
435 /* FIXME: Take these paths from the registry */
436 lpFolderData
= &CommonShellFolders
[0];
437 while (lpFolderData
->lpValueName
!= NULL
)
439 wcscpy(szBuffer
, szProfilePath
);
440 wcscat(szBuffer
, L
"\\");
442 /* Append the folder name */
443 dwLength
= wcslen(szBuffer
);
444 if (!LoadStringW(hInstance
,
447 MAX_PATH
- dwLength
))
449 /* Use the default name instead */
450 wcscat(szBuffer
, lpFolderData
->lpPath
);
453 if (!CreateDirectoryW(szBuffer
, NULL
))
455 if (GetLastError() != ERROR_ALREADY_EXISTS
)
457 DPRINT1("Error: %lu\n", GetLastError());
462 if (lpFolderData
->bHidden
)
464 SetFileAttributesW(szBuffer
,
465 FILE_ATTRIBUTE_HIDDEN
);
471 /* Set common 'Shell Folders' values */
472 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
473 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
477 if (Error
!= ERROR_SUCCESS
)
479 DPRINT1("Error: %lu\n", Error
);
480 SetLastError((DWORD
)Error
);
484 lpFolderData
= &CommonShellFolders
[0];
485 while (lpFolderData
->lpValueName
!= NULL
)
487 if (lpFolderData
->bShellFolder
)
489 wcscpy(szBuffer
, szProfilePath
);
490 wcscat(szBuffer
, L
"\\");
492 /* Append the folder name */
493 dwLength
= wcslen(szBuffer
);
494 if (!LoadStringW(hInstance
,
497 MAX_PATH
- dwLength
))
499 /* Use the default name instead */
500 wcscat(szBuffer
, lpFolderData
->lpPath
);
503 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
504 Error
= RegSetValueExW(hKey
,
505 lpFolderData
->lpValueName
,
510 if (Error
!= ERROR_SUCCESS
)
512 DPRINT1("Error: %lu\n", Error
);
514 SetLastError((DWORD
)Error
);
524 /* Set common 'User Shell Folders' values */
525 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
526 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
530 if (Error
!= ERROR_SUCCESS
)
532 DPRINT1("Error: %lu\n", Error
);
533 SetLastError((DWORD
)Error
);
537 lpFolderData
= &CommonShellFolders
[0];
538 while (lpFolderData
->lpValueName
!= NULL
)
540 if (lpFolderData
->bUserShellFolder
)
542 wcscpy(szBuffer
, L
"%ALLUSERSPROFILE%\\");
544 /* Append the folder name */
545 dwLength
= wcslen(szBuffer
);
546 if (!LoadStringW(hInstance
,
549 MAX_PATH
- dwLength
))
551 /* Use the default name instead */
552 wcscat(szBuffer
, lpFolderData
->lpPath
);
555 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
556 Error
= RegSetValueExW(hKey
,
557 lpFolderData
->lpValueName
,
562 if (Error
!= ERROR_SUCCESS
)
564 DPRINT1("Error: %lu\n", Error
);
566 SetLastError((DWORD
)Error
);
576 /* Load 'Program Files' location */
577 if (!LoadStringW(hInstance
,
582 DPRINT1("Error: %lu\n", GetLastError());
587 if (!ExpandEnvironmentStringsW (szBuffer
,
591 DPRINT1("Error: %lu\n", GetLastError());
597 Error
= RegOpenKeyExW (HKEY_LOCAL_MACHINE
,
598 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
602 if (Error
!= ERROR_SUCCESS
)
604 DPRINT1("Error: %lu\n", Error
);
605 SetLastError((DWORD
)Error
);
609 dwLength
= (wcslen (szProfilesPath
) + 1) * sizeof(WCHAR
);
610 Error
= RegSetValueExW (hKey
,
614 (LPBYTE
)szProfilesPath
,
616 if (Error
!= ERROR_SUCCESS
)
618 DPRINT1("Error: %lu\n", Error
);
620 SetLastError((DWORD
)Error
);
626 /* Create directory */
627 if (!CreateDirectoryW (szProfilesPath
, NULL
))
629 if (GetLastError () != ERROR_ALREADY_EXISTS
)
631 DPRINT1("Error: %lu\n", GetLastError());
644 UpdateUsersShellFolderSettings(LPCWSTR lpUserProfilePath
,
647 WCHAR szBuffer
[MAX_PATH
];
649 PFOLDERDATA lpFolderData
;
653 DPRINT("UpdateUsersShellFolderSettings() called\n");
655 DPRINT("User profile path: %S\n", lpUserProfilePath
);
657 Error
= RegOpenKeyExW(hUserKey
,
658 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
662 if (Error
!= ERROR_SUCCESS
)
664 DPRINT1("Error: %lu\n", Error
);
665 SetLastError((DWORD
)Error
);
669 lpFolderData
= &UserShellFolders
[0];
670 while (lpFolderData
->lpValueName
!= NULL
)
672 if (lpFolderData
->bShellFolder
)
674 wcscpy(szBuffer
, lpUserProfilePath
);
675 wcscat(szBuffer
, L
"\\");
677 /* Append the folder name */
678 dwLength
= wcslen(szBuffer
);
679 if (!LoadStringW(hInstance
,
682 MAX_PATH
- dwLength
))
684 /* Use the default name instead */
685 wcscat(szBuffer
, lpFolderData
->lpPath
);
688 DPRINT("%S: %S\n", lpFolderData
->lpValueName
, szBuffer
);
690 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
691 Error
= RegSetValueExW(hFoldersKey
,
692 lpFolderData
->lpValueName
,
697 if (Error
!= ERROR_SUCCESS
)
699 DPRINT1("Error: %lu\n", Error
);
700 RegCloseKey(hFoldersKey
);
701 SetLastError((DWORD
)Error
);
709 RegCloseKey(hFoldersKey
);
711 DPRINT("UpdateUsersShellFolderSettings() done\n");