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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * FILE: dll/win32/userenv/setup.c
23 * PURPOSE: Profile setup functions
24 * PROGRAMMER: Eric Kohl
32 #include "resources.h"
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
,
125 ARRAYSIZE(szBuffer
)))
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
);
144 if (!ExpandEnvironmentStringsW(szBuffer
,
146 ARRAYSIZE(szProfilesPath
)))
148 DPRINT1("Error: %lu\n", GetLastError());
153 /* Create profiles directory */
154 if (!CreateDirectoryW(szProfilesPath
, NULL
))
156 if (GetLastError() != ERROR_ALREADY_EXISTS
)
158 DPRINT1("Error: %lu\n", GetLastError());
164 /* Store the profiles directory path in the registry */
165 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
166 Error
= RegSetValueExW(hKey
,
167 L
"ProfilesDirectory",
172 if (Error
!= ERROR_SUCCESS
)
174 DPRINT1("Error: %lu\n", Error
);
176 SetLastError((DWORD
)Error
);
180 /* Set 'DefaultUserProfile' value */
181 wcscpy(szBuffer
, L
"Default User");
183 /* Create Default User profile directory path */
184 wcscpy(szProfilePath
, szProfilesPath
);
185 wcscat(szProfilePath
, L
"\\");
186 wcscat(szProfilePath
, szBuffer
);
188 /* Attempt default user directory creation */
189 if (!CreateDirectoryW(szProfilePath
, NULL
))
191 if (GetLastError() != ERROR_ALREADY_EXISTS
)
193 DPRINT1("Error: %lu\n", GetLastError());
198 /* Directory existed, let's try to append the postfix */
199 if (!AppendSystemPostfix(szBuffer
, ARRAYSIZE(szBuffer
)))
201 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
206 /* Create Default User profile directory path again */
207 wcscpy(szProfilePath
, szProfilesPath
);
208 wcscat(szProfilePath
, L
"\\");
209 wcscat(szProfilePath
, szBuffer
);
211 /* Attempt creation again with appended postfix */
212 if (!CreateDirectoryW(szProfilePath
, NULL
))
214 if (GetLastError() != ERROR_ALREADY_EXISTS
)
216 DPRINT1("Error: %lu\n", GetLastError());
223 /* Store the default user profile path in the registry */
224 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
225 Error
= RegSetValueExW(hKey
,
226 L
"DefaultUserProfile",
231 if (Error
!= ERROR_SUCCESS
)
233 DPRINT1("Error: %lu\n", Error
);
235 SetLastError((DWORD
)Error
);
241 /* Set current user profile */
242 SetEnvironmentVariableW(L
"USERPROFILE", szProfilePath
);
244 /* Create 'Default User' subdirectories */
245 /* FIXME: Get these paths from the registry */
246 lpFolderData
= &UserShellFolders
[0];
247 while (lpFolderData
->lpValueName
!= NULL
)
249 wcscpy(szBuffer
, szProfilePath
);
250 wcscat(szBuffer
, L
"\\");
252 /* Append the folder name */
253 dwLength
= wcslen(szBuffer
);
254 if (!LoadStringW(hInstance
,
257 ARRAYSIZE(szBuffer
) - dwLength
))
259 /* Use the default name instead */
260 wcscat(szBuffer
, lpFolderData
->lpPath
);
263 if (!CreateDirectoryW(szBuffer
, NULL
))
265 if (GetLastError() != ERROR_ALREADY_EXISTS
)
267 DPRINT1("Error: %lu\n", GetLastError());
272 if (lpFolderData
->bHidden
== TRUE
)
274 SetFileAttributesW(szBuffer
,
275 FILE_ATTRIBUTE_HIDDEN
);
281 /* Set default 'Shell Folders' values */
282 Error
= RegOpenKeyExW(HKEY_USERS
,
283 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
287 if (Error
!= ERROR_SUCCESS
)
289 DPRINT1("Error: %lu\n", Error
);
290 SetLastError((DWORD
)Error
);
294 lpFolderData
= &UserShellFolders
[0];
295 while (lpFolderData
->lpValueName
!= NULL
)
297 if (lpFolderData
->bShellFolder
)
299 wcscpy(szBuffer
, szProfilePath
);
300 wcscat(szBuffer
, L
"\\");
302 /* Append the folder name */
303 dwLength
= wcslen(szBuffer
);
304 if (!LoadStringW(hInstance
,
307 ARRAYSIZE(szBuffer
) - dwLength
))
309 /* Use the default name instead */
310 wcscat(szBuffer
, lpFolderData
->lpPath
);
313 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
314 Error
= RegSetValueExW(hKey
,
315 lpFolderData
->lpValueName
,
320 if (Error
!= ERROR_SUCCESS
)
322 DPRINT1("Error: %lu\n", Error
);
324 SetLastError((DWORD
)Error
);
332 /* Set 'Fonts' folder path */
333 GetWindowsDirectoryW(szBuffer
, ARRAYSIZE(szBuffer
));
334 wcscat(szBuffer
, L
"\\Fonts");
336 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
337 Error
= RegSetValueExW(hKey
,
343 if (Error
!= ERROR_SUCCESS
)
345 DPRINT1("Error: %lu\n", Error
);
347 SetLastError((DWORD
)Error
);
353 /* Set default 'User Shell Folders' values */
354 Error
= RegOpenKeyExW(HKEY_USERS
,
355 L
".Default\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
359 if (Error
!= ERROR_SUCCESS
)
361 DPRINT1("Error: %lu\n", Error
);
362 SetLastError((DWORD
)Error
);
366 lpFolderData
= &UserShellFolders
[0];
367 while (lpFolderData
->lpValueName
!= NULL
)
369 if (lpFolderData
->bUserShellFolder
)
371 wcscpy(szBuffer
, L
"%USERPROFILE%\\");
373 /* Append the folder name */
374 dwLength
= wcslen(szBuffer
);
375 if (!LoadStringW(hInstance
,
378 ARRAYSIZE(szBuffer
) - dwLength
))
380 /* Use the default name instead */
381 wcscat(szBuffer
, lpFolderData
->lpPath
);
384 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
385 Error
= RegSetValueExW(hKey
,
386 lpFolderData
->lpValueName
,
391 if (Error
!= ERROR_SUCCESS
)
393 DPRINT1("Error: %lu\n", Error
);
395 SetLastError((DWORD
)Error
);
405 /* Set 'AllUsersProfile' value */
406 wcscpy(szBuffer
, L
"All Users");
408 /* Create 'All Users' profile directory path */
409 wcscpy(szProfilePath
, szProfilesPath
);
410 wcscat(szProfilePath
, L
"\\");
411 wcscat(szProfilePath
, szBuffer
);
413 /* Attempt 'All Users' directory creation */
414 if (!CreateDirectoryW(szProfilePath
, NULL
))
416 if (GetLastError() != ERROR_ALREADY_EXISTS
)
418 DPRINT1("Error: %lu\n", GetLastError());
422 /* Directory existed, let's try to append the postfix */
423 if (!AppendSystemPostfix(szBuffer
, ARRAYSIZE(szBuffer
)))
425 DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
429 /* Attempt again creation with appended postfix */
430 wcscpy(szProfilePath
, szProfilesPath
);
431 wcscat(szProfilePath
, L
"\\");
432 wcscat(szProfilePath
, szBuffer
);
434 if (!CreateDirectoryW(szProfilePath
, NULL
))
436 if (GetLastError() != ERROR_ALREADY_EXISTS
)
438 DPRINT1("Error: %lu\n", GetLastError());
444 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
445 L
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
449 if (Error
!= ERROR_SUCCESS
)
451 DPRINT1("Error: %lu\n", Error
);
452 SetLastError((DWORD
)Error
);
456 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
457 Error
= RegSetValueExW(hKey
,
466 if (Error
!= ERROR_SUCCESS
)
468 DPRINT1("Error: %lu\n", Error
);
469 SetLastError((DWORD
)Error
);
473 /* Set 'All Users' profile */
474 SetEnvironmentVariableW(L
"ALLUSERSPROFILE", szProfilePath
);
476 /* Create 'All Users' subdirectories */
477 /* FIXME: Take these paths from the registry */
478 lpFolderData
= &CommonShellFolders
[0];
479 while (lpFolderData
->lpValueName
!= NULL
)
481 wcscpy(szBuffer
, szProfilePath
);
482 wcscat(szBuffer
, L
"\\");
484 /* Append the folder name */
485 dwLength
= wcslen(szBuffer
);
486 if (!LoadStringW(hInstance
,
489 ARRAYSIZE(szBuffer
) - dwLength
))
491 /* Use the default name instead */
492 wcscat(szBuffer
, lpFolderData
->lpPath
);
495 if (!CreateDirectoryW(szBuffer
, NULL
))
497 if (GetLastError() != ERROR_ALREADY_EXISTS
)
499 DPRINT1("Error: %lu\n", GetLastError());
504 if (lpFolderData
->bHidden
)
506 SetFileAttributesW(szBuffer
,
507 FILE_ATTRIBUTE_HIDDEN
);
513 /* Set common 'Shell Folders' values */
514 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
515 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
519 if (Error
!= ERROR_SUCCESS
)
521 DPRINT1("Error: %lu\n", Error
);
522 SetLastError((DWORD
)Error
);
526 lpFolderData
= &CommonShellFolders
[0];
527 while (lpFolderData
->lpValueName
!= NULL
)
529 if (lpFolderData
->bShellFolder
)
531 wcscpy(szBuffer
, szProfilePath
);
532 wcscat(szBuffer
, L
"\\");
534 /* Append the folder name */
535 dwLength
= wcslen(szBuffer
);
536 if (!LoadStringW(hInstance
,
539 ARRAYSIZE(szBuffer
) - dwLength
))
541 /* Use the default name instead */
542 wcscat(szBuffer
, lpFolderData
->lpPath
);
545 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
546 Error
= RegSetValueExW(hKey
,
547 lpFolderData
->lpValueName
,
552 if (Error
!= ERROR_SUCCESS
)
554 DPRINT1("Error: %lu\n", Error
);
556 SetLastError((DWORD
)Error
);
566 /* Set common 'User Shell Folders' values */
567 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
568 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders",
572 if (Error
!= ERROR_SUCCESS
)
574 DPRINT1("Error: %lu\n", Error
);
575 SetLastError((DWORD
)Error
);
579 lpFolderData
= &CommonShellFolders
[0];
580 while (lpFolderData
->lpValueName
!= NULL
)
582 if (lpFolderData
->bUserShellFolder
)
584 wcscpy(szBuffer
, L
"%ALLUSERSPROFILE%\\");
586 /* Append the folder name */
587 dwLength
= wcslen(szBuffer
);
588 if (!LoadStringW(hInstance
,
591 ARRAYSIZE(szBuffer
) - dwLength
))
593 /* Use the default name instead */
594 wcscat(szBuffer
, lpFolderData
->lpPath
);
597 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
598 Error
= RegSetValueExW(hKey
,
599 lpFolderData
->lpValueName
,
604 if (Error
!= ERROR_SUCCESS
)
606 DPRINT1("Error: %lu\n", Error
);
608 SetLastError((DWORD
)Error
);
618 /* Load 'Program Files' location */
619 if (!LoadStringW(hInstance
,
622 ARRAYSIZE(szBuffer
)))
624 DPRINT1("Error: %lu\n", GetLastError());
628 if (!LoadStringW(hInstance
,
630 szCommonFilesDirPath
,
631 ARRAYSIZE(szCommonFilesDirPath
)))
633 DPRINT1("Warning: %lu\n", GetLastError());
637 if (!ExpandEnvironmentStringsW(szBuffer
,
639 ARRAYSIZE(szProfilesPath
)))
641 DPRINT1("Error: %lu\n", GetLastError());
645 wcscpy(szBuffer
, szProfilesPath
);
646 wcscat(szBuffer
, L
"\\");
647 wcscat(szBuffer
, szCommonFilesDirPath
);
649 if (!ExpandEnvironmentStringsW(szBuffer
,
650 szCommonFilesDirPath
,
651 ARRAYSIZE(szCommonFilesDirPath
)))
653 DPRINT1("Warning: %lu\n", GetLastError());
657 Error
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
658 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion",
662 if (Error
!= ERROR_SUCCESS
)
664 DPRINT1("Error: %lu\n", Error
);
665 SetLastError((DWORD
)Error
);
669 dwLength
= (wcslen(szProfilesPath
) + 1) * sizeof(WCHAR
);
670 Error
= RegSetValueExW(hKey
,
674 (LPBYTE
)szProfilesPath
,
676 if (Error
!= ERROR_SUCCESS
)
678 DPRINT1("Error: %lu\n", Error
);
680 SetLastError((DWORD
)Error
);
684 dwLength
= (wcslen(szCommonFilesDirPath
) + 1) * sizeof(WCHAR
);
685 Error
= RegSetValueExW(hKey
,
689 (LPBYTE
)szCommonFilesDirPath
,
691 if (Error
!= ERROR_SUCCESS
)
693 DPRINT1("Warning: %lu\n", Error
);
698 /* Create directory */
699 if (!CreateDirectoryW(szProfilesPath
, NULL
))
701 if (GetLastError() != ERROR_ALREADY_EXISTS
)
703 DPRINT1("Error: %lu\n", GetLastError());
708 /* Create directory */
709 if (!CreateDirectoryW(szCommonFilesDirPath
, NULL
))
711 if (GetLastError() != ERROR_ALREADY_EXISTS
)
713 DPRINT1("Warning: %lu\n", GetLastError());
724 UpdateUsersShellFolderSettings(LPCWSTR lpUserProfilePath
,
727 WCHAR szBuffer
[MAX_PATH
];
729 PFOLDERDATA lpFolderData
;
733 DPRINT("UpdateUsersShellFolderSettings() called\n");
735 DPRINT("User profile path: %S\n", lpUserProfilePath
);
737 Error
= RegOpenKeyExW(hUserKey
,
738 L
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
742 if (Error
!= ERROR_SUCCESS
)
744 DPRINT1("Error: %lu\n", Error
);
745 SetLastError((DWORD
)Error
);
749 lpFolderData
= &UserShellFolders
[0];
750 while (lpFolderData
->lpValueName
!= NULL
)
752 if (lpFolderData
->bShellFolder
)
754 wcscpy(szBuffer
, lpUserProfilePath
);
755 wcscat(szBuffer
, L
"\\");
757 /* Append the folder name */
758 dwLength
= wcslen(szBuffer
);
759 if (!LoadStringW(hInstance
,
762 ARRAYSIZE(szBuffer
) - dwLength
))
764 /* Use the default name instead */
765 wcscat(szBuffer
, lpFolderData
->lpPath
);
768 DPRINT("%S: %S\n", lpFolderData
->lpValueName
, szBuffer
);
770 dwLength
= (wcslen(szBuffer
) + 1) * sizeof(WCHAR
);
771 Error
= RegSetValueExW(hFoldersKey
,
772 lpFolderData
->lpValueName
,
777 if (Error
!= ERROR_SUCCESS
)
779 DPRINT1("Error: %lu\n", Error
);
780 RegCloseKey(hFoldersKey
);
781 SetLastError((DWORD
)Error
);
789 RegCloseKey(hFoldersKey
);
791 DPRINT("UpdateUsersShellFolderSettings() done\n");