Skip to content

Commit 9223134

Browse files
authored
[WINMM] PlaySound: Fix user-specific environment variables handling (reactos#7536)
Correctly retrieve user-specific environment variables when impersonating. Addendum to commit f18111b. CORE-13951
1 parent e6c18b4 commit 9223134

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

dll/win32/winmm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ endif()
3030

3131
set_module_type(winmm win32dll)
3232
target_link_libraries(winmm wine ${PSEH_LIB} oldnames)
33-
add_importlibs(winmm advapi32 user32 msvcrt kernel32 ntdll)
33+
add_importlibs(winmm userenv advapi32 user32 msvcrt kernel32 ntdll)
3434
add_pch(winmm winemm.h SOURCE)
3535
add_cd_file(TARGET winmm DESTINATION reactos/system32 FOR all)
3636

dll/win32/winmm/playsound.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "winemm.h"
2727

2828
#include <winternl.h>
29+
#include <userenv.h>
2930

3031
WINE_DEFAULT_DEBUG_CHANNEL(winmm);
3132

@@ -39,6 +40,61 @@ typedef struct tagWINE_PLAYSOUND
3940
static WINE_PLAYSOUND *PlaySoundCurrent;
4041
static BOOL bPlaySoundStop;
4142

43+
/* An impersonation-aware equivalent of ExpandEnvironmentStringsW */
44+
static DWORD PlaySound_ExpandEnvironmentStrings(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize)
45+
{
46+
HANDLE hToken;
47+
DWORD dwError;
48+
DWORD dwLength = 0;
49+
50+
if (!OpenThreadToken(GetCurrentThread(),
51+
TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE,
52+
TRUE,
53+
&hToken))
54+
{
55+
dwError = GetLastError();
56+
57+
if (dwError == ERROR_NO_TOKEN)
58+
{
59+
/* We are not impersonating, forward this to ExpandEnvironmentStrings */
60+
return ExpandEnvironmentStringsW(lpSrc, lpDst, nSize);
61+
}
62+
63+
ERR("OpenThreadToken failed (0x%x)\n", dwError);
64+
return 0;
65+
}
66+
67+
if (!ExpandEnvironmentStringsForUserW(hToken, lpSrc, lpDst, nSize))
68+
{
69+
dwError = GetLastError();
70+
71+
if (dwError == ERROR_INSUFFICIENT_BUFFER || nSize == 0)
72+
{
73+
/* The buffer is too small, find the required buffer size.
74+
* NOTE: ExpandEnvironmentStringsForUser doesn't support retrieving buffer size. */
75+
WCHAR szExpanded[1024];
76+
77+
if (ExpandEnvironmentStringsForUserW(hToken, lpSrc, szExpanded, ARRAY_SIZE(szExpanded)))
78+
{
79+
/* We success, return the required buffer size */
80+
dwLength = lstrlenW(szExpanded) + 1;
81+
goto Cleanup;
82+
}
83+
}
84+
85+
ERR("ExpandEnvironmentStringsForUser failed (0x%x)\n", dwError);
86+
}
87+
else
88+
{
89+
/* We success, return the size of the string */
90+
dwLength = lstrlenW(lpDst) + 1;
91+
}
92+
93+
Cleanup:
94+
CloseHandle(hToken);
95+
return dwLength;
96+
}
97+
4298
static HMMIO get_mmioFromFile(LPCWSTR lpszName)
4399
{
44100
HMMIO ret;
@@ -158,15 +214,15 @@ static HMMIO get_mmioFromProfile(UINT uFlags, LPCWSTR lpszName)
158214

159215
if (type == REG_EXPAND_SZ)
160216
{
161-
count = ExpandEnvironmentStringsW(str, NULL, 0);
217+
count = PlaySound_ExpandEnvironmentStrings(str, NULL, 0);
162218
if (count == 0)
163219
goto None;
164220

165221
pszSnd = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
166222
if (!pszSnd)
167223
goto None;
168224

169-
if (ExpandEnvironmentStringsW(str, pszSnd, count) == 0)
225+
if (PlaySound_ExpandEnvironmentStrings(str, pszSnd, count) == 0)
170226
{
171227
HeapFree(GetProcessHeap(), 0, pszSnd);
172228
goto None;

0 commit comments

Comments
 (0)