From f6df0d6caf7fefb7c69b07fcd2b2e06fc4930fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B3=88=E5=8F=AF?= <137729898@qq.com> Date: Mon, 3 Jun 2019 16:11:48 +0800 Subject: [PATCH] better way to obtain default user profile's directory Microsoft provided the **GetDefaultUserProfileDirectory** function to obtain the default user profile's directory, also, the directory name **Default User** had changed to **Default** in later version of Windows, so it is not correct in some cases. --- ImeModule.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ImeModule.cpp b/ImeModule.cpp index 67f63f8..1bd8d11 100644 --- a/ImeModule.cpp +++ b/ImeModule.cpp @@ -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; @@ -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