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
);
108 InitializeProfiles(VOID
)
110 WCHAR szProfilesPath
[MAX_PATH
];
111 WCHAR szProfilePath
[MAX_PATH
];
112 WCHAR szCommonFilesDirPath
[MAX_PATH
];
113 WCHAR szBuffer
[MAX_PATH
];
115 PFOLDERDATA lpFolderData
;
119 DPRINT("InitializeProfiles()\n");
121 /* Load profiles directory path */
122 if (!LoadStringW(hInstance
,
127 DPRINT1("Error: %lu\n", GetLastError());
131 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
132 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
136 if (Error
!= ERROR_SUCCESS
)
138 DPRINT1("Error: %lu\n", Error
);
139 SetLastError((DWORD
)Error
);
143 /* Store profiles directory path */
144 dwLength
= (wcslen (szBuffer
) + 1) * sizeof(WCHAR
);
145 Error
= RegSetValueExW(hKey
,
146 L
"ProfilesDirectory",
151 if (Error
!= ERROR_SUCCESS
)
153 DPRINT1("Error: %lu\n", Error
);
155 SetLastError((DWORD
)Error
);
160 if (!ExpandEnvironmentStringsW(szBuffer
,
164 DPRINT1("Error: %lu\n", GetLastError());
169 /* Create profiles directory */
170 if (!CreateDirectoryW(szProfilesPath
, NULL
))
172 if (GetLastError() != ERROR_ALREADY_EXISTS
)
174 DPRINT1("Error: %lu\n", GetLastError());
180 /* Set 'DefaultUserProfile' value */
181 wcscpy(szBuffer
, L
"Default User");
182 if (!AppendSystemPostfix(szBuffer
, MAX_PATH
))
184 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
189 dwLength
= (wcslen (szBuffer
) + 1) * sizeof(WCHAR
);
190 Error
= RegSetValueExW(hKey
,
191 L
"DefaultUserProfile",
196 if (Error
!= ERROR_SUCCESS
)
198 DPRINT1("Error: %lu\n", Error
);
200 SetLastError((DWORD
)Error
);
206 /* Create 'Default User' profile directory */
207 wcscpy(szProfilePath
, szProfilesPath
);
208 wcscat(szProfilePath
, L
"\\");
209 wcscat(szProfilePath
, szBuffer
);
210 if (!CreateDirectoryW (szProfilePath
, NULL
))
212 if (GetLastError() != ERROR_ALREADY_EXISTS
)
214 DPRINT1("Error: %lu\n", GetLastError());
219 /* Set current user profile */
220 SetEnvironmentVariableW(L
"USERPROFILE", szProfilePath
);
222 /* Create 'Default User' subdirectories */
223 /* FIXME: Get these paths from the registry */
224 lpFolderData
= &UserShellFolders
[0];
225 while (lpFolderData
->lpValueName
!= NULL
)
227 wcscpy(szBuffer
, szProfilePath
);
228 wcscat(szBuffer
, L
"\\");
230 /* Append the folder name */
231 dwLength
= wcslen(szBuffer
);
232 if (!LoadStringW(hInstance
,
235 MAX_PATH
- dwLength
))
237 /* Use the default name instead */
238 wcscat(szBuffer
, lpFolderData
->lpPath
);
241 if (!CreateDirectoryW(szBuffer
, NULL
))
243 if (GetLastError() != ERROR_ALREADY_EXISTS
)
245 DPRINT1("Error: %lu\n", GetLastError());
250 if (lpFolderData
->bHidden
== TRUE
)
252 SetFileAttributesW(szBuffer
,
253 FILE_ATTRIBUTE_HIDDEN
);
259 /* Set default 'Shell Folders' values */
260 Error
= RegOpenKeyExW(HKEY_USERS
,
261 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
265 if (Error
!= ERROR_SUCCESS
)
267 DPRINT1("Error: %lu\n", Error
);
268 SetLastError((DWORD
)Error
);
272 lpFolderData
= &UserShellFolders
[0];
273 while (lpFolderData
->lpValueName
!= NULL
)
275 if (lpFolderData
->bShellFolder
)
277 wcscpy(szBuffer
, szProfilePath
);
278 wcscat(szBuffer
, L
"\\");
280 /* Append the folder name */
281 dwLength
= wcslen(szBuffer
);
282 if (!LoadStringW(hInstance
,
285 MAX_PATH
- dwLength
))
287 /* Use the default name instead */
288 wcscat(szBuffer
, lpFolderData
->lpPath
);
291 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
292 Error
= RegSetValueExW(hKey
,
293 lpFolderData
->lpValueName
,
298 if (Error
!= ERROR_SUCCESS
)
300 DPRINT1("Error: %lu\n", Error
);
302 SetLastError((DWORD
)Error
);
310 /* Set 'Fonts' folder path */
311 GetWindowsDirectoryW(szBuffer
, MAX_PATH
);
312 wcscat(szBuffer
, L
"\\fonts");
314 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
315 Error
= RegSetValueExW(hKey
,
321 if (Error
!= ERROR_SUCCESS
)
323 DPRINT1("Error: %lu\n", Error
);
325 SetLastError((DWORD
)Error
);
331 /* Set default 'User Shell Folders' values */
332 Error
= RegOpenKeyExW(HKEY_USERS
,
333 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
337 if (Error
!= ERROR_SUCCESS
)
339 DPRINT1("Error: %lu\n", Error
);
340 SetLastError((DWORD
)Error
);
344 lpFolderData
= &UserShellFolders
[0];
345 while (lpFolderData
->lpValueName
!= NULL
)
347 if (lpFolderData
->bUserShellFolder
)
349 wcscpy(szBuffer
, L
"%USERPROFILE%\\");
351 /* Append the folder name */
352 dwLength
= wcslen(szBuffer
);
353 if (!LoadStringW(hInstance
,
356 MAX_PATH
- dwLength
))
358 /* Use the default name instead */
359 wcscat(szBuffer
, lpFolderData
->lpPath
);
362 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
363 Error
= RegSetValueExW(hKey
,
364 lpFolderData
->lpValueName
,
369 if (Error
!= ERROR_SUCCESS
)
371 DPRINT1("Error: %lu\n", Error
);
373 SetLastError((DWORD
)Error
);
383 /* Set 'AllUsersProfile' value */
384 wcscpy(szBuffer
, L
"All Users");
385 if (!AppendSystemPostfix(szBuffer
, MAX_PATH
))
387 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
391 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
392 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
396 if (Error
!= ERROR_SUCCESS
)
398 DPRINT1("Error: %lu\n", Error
);
399 SetLastError((DWORD
)Error
);
403 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
404 Error
= RegSetValueExW(hKey
,
410 if (Error
!= ERROR_SUCCESS
)
412 DPRINT1("Error: %lu\n", Error
);
414 SetLastError((DWORD
)Error
);
420 /* Create 'All Users' profile directory */
421 wcscpy(szProfilePath
, szProfilesPath
);
422 wcscat(szProfilePath
, L
"\\");
423 wcscat(szProfilePath
, szBuffer
);
424 if (!CreateDirectoryW(szProfilePath
, NULL
))
426 if (GetLastError() != ERROR_ALREADY_EXISTS
)
428 DPRINT1("Error: %lu\n", GetLastError());
433 /* Set 'All Users' profile */
434 SetEnvironmentVariableW(L
"ALLUSERSPROFILE", szProfilePath
);
436 /* Create 'All Users' subdirectories */
437 /* FIXME: Take these paths from the registry */
438 lpFolderData
= &CommonShellFolders
[0];
439 while (lpFolderData
->lpValueName
!= NULL
)
441 wcscpy(szBuffer
, szProfilePath
);
442 wcscat(szBuffer
, L
"\\");
444 /* Append the folder name */
445 dwLength
= wcslen(szBuffer
);
446 if (!LoadStringW(hInstance
,
449 MAX_PATH
- dwLength
))
451 /* Use the default name instead */
452 wcscat(szBuffer
, lpFolderData
->lpPath
);
455 if (!CreateDirectoryW(szBuffer
, NULL
))
457 if (GetLastError() != ERROR_ALREADY_EXISTS
)
459 DPRINT1("Error: %lu\n", GetLastError());
464 if (lpFolderData
->bHidden
)
466 SetFileAttributesW(szBuffer
,
467 FILE_ATTRIBUTE_HIDDEN
);
473 /* Set common 'Shell Folders' values */
474 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
475 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
479 if (Error
!= ERROR_SUCCESS
)
481 DPRINT1("Error: %lu\n", Error
);
482 SetLastError((DWORD
)Error
);
486 lpFolderData
= &CommonShellFolders
[0];
487 while (lpFolderData
->lpValueName
!= NULL
)
489 if (lpFolderData
->bShellFolder
)
491 wcscpy(szBuffer
, szProfilePath
);
492 wcscat(szBuffer
, L
"\\");
494 /* Append the folder name */
495 dwLength
= wcslen(szBuffer
);
496 if (!LoadStringW(hInstance
,
499 MAX_PATH
- dwLength
))
501 /* Use the default name instead */
502 wcscat(szBuffer
, lpFolderData
->lpPath
);
505 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
506 Error
= RegSetValueExW(hKey
,
507 lpFolderData
->lpValueName
,
512 if (Error
!= ERROR_SUCCESS
)
514 DPRINT1("Error: %lu\n", Error
);
516 SetLastError((DWORD
)Error
);
526 /* Set common 'User Shell Folders' values */
527 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
528 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
532 if (Error
!= ERROR_SUCCESS
)
534 DPRINT1("Error: %lu\n", Error
);
535 SetLastError((DWORD
)Error
);
539 lpFolderData
= &CommonShellFolders
[0];
540 while (lpFolderData
->lpValueName
!= NULL
)
542 if (lpFolderData
->bUserShellFolder
)
544 wcscpy(szBuffer
, L
"%ALLUSERSPROFILE%\\");
546 /* Append the folder name */
547 dwLength
= wcslen(szBuffer
);
548 if (!LoadStringW(hInstance
,
551 MAX_PATH
- dwLength
))
553 /* Use the default name instead */
554 wcscat(szBuffer
, lpFolderData
->lpPath
);
557 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
558 Error
= RegSetValueExW(hKey
,
559 lpFolderData
->lpValueName
,
564 if (Error
!= ERROR_SUCCESS
)
566 DPRINT1("Error: %lu\n", Error
);
568 SetLastError((DWORD
)Error
);
578 /* Load 'Program Files' location */
579 if (!LoadStringW(hInstance
,
584 DPRINT1("Error: %lu\n", GetLastError());
588 if (!LoadStringW(hInstance
,
590 szCommonFilesDirPath
,
593 DPRINT1("Warning: %lu\n", GetLastError());
597 if (!ExpandEnvironmentStringsW(szBuffer
,
601 DPRINT1("Error: %lu\n", GetLastError());
605 wcscpy(szBuffer
, szProfilesPath
);
606 wcscat(szBuffer
, L
"\\");
607 wcscat(szBuffer
, szCommonFilesDirPath
);
609 if (!ExpandEnvironmentStringsW(szBuffer
,
610 szCommonFilesDirPath
,
613 DPRINT1("Warning: %lu\n", GetLastError());
617 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
618 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
622 if (Error
!= ERROR_SUCCESS
)
624 DPRINT1("Error: %lu\n", Error
);
625 SetLastError((DWORD
)Error
);
629 dwLength
= (wcslen (szProfilesPath
) + 1) * sizeof(WCHAR
);
630 Error
= RegSetValueExW(hKey
,
634 (LPBYTE
)szProfilesPath
,
636 if (Error
!= ERROR_SUCCESS
)
638 DPRINT1("Error: %lu\n", Error
);
640 SetLastError((DWORD
)Error
);
644 dwLength
= (wcslen(szCommonFilesDirPath
) + 1) * sizeof(WCHAR
);
645 Error
= RegSetValueExW(hKey
,
649 (LPBYTE
)szCommonFilesDirPath
,
651 if (Error
!= ERROR_SUCCESS
)
653 DPRINT1("Warning: %lu\n", Error
);
658 /* Create directory */
659 if (!CreateDirectoryW(szProfilesPath
, NULL
))
661 if (GetLastError () != ERROR_ALREADY_EXISTS
)
663 DPRINT1("Error: %lu\n", GetLastError());
668 /* Create directory */
669 if (!CreateDirectoryW(szCommonFilesDirPath
, NULL
))
671 if (GetLastError () != ERROR_ALREADY_EXISTS
)
673 DPRINT1("Warning: %lu\n", GetLastError());
684 UpdateUsersShellFolderSettings(LPCWSTR lpUserProfilePath
,
687 WCHAR szBuffer
[MAX_PATH
];
689 PFOLDERDATA lpFolderData
;
693 DPRINT("UpdateUsersShellFolderSettings() called\n");
695 DPRINT("User profile path: %S\n", lpUserProfilePath
);
697 Error
= RegOpenKeyExW(hUserKey
,
698 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
702 if (Error
!= ERROR_SUCCESS
)
704 DPRINT1("Error: %lu\n", Error
);
705 SetLastError((DWORD
)Error
);
709 lpFolderData
= &UserShellFolders
[0];
710 while (lpFolderData
->lpValueName
!= NULL
)
712 if (lpFolderData
->bShellFolder
)
714 wcscpy(szBuffer
, lpUserProfilePath
);
715 wcscat(szBuffer
, L
"\\");
717 /* Append the folder name */
718 dwLength
= wcslen(szBuffer
);
719 if (!LoadStringW(hInstance
,
722 MAX_PATH
- dwLength
))
724 /* Use the default name instead */
725 wcscat(szBuffer
, lpFolderData
->lpPath
);
728 DPRINT("%S: %S\n", lpFolderData
->lpValueName
, szBuffer
);
730 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
731 Error
= RegSetValueExW(hFoldersKey
,
732 lpFolderData
->lpValueName
,
737 if (Error
!= ERROR_SUCCESS
)
739 DPRINT1("Error: %lu\n", Error
);
740 RegCloseKey(hFoldersKey
);
741 SetLastError((DWORD
)Error
);
749 RegCloseKey(hFoldersKey
);
751 DPRINT("UpdateUsersShellFolderSettings() done\n");