📄 profile.c
字号:
/*
* ReactOS kernel
* Copyright (C) 2004 ReactOS Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id: profile.c 28314 2007-08-13 14:18:49Z hpoussin $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: lib/userenv/profile.c
* PURPOSE: User profile code
* PROGRAMMER: Eric Kohl
*/
#include <precomp.h>
#define NDEBUG
#include <debug.h>
/* FUNCTIONS ***************************************************************/
BOOL
AppendSystemPostfix (LPWSTR lpName,
DWORD dwMaxLength)
{
WCHAR szSystemRoot[MAX_PATH];
LPWSTR lpszPostfix;
LPWSTR lpszPtr;
/* Build profile name postfix */
if (!ExpandEnvironmentStringsW (L"%SystemRoot%",
szSystemRoot,
MAX_PATH))
{
DPRINT1("Error: %lu\n", GetLastError());
return FALSE;
}
_wcsupr (szSystemRoot);
/* Get name postfix */
szSystemRoot[2] = L'.';
lpszPostfix = &szSystemRoot[2];
lpszPtr = lpszPostfix;
while (*lpszPtr != (WCHAR)0)
{
if (*lpszPtr == L'\\')
*lpszPtr = '_';
lpszPtr++;
}
if (wcslen(lpName) + wcslen(lpszPostfix) + 1 >= dwMaxLength)
{
DPRINT1("Error: buffer overflow\n");
SetLastError(ERROR_BUFFER_OVERFLOW);
return FALSE;
}
wcscat(lpName, lpszPostfix);
return TRUE;
}
BOOL WINAPI
CreateUserProfileA (PSID Sid,
LPCSTR lpUserName)
{
UNICODE_STRING UserName;
BOOL bResult;
NTSTATUS Status;
Status = RtlCreateUnicodeStringFromAsciiz (&UserName,
(LPSTR)lpUserName);
if (!NT_SUCCESS(Status))
{
SetLastError (RtlNtStatusToDosError (Status));
return FALSE;
}
bResult = CreateUserProfileW (Sid,
UserName.Buffer);
RtlFreeUnicodeString (&UserName);
return bResult;
}
BOOL WINAPI
CreateUserProfileW (PSID Sid,
LPCWSTR lpUserName)
{
WCHAR szRawProfilesPath[MAX_PATH];
WCHAR szProfilesPath[MAX_PATH];
WCHAR szUserProfilePath[MAX_PATH];
WCHAR szDefaultUserPath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
LPWSTR SidString;
DWORD dwLength;
DWORD dwDisposition;
HKEY hKey;
LONG Error;
DPRINT("CreateUserProfileW() called\n");
Error = RegOpenKeyExW (HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
0,
KEY_QUERY_VALUE,
&hKey);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
SetLastError((DWORD)Error);
return FALSE;
}
/* Get profiles path */
dwLength = MAX_PATH * sizeof(WCHAR);
Error = RegQueryValueExW (hKey,
L"ProfilesDirectory",
NULL,
NULL,
(LPBYTE)szRawProfilesPath,
&dwLength);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
/* Expand it */
if (!ExpandEnvironmentStringsW (szRawProfilesPath,
szProfilesPath,
MAX_PATH))
{
DPRINT1("Error: %lu\n", GetLastError());
RegCloseKey (hKey);
return FALSE;
}
/* Get default user path */
dwLength = MAX_PATH * sizeof(WCHAR);
Error = RegQueryValueExW (hKey,
L"DefaultUserProfile",
NULL,
NULL,
(LPBYTE)szBuffer,
&dwLength);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
RegCloseKey (hKey);
wcscpy (szUserProfilePath, szProfilesPath);
wcscat (szUserProfilePath, L"\\");
wcscat (szUserProfilePath, lpUserName);
if (!AppendSystemPostfix (szUserProfilePath, MAX_PATH))
{
DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
LocalFree ((HLOCAL)SidString);
return FALSE;
}
wcscpy (szDefaultUserPath, szProfilesPath);
wcscat (szDefaultUserPath, L"\\");
wcscat (szDefaultUserPath, szBuffer);
/* Create user profile directory */
if (!CreateDirectoryW (szUserProfilePath, NULL))
{
if (GetLastError () != ERROR_ALREADY_EXISTS)
{
DPRINT1("Error: %lu\n", GetLastError());
return FALSE;
}
}
/* Copy default user directory */
if (!CopyDirectory (szUserProfilePath, szDefaultUserPath))
{
DPRINT1("Error: %lu\n", GetLastError());
return FALSE;
}
/* Add profile to profile list */
if (!ConvertSidToStringSidW (Sid,
&SidString))
{
DPRINT1("Error: %lu\n", GetLastError());
return FALSE;
}
wcscpy (szBuffer,
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\");
wcscat (szBuffer, SidString);
/* Create user profile key */
Error = RegCreateKeyExW (HKEY_LOCAL_MACHINE,
szBuffer,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hKey,
&dwDisposition);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
LocalFree ((HLOCAL)SidString);
SetLastError((DWORD)Error);
return FALSE;
}
/* Create non-expanded user profile path */
wcscpy (szBuffer, szRawProfilesPath);
wcscat (szBuffer, L"\\");
wcscat (szBuffer, lpUserName);
if (!AppendSystemPostfix (szBuffer, MAX_PATH))
{
DPRINT1("AppendSystemPostfix() failed\n", GetLastError());
LocalFree ((HLOCAL)SidString);
RegCloseKey (hKey);
return FALSE;
}
/* Set 'ProfileImagePath' value (non-expanded) */
Error = RegSetValueExW (hKey,
L"ProfileImagePath",
0,
REG_EXPAND_SZ,
(LPBYTE)szBuffer,
(wcslen (szBuffer) + 1) * sizeof(WCHAR));
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
LocalFree ((HLOCAL)SidString);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
/* Set 'Sid' value */
Error = RegSetValueExW (hKey,
L"Sid",
0,
REG_BINARY,
Sid,
GetLengthSid (Sid));
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
LocalFree ((HLOCAL)SidString);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
RegCloseKey (hKey);
/* Create user hive name */
wcscpy (szBuffer, szUserProfilePath);
wcscat (szBuffer, L"\\ntuser.dat");
/* Create new user hive */
Error = RegLoadKeyW (HKEY_USERS,
SidString,
szBuffer);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
LocalFree ((HLOCAL)SidString);
SetLastError((DWORD)Error);
return FALSE;
}
/* Initialize user hive */
if (!CreateUserHive (SidString, szUserProfilePath))
{
DPRINT1("Error: %lu\n", GetLastError());
LocalFree ((HLOCAL)SidString);
return FALSE;
}
RegUnLoadKeyW (HKEY_USERS,
SidString);
LocalFree ((HLOCAL)SidString);
DPRINT("CreateUserProfileW() done\n");
return TRUE;
}
BOOL WINAPI
GetAllUsersProfileDirectoryA (LPSTR lpProfileDir,
LPDWORD lpcchSize)
{
LPWSTR lpBuffer;
BOOL bResult;
lpBuffer = GlobalAlloc (GMEM_FIXED,
*lpcchSize * sizeof(WCHAR));
if (lpBuffer == NULL)
return FALSE;
bResult = GetAllUsersProfileDirectoryW (lpBuffer,
lpcchSize);
if (bResult)
{
WideCharToMultiByte (CP_ACP,
0,
lpBuffer,
-1,
lpProfileDir,
*lpcchSize,
NULL,
NULL);
}
GlobalFree (lpBuffer);
return bResult;
}
BOOL WINAPI
GetAllUsersProfileDirectoryW (LPWSTR lpProfileDir,
LPDWORD lpcchSize)
{
WCHAR szProfilePath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
DWORD dwLength;
HKEY hKey;
LONG Error;
Error = RegOpenKeyExW (HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
0,
KEY_QUERY_VALUE,
&hKey);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
SetLastError((DWORD)Error);
return FALSE;
}
/* Get profiles path */
dwLength = sizeof(szBuffer);
Error = RegQueryValueExW (hKey,
L"ProfilesDirectory",
NULL,
NULL,
(LPBYTE)szBuffer,
&dwLength);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
/* Expand it */
if (!ExpandEnvironmentStringsW (szBuffer,
szProfilePath,
MAX_PATH))
{
DPRINT1("Error: %lu\n", GetLastError());
RegCloseKey (hKey);
return FALSE;
}
/* Get 'AllUsersProfile' name */
dwLength = sizeof(szBuffer);
Error = RegQueryValueExW (hKey,
L"AllUsersProfile",
NULL,
NULL,
(LPBYTE)szBuffer,
&dwLength);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
RegCloseKey (hKey);
wcscat (szProfilePath, L"\\");
wcscat (szProfilePath, szBuffer);
dwLength = wcslen (szProfilePath) + 1;
if (lpProfileDir != NULL)
{
if (*lpcchSize < dwLength)
{
*lpcchSize = dwLength;
SetLastError (ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
wcscpy (lpProfileDir, szProfilePath);
}
*lpcchSize = dwLength;
return TRUE;
}
BOOL WINAPI
GetDefaultUserProfileDirectoryA (LPSTR lpProfileDir,
LPDWORD lpcchSize)
{
LPWSTR lpBuffer;
BOOL bResult;
lpBuffer = GlobalAlloc (GMEM_FIXED,
*lpcchSize * sizeof(WCHAR));
if (lpBuffer == NULL)
return FALSE;
bResult = GetDefaultUserProfileDirectoryW (lpBuffer,
lpcchSize);
if (bResult)
{
WideCharToMultiByte (CP_ACP,
0,
lpBuffer,
-1,
lpProfileDir,
*lpcchSize,
NULL,
NULL);
}
GlobalFree (lpBuffer);
return bResult;
}
BOOL WINAPI
GetDefaultUserProfileDirectoryW (LPWSTR lpProfileDir,
LPDWORD lpcchSize)
{
WCHAR szProfilePath[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
DWORD dwLength;
HKEY hKey;
LONG Error;
Error = RegOpenKeyExW (HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList",
0,
KEY_QUERY_VALUE,
&hKey);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
SetLastError((DWORD)Error);
return FALSE;
}
/* Get profiles path */
dwLength = sizeof(szBuffer);
Error = RegQueryValueExW (hKey,
L"ProfilesDirectory",
NULL,
NULL,
(LPBYTE)szBuffer,
&dwLength);
if (Error != ERROR_SUCCESS)
{
DPRINT1("Error: %lu\n", Error);
RegCloseKey (hKey);
SetLastError((DWORD)Error);
return FALSE;
}
/* Expand it */
if (!ExpandEnvironmentStringsW (szBuffer,
szProfilePath,
MAX_PATH))
{
DPRINT1("Error: %lu\n", GetLastError());
RegCloseKey (hKey);
return FALSE;
}
/* Get 'DefaultUserProfile' name */
dwLength = sizeof(szBuffer);
Error = RegQueryValueExW (hKey,
L"DefaultUserProfile",
NULL,
NULL,
(LPBYTE)szBuffer,
&dwLength);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -