Skip to content

better way to obtain default user profile's directory #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions ImeModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,9 @@ static void loadDefaultUserRegistry(const wchar_t* defaultUserRegKey) {
// the HKEY_CURRENT_USER key of newly created users can also contain our settings.
// In order to do this, we need to load the default "hive" to registry first.
// Reference: https://msdn.microsoft.com/zh-tw/library/windows/desktop/ms724889(v=vs.85).aspx
wchar_t *userProfilesDir = nullptr;
if (SUCCEEDED(::SHGetKnownFolderPath(FOLDERID_UserProfiles, 0, NULL, &userProfilesDir))) {
// get the path of the default ntuser.dat file
std::wstring defaultRegFile = userProfilesDir;
::CoTaskMemFree(userProfilesDir);
defaultRegFile += L"\\Default User\\ntuser.dat";

wchar_t szHiveFile[MAX_PATH] = { 0 };
DWORD cchHiveFile = ARRAYSIZE(szHiveFile);
if (GetDefaultUserProfileDirectoryW(szHiveFile, &cchHiveFile) && PathAppendW(szHiveFile, L"ntuser.dat")) {
// loading registry file requires special privileges SE_RESTORE_NAME and SE_BACKUP_NAME.
// So let's do privilege elevation for our process.
HANDLE processToken = NULL;
Expand All @@ -136,7 +132,7 @@ static void loadDefaultUserRegistry(const wchar_t* defaultUserRegKey) {
::CloseHandle(processToken);

// load the default registry hive under the specified key name
::RegLoadKeyW(HKEY_USERS, defaultUserRegKey, defaultRegFile.c_str());
::RegLoadKeyW(HKEY_USERS, defaultUserRegKey, szHiveFile);
}
}
#endif // #ifndef _WIN64
Expand Down