I'm using ExpandEnvironmentStringsForUser function to expand a directory which includes environment variable. However, the returned value is sometimes wrong.
My process is running with system privilege.
E.g:
Input string: "C:\Users\%username%\Documents\"
Output string (maybe one of following strings)
C:\Users\cuong.huynh\Documents\ (expected output)
C:\Users\cuong.huynh\AppData\Roaming (wrong)
C:\Program Files\Common Files (wrong)
My brief code is as follows:
DWORD dwSessionID = WTSGetActiveConsoleSessionId();
WTSQueryUserToken(dwSessionID, &hUserToken);
DuplicateToken(hUserToken, SecurityImpersonation, &hDuplicated)
hToken = hDuplicated;
LPCWSTR DesPath = (StringUtil::StrToWstr(setting)).c_str(); //(input)
wchar_t ExpandedDesPath[MAX_PATH]; //(output)
ExpandEnvironmentStringsForUser(hToken, DesPath, ExpandedDesPath, MAX_PATH - 1)
Does anyone know the reason why the output is not stable?