Skip to content

Commit 546297f

Browse files
authored
[SHELL32][SHLWAPI][BROWSEUI][SHELL32_APITEST][SDK][KBSWITCH][RAPPS] Support SHEvaluateSystemCommandTemplate (reactos#9111)
This function has an important role for new ShellExecute implementation. JIRA issue: CORE-19278 - Add evalcmd static library into dll/win32/shell32/evalcmd. - Implement SHEvaluateSystemCommandTemplate in evalcmd. - Link evalcmd to shell32 and shlwapi. - Modify shell32.spec and shlwapi.spec. - Add prototype to <shellapi.h>. - Add SHEvaluateSystemCommandTemplate testcase. - Define PathIsAbsolute inline function in <shlwapi_undoc.h> and use it.
1 parent afbc922 commit 546297f

17 files changed

Lines changed: 531 additions & 5 deletions

File tree

‎base/applications/kbswitch/kbswitch.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
#include "kbswitch.h"
1010
#include <shlobj.h>
11+
#include <shlwapi.h>
1112
#include <shlwapi_undoc.h>
1213
#include <undocuser.h>
1314
#include <imm.h>

‎base/applications/rapps/loaddlg.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include <ui/rosctrls.h>
4444
#include <windowsx.h>
45+
#include <shlwapi.h>
4546
#include <shlwapi_undoc.h>
4647
#include <process.h>
4748
#undef SubclassWindow

‎dll/win32/browseui/shellfind/CSearchBar.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ LOCATIONITEM* CSearchBar::GetAddressEditBoxLocations(WCHAR *szPath)
480480

481481
DWORD dwAttributes = GetFileAttributesW(pszPath);
482482
if (dwAttributes != INVALID_FILE_ATTRIBUTES && (dwAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
483-
PathIsAbsolute(pszPath))
483+
FindFiles_PathIsAbsolute(pszPath))
484484
{
485485
return BuildLocationList(&pszPath, 1);
486486
}

‎dll/win32/browseui/shellfind/shellfind.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static inline BOOL PathIsOnUnc(PCWSTR Path)
7979
return PathIsUNCW(Path); // FIXME: Verify the path starts with <\\Server\Share>[\]
8080
}
8181

82-
static inline bool PathIsAbsolute(PCWSTR Path)
82+
static inline bool FindFiles_PathIsAbsolute(PCWSTR Path)
8383
{
8484
// Note: PathIsRelativeW is too forgiving
8585
return PathIsOnDrive(Path) || PathIsOnUnc(Path);

‎dll/win32/shell32/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PROJECT(SHELL)
22

3+
add_subdirectory(evalcmd)
34
add_subdirectory(shelldesktop)
45
add_subdirectory(shellmenu)
56
add_subdirectory(shellrecyclebin)
@@ -131,6 +132,9 @@ if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
131132
target_compile_options(shell32 PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:-Wno-overloaded-virtual>")
132133
endif()
133134
target_link_libraries(shell32 shellmenu shelldesktop wine uuid recyclebin cpprt atl_classes oldnames)
135+
if(DLL_EXPORT_VERSION GREATER_EQUAL 0x600)
136+
target_link_libraries(shell32 evalcmd)
137+
endif()
134138
add_delay_importlibs(shell32 powrprof shdocvw devmgr winspool.drv winmm mpr uxtheme ole32 oleaut32 userenv browseui version fmifs netapi32 secur32)
135139
add_importlibs(shell32 advapi32 gdi32 user32 comctl32 comdlg32 shlwapi shcore msvcrt kernel32 ntdll)
136140
add_dependencies(shell32 stdole2) # shell32_shldisp.tlb needs stdole2.tlb
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
add_library(evalcmd STATIC evalcmd.cpp)
3+
add_dependencies(evalcmd psdk)
4+
target_link_libraries(evalcmd PRIVATE pathcch_static)
5+
target_compile_definitions(evalcmd PRIVATE STATIC_PATHCCH)
6+
7+
if(DLL_EXPORT_VERSION GREATER_EQUAL 0x600)
8+
target_compile_definitions(evalcmd PRIVATE _SHELL32_)
9+
else()
10+
target_compile_definitions(evalcmd PRIVATE _SHLWAPI_)
11+
endif()
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/*
2+
* PROJECT: ReactOS Shell
3+
* LICENSE: LGPL-2.1+ (https://spdx.org/licenses/LGPL-2.1+)
4+
* PURPOSE: SHEvaluateSystemCommandTemplate
5+
* COPYRIGHT: Copyright 2026 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6+
*/
7+
8+
#include <windef.h>
9+
#include <shlobj.h>
10+
#include <shlwapi.h>
11+
#include <shlguid_undoc.h>
12+
#include <shlobj_undoc.h>
13+
#include <shlwapi_undoc.h>
14+
#include <strsafe.h>
15+
#include <pathcch.h>
16+
#include <assert.h>
17+
18+
#define PATH_VALID_CHARS \
19+
(PATH_CHAR_CLASS_DOT | PATH_CHAR_CLASS_SEMICOLON | PATH_CHAR_CLASS_COMMA | \
20+
PATH_CHAR_CLASS_SPACE | PATH_CHAR_CLASS_OTHER_VALID)
21+
22+
/**
23+
* Get the position of the arguments of a command line string as CreateProcess does.
24+
*/
25+
static PCWSTR _PathGetArgsLikeCreateProcess(PCWSTR lpString)
26+
{
27+
PCWSTR pch;
28+
if (*lpString == L'"')
29+
{
30+
pch = StrChrW(lpString + 1, L'"');
31+
if (pch)
32+
{
33+
++pch;
34+
if (*pch == L' ')
35+
++pch;
36+
return pch;
37+
}
38+
}
39+
else
40+
{
41+
pch = StrChrW(lpString, L' ');
42+
if (pch)
43+
return pch + 1;
44+
}
45+
return &lpString[lstrlenW(lpString)];
46+
}
47+
48+
static HRESULT
49+
_PathCopyExeAndTrimWhiteSpaces(PWSTR pszBuff, size_t cchBuff, PCWSTR pszSrc, size_t cchSrc)
50+
{
51+
HRESULT hr = StringCchCopyNW(pszBuff, cchBuff, pszSrc, cchSrc);
52+
if (SUCCEEDED(hr))
53+
StrTrimW(pszBuff, L" \t");
54+
return hr;
55+
}
56+
57+
/**
58+
* @brief "Program Files" contains space. It needs special handling. This function will detect it.
59+
*/
60+
static BOOL _PathMatchesSuspicious(PCWSTR lpString)
61+
{
62+
WCHAR pszPath[MAX_PATH];
63+
SHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL, 0, pszPath);
64+
INT cch = lstrlenW(pszPath);
65+
return StrCmpNIW(lpString, pszPath, cch) == 0;
66+
}
67+
68+
// This function attempts to find where the "arguments" portion of a command-line path string
69+
static PCWSTR _PathGuessNextBestArgs(PCWSTR pszPath)
70+
{
71+
PCWSTR pSpaceStart = NULL;
72+
BOOL bValid = TRUE;
73+
74+
for (; *pszPath && bValid; ++pszPath)
75+
{
76+
switch (*pszPath)
77+
{
78+
case L' ':
79+
if (!pSpaceStart)
80+
pSpaceStart = pszPath;
81+
break;
82+
83+
case L'"':
84+
case L'%':
85+
bValid = FALSE;
86+
break;
87+
88+
case L'\\':
89+
bValid = !PathIsUNCW(pszPath);
90+
if (bValid)
91+
pSpaceStart = NULL;
92+
break;
93+
94+
default:
95+
bValid = PathIsValidCharW(*pszPath, PATH_VALID_CHARS);
96+
break;
97+
}
98+
}
99+
100+
if (pSpaceStart)
101+
{
102+
while (*pSpaceStart == L' ')
103+
++pSpaceStart;
104+
return pSpaceStart;
105+
}
106+
107+
return bValid ? pszPath : NULL;
108+
}
109+
110+
static VOID _MakeAppPathKey(PCWSTR pszPath, PWSTR pszDest, UINT cchDest)
111+
{
112+
HRESULT hr = PathCchCombineEx(pszDest, cchDest,
113+
L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths",
114+
pszPath, PATHCCH_NONE);
115+
if (SUCCEEDED(hr))
116+
PathCchAddExtension(pszDest, cchDest, L".exe");
117+
}
118+
119+
static BOOL _GetAppPath(PCWSTR pszPath, PWSTR pszValue, DWORD cchValue)
120+
{
121+
WCHAR szSubKey[MAX_PATH];
122+
_MakeAppPathKey(pszPath, szSubKey, _countof(szSubKey));
123+
DWORD cbData = cchValue * sizeof(WCHAR);
124+
LSTATUS error = SHGetValueW(HKEY_LOCAL_MACHINE, szSubKey, NULL, NULL, pszValue, &cbData);
125+
return error == ERROR_SUCCESS;
126+
}
127+
128+
static HRESULT _PathExeExists(_In_ PCWSTR pszPath)
129+
{
130+
WCHAR szPath[MAX_PATH];
131+
StringCchCopyW(szPath, _countof(szPath), pszPath);
132+
133+
DWORD dwWhich = WHICH_PIF | WHICH_COM | WHICH_EXE | WHICH_BAT | WHICH_CMD | WHICH_OPTIONAL;
134+
DWORD attrs;
135+
if (!PathFileExistsDefExtAndAttributesW(szPath, dwWhich, &attrs) ||
136+
(attrs & FILE_ATTRIBUTE_DIRECTORY))
137+
{
138+
return CO_E_APPNOTFOUND;
139+
}
140+
return S_OK;
141+
}
142+
143+
static HRESULT
144+
_PathFindInFolder(_In_ INT csidl, _In_ PCWSTR pszSrc, _Out_ PWSTR pszPath, _In_ UINT cchPath)
145+
{
146+
WCHAR szDir[MAX_PATH];
147+
HRESULT hr = SHGetFolderPathW(0, csidl, 0, 0, szDir);
148+
if (FAILED(hr))
149+
return hr;
150+
151+
hr = PathCchCombineEx(pszPath, cchPath, szDir, pszSrc, PATHCCH_NONE);
152+
if (FAILED(hr))
153+
return hr;
154+
155+
return _PathExeExists(pszPath);
156+
}
157+
158+
static HRESULT _PathFindInSystem(_Inout_ PWSTR pszPath, _In_ UINT cchPath)
159+
{
160+
WCHAR szPath[MAX_PATH];
161+
HRESULT hr = _PathFindInFolder(CSIDL_SYSTEM, pszPath, szPath, _countof(szPath));
162+
if (FAILED(hr))
163+
hr = _PathFindInFolder(CSIDL_WINDOWS, pszPath, szPath, _countof(szPath));
164+
if (FAILED(hr))
165+
return hr;
166+
return StringCchCopyW(pszPath, cchPath, szPath);
167+
}
168+
169+
/*************************************************************************
170+
* SHEvaluateSystemCommandTemplate [SHELL32.@] (Vista+)
171+
* SHEvaluateSystemCommandTemplate [SHLWAPI.552] (XP SP1 and SP2)
172+
*
173+
* https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shevaluatesystemcommandtemplate
174+
* https://github.com/tpn/winsdk-10/blob/9b69fd26ac0c7d0b83d378dba01080e93349c2ed/Include/10.0.16299.0/um/shellapi.h#L525
175+
*/
176+
EXTERN_C
177+
HRESULT WINAPI
178+
SHEvaluateSystemCommandTemplate(
179+
_In_ PCWSTR pszCmdTemplate,
180+
_Outptr_ PWSTR *ppszApplication,
181+
_Outptr_opt_ PWSTR *ppszCommandLine,
182+
_Outptr_opt_ PWSTR *ppszParameters)
183+
{
184+
HRESULT hr;
185+
BOOL bQuoted;
186+
WCHAR szExe[MAX_PATH], szProgram[MAX_PATH];
187+
188+
PCWSTR pszArgs = _PathGetArgsLikeCreateProcess(pszCmdTemplate);
189+
UINT cchArgs = (UINT)(pszArgs - pszCmdTemplate);
190+
hr = _PathCopyExeAndTrimWhiteSpaces(szExe, _countof(szExe), pszCmdTemplate, cchArgs);
191+
if (FAILED(hr))
192+
goto Exit;
193+
194+
// Unquote if necessary
195+
bQuoted = (szExe[0] == L'"');
196+
if (bQuoted)
197+
PathUnquoteSpacesW(szExe);
198+
199+
hr = StringCchCopyW(szProgram, _countof(szProgram), szExe);
200+
assert(SUCCEEDED(hr));
201+
202+
if (PathIsAbsolute(szExe))
203+
{
204+
if (bQuoted)
205+
{
206+
hr = _PathExeExists(szExe);
207+
}
208+
else // Not quoted
209+
{
210+
if (_PathMatchesSuspicious(szExe)) // ProgramFiles-likely?
211+
hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
212+
else
213+
hr = _PathExeExists(szExe);
214+
}
215+
216+
// Detect where the full executable path ends and the arguments start
217+
while (FAILED(hr))
218+
{
219+
if (bQuoted || !*pszArgs)
220+
break;
221+
222+
pszArgs = _PathGuessNextBestArgs(pszArgs);
223+
if (!pszArgs)
224+
break;
225+
226+
cchArgs = (UINT)(pszArgs - pszCmdTemplate);
227+
hr = _PathCopyExeAndTrimWhiteSpaces(szExe, _countof(szExe), pszCmdTemplate, cchArgs);
228+
if (FAILED(hr))
229+
break;
230+
231+
hr = _PathExeExists(szExe);
232+
}
233+
}
234+
else
235+
{
236+
if (!PathIsFileSpecW(szExe))
237+
{
238+
hr = E_ACCESSDENIED;
239+
goto Exit;
240+
}
241+
242+
if (_GetAppPath(szExe, szExe, _countof(szExe)))
243+
{
244+
hr = StringCchCopyW(szProgram, _countof(szProgram), PathFindFileNameW(szExe));
245+
}
246+
else if (SHWindowsPolicyEx(POLID_UsePathEnvVarForCommandTemplates, FALSE))
247+
{
248+
hr = PathFindOnPathExW(szExe, NULL, PATH_VALID_CHARS) ? S_OK : CO_E_APPNOTFOUND;
249+
}
250+
else
251+
{
252+
hr = _PathFindInSystem(szExe, _countof(szExe));
253+
}
254+
}
255+
256+
Exit:
257+
*ppszApplication = NULL;
258+
if (ppszCommandLine)
259+
*ppszCommandLine = NULL;
260+
if (ppszParameters)
261+
*ppszParameters = NULL;
262+
263+
if (!pszArgs)
264+
pszArgs = L"";
265+
266+
// Create output strings
267+
if (SUCCEEDED(hr))
268+
hr = SHStrDupW(szExe, ppszApplication);
269+
270+
if (SUCCEEDED(hr) && ppszCommandLine)
271+
{
272+
size_t cch = lstrlenW(szProgram) + lstrlenW(pszArgs) + 4; // 4 for '"', '"', ' ', NUL
273+
hr = SHCoAlloc(cch * sizeof(WCHAR), (PVOID*)ppszCommandLine);
274+
if (SUCCEEDED(hr))
275+
hr = StringCchPrintfW(*ppszCommandLine, cch, L"\"%s\" %s", szProgram, pszArgs);
276+
}
277+
278+
if (SUCCEEDED(hr) && ppszParameters)
279+
hr = SHStrDupW(pszArgs, ppszParameters);
280+
281+
if (FAILED(hr))
282+
{
283+
// Clean up
284+
if (*ppszApplication)
285+
{
286+
CoTaskMemFree(*ppszApplication);
287+
*ppszApplication = NULL;
288+
}
289+
if (ppszCommandLine && *ppszCommandLine)
290+
{
291+
CoTaskMemFree(*ppszCommandLine);
292+
*ppszCommandLine = NULL;
293+
}
294+
if (ppszParameters && *ppszParameters)
295+
{
296+
CoTaskMemFree(*ppszParameters);
297+
*ppszParameters = NULL;
298+
}
299+
}
300+
301+
return hr;
302+
}

‎dll/win32/shell32/shell32.spec‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@
505505
@ stdcall SHEmptyRecycleBinW(long wstr long) # 2k3:285, Vista:344
506506
@ stub SHEnableServiceObject # 2k3:286, Vista:345
507507
@ stdcall SHEnumerateUnreadMailAccountsW(ptr long ptr long) # 2k3:287, Vista:346
508-
@ stub -version=0x600+ SHEvaluateSystemCommandTemplate # Vista:347
508+
@ stdcall -version=0x600+ SHEvaluateSystemCommandTemplate(wstr ptr ptr ptr) # Vista:347
509509
@ stdcall SHExtractIconsW(wstr long long long ptr ptr long long) user32.PrivateExtractIconsW # 2k3:288, Vista:348
510510
@ stdcall SHFileOperation(ptr) SHFileOperationA # 2k3:289, Vista:349
511511
@ stdcall SHFileOperationA(ptr) # 2k3:290, Vista:350

‎dll/win32/shell32/wine/shpolicy.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
#include <winbase.h>
4040
#include <shlobj.h>
4141
#include <initguid.h>
42-
#include <shlwapi_undoc.h>
4342
#include <shlwapi.h>
43+
#include <shlwapi_undoc.h>
4444
#include <wine/debug.h>
4545

4646
#include "shell32_main.h"

‎dll/win32/shlwapi/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ add_dependencies(shlwapi_autocomp psdk)
6969

7070
set_module_type(shlwapi win32dll UNICODE)
7171
target_link_libraries(shlwapi uuid wine crtheap cppstl)
72+
if(DLL_EXPORT_VERSION LESS 0x600)
73+
target_link_libraries(shlwapi evalcmd)
74+
endif()
7275
add_delay_importlibs(shlwapi userenv oleaut32 ole32 comctl32 comdlg32 mpr mlang urlmon shell32 winmm version)
7376
add_importlibs(shlwapi shcore user32 gdi32 advapi32 wininet msvcrt kernelbase kernel32 kernel32_vista ntdll)
7477
#add_pch(shlwapi precomp.h "${PCH_SKIP_SOURCE}")

0 commit comments

Comments
 (0)