2626#include "winemm.h"
2727
2828#include <winternl.h>
29+ #include <userenv.h>
2930
3031WINE_DEFAULT_DEBUG_CHANNEL (winmm );
3132
@@ -39,6 +40,61 @@ typedef struct tagWINE_PLAYSOUND
3940static WINE_PLAYSOUND * PlaySoundCurrent ;
4041static 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+
4298static 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