25#include <osl/diagnose.h>
26#include <osl/process.h>
29#include <osl/file.hxx>
31#include <com/sun/star/lang/IllegalArgumentException.hpp>
32#include <com/sun/star/security/AccessControlException.hpp>
33#include <com/sun/star/system/SystemShellExecuteException.hpp>
34#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
35#include <com/sun/star/uri/UriReferenceFactory.hpp>
44#include <systools/win32/comtools.hxx>
47using namespace ::com::sun::star::system::SystemShellExecuteFlags;
59 struct errentry errtable[] = {
60 { ERROR_SUCCESS, osl_File_E_None },
61 { ERROR_INVALID_FUNCTION, osl_File_E_INVAL },
62 { ERROR_FILE_NOT_FOUND, osl_File_E_NOENT },
63 { ERROR_PATH_NOT_FOUND, osl_File_E_NOENT },
64 { ERROR_TOO_MANY_OPEN_FILES, osl_File_E_MFILE },
65 { ERROR_ACCESS_DENIED, osl_File_E_ACCES },
66 { ERROR_INVALID_HANDLE, osl_File_E_BADF },
67 { ERROR_ARENA_TRASHED, osl_File_E_NOMEM },
68 { ERROR_NOT_ENOUGH_MEMORY, osl_File_E_NOMEM },
69 { ERROR_INVALID_BLOCK, osl_File_E_NOMEM },
70 { ERROR_BAD_ENVIRONMENT, osl_File_E_2BIG },
71 { ERROR_BAD_FORMAT, osl_File_E_NOEXEC },
72 { ERROR_INVALID_ACCESS, osl_File_E_INVAL },
73 { ERROR_INVALID_DATA, osl_File_E_INVAL },
74 { ERROR_INVALID_DRIVE, osl_File_E_NOENT },
75 { ERROR_CURRENT_DIRECTORY, osl_File_E_ACCES },
76 { ERROR_NOT_SAME_DEVICE, osl_File_E_XDEV },
77 { ERROR_NO_MORE_FILES, osl_File_E_NOENT },
78 { ERROR_LOCK_VIOLATION, osl_File_E_ACCES },
79 { ERROR_BAD_NETPATH, osl_File_E_NOENT },
80 { ERROR_NETWORK_ACCESS_DENIED, osl_File_E_ACCES },
81 { ERROR_BAD_NET_NAME, osl_File_E_NOENT },
82 { ERROR_FILE_EXISTS, osl_File_E_EXIST },
83 { ERROR_CANNOT_MAKE, osl_File_E_ACCES },
84 { ERROR_FAIL_I24, osl_File_E_ACCES },
85 { ERROR_INVALID_PARAMETER, osl_File_E_INVAL },
86 { ERROR_NO_PROC_SLOTS, osl_File_E_AGAIN },
87 { ERROR_DRIVE_LOCKED, osl_File_E_ACCES },
88 { ERROR_BROKEN_PIPE, osl_File_E_PIPE },
89 { ERROR_DISK_FULL, osl_File_E_NOSPC },
90 { ERROR_INVALID_TARGET_HANDLE, osl_File_E_BADF },
91 { ERROR_INVALID_HANDLE, osl_File_E_INVAL },
92 { ERROR_WAIT_NO_CHILDREN, osl_File_E_CHILD },
93 { ERROR_CHILD_NOT_COMPLETE, osl_File_E_CHILD },
94 { ERROR_DIRECT_ACCESS_HANDLE, osl_File_E_BADF },
95 { ERROR_NEGATIVE_SEEK, osl_File_E_INVAL },
96 { ERROR_SEEK_ON_DEVICE, osl_File_E_ACCES },
97 { ERROR_DIR_NOT_EMPTY, osl_File_E_NOTEMPTY },
98 { ERROR_NOT_LOCKED, osl_File_E_ACCES },
99 { ERROR_BAD_PATHNAME, osl_File_E_NOENT },
100 { ERROR_MAX_THRDS_REACHED, osl_File_E_AGAIN },
101 { ERROR_LOCK_FAILED, osl_File_E_ACCES },
102 { ERROR_ALREADY_EXISTS, osl_File_E_EXIST },
103 { ERROR_FILENAME_EXCED_RANGE, osl_File_E_NOENT },
104 { ERROR_NESTING_NOT_ALLOWED, osl_File_E_AGAIN },
105 { ERROR_NOT_ENOUGH_QUOTA, osl_File_E_NOMEM }
109 #define ERRTABLESIZE (SAL_N_ELEMENTS(errtable))
113 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
114 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
118 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
119 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
124 oslFileError _mapError( DWORD dwError )
131 if ( dwError == errtable[
i].oscode )
132 return static_cast<oslFileError
>(errtable[
i].errnocode);
140 return osl_File_E_ACCES;
142 return osl_File_E_NOEXEC;
144 return osl_File_E_INVAL;
147 #define MapError( oserror ) _mapError( oserror )
149 #define E_UNKNOWN_EXEC_ERROR -1
153 WeakComponentImplHelper< css::system::XSystemShellExecute, css::lang::XServiceInfo >(
m_aMutex ),
155 mnNbCallCoInitializeExForReinit(0)
173bool checkExtension(std::u16string_view extension, std::u16string_view denylist) {
174 assert(!extension.empty());
175 for (std::size_t i = 0;
i != std::u16string_view::npos;) {
187BOOL CALLBACK FindAndActivateProcWnd(HWND hwnd, LPARAM lParam)
189 if (!IsWindowVisible(hwnd))
191 if (GetWindow(hwnd, GW_OWNER))
193 const DWORD nParamProcId =
static_cast<DWORD
>(lParam);
194 assert(nParamProcId != 0);
195 DWORD nWndProcId = 0;
196 (void)GetWindowThreadProcessId(hwnd, &nWndProcId);
197 if (nWndProcId != nParamProcId)
203 ShowWindow(hwnd, SW_RESTORE);
205 SetForegroundWindow(hwnd);
206 SetActiveWindow(hwnd);
211void SAL_CALL
CSysShExec::execute(
const OUString& aCommand,
const OUString& aParameter, sal_Int32 nFlags )
215 throw css::lang::IllegalArgumentException(
217 static_cast< css::system::XSystemShellExecute*
>(
this ),
220 if ((nFlags & ~(NO_SYSTEM_ERROR_MESSAGE | URIS_ONLY)) != 0)
221 throw css::lang::IllegalArgumentException(
222 "Invalid Flags specified",
223 static_cast< css::system::XSystemShellExecute*
>(
this ),
226 OUString preprocessed_command(
aCommand);
227 if ((nFlags & URIS_ONLY) != 0)
229 css::uno::Reference< css::uri::XUriReference > uri(
231 if (!(uri.is() && uri->isAbsolute()))
233 throw css::lang::IllegalArgumentException(
234 "XSystemShellExecute.execute URIS_ONLY with"
235 " non-absolute URI reference "
239 if (uri->getScheme().equalsIgnoreAsciiCase(
"file")) {
241 uri->clearFragment();
244 = osl::FileBase::getSystemPathFromFileURL(uri->getUriReference(), pathname);
245 if (e1 != osl::FileBase::E_None) {
246 throw css::lang::IllegalArgumentException(
247 (
"XSystemShellExecute.execute, getSystemPathFromFileURL <" +
aCommand
248 +
"> failed with " + OUString::number(e1)),
251 const int MAX_LONG_PATH = 32767;
252 if (pathname.getLength() >= MAX_LONG_PATH)
254 throw css::lang::IllegalArgumentException(
255 "XSystemShellExecute.execute, path <" + pathname +
"> too long", {}, 0);
257 preprocessed_command = pathname;
258 wchar_t path[MAX_LONG_PATH];
259 wcscpy_s(path, o3tl::toW(pathname.getStr()));
260 for (
int i = 0;; ++
i) {
262 if (PathResolve(path,
nullptr, PRF_VERIFYEXISTS | PRF_REQUIREABSOLUTE) == 0)
264 throw css::lang::IllegalArgumentException(
265 OUString::Concat(
u"XSystemShellExecute.execute, PathResolve(") + o3tl::toU(path)
270 if (SHGetFileInfoW(path, 0, &info,
sizeof info, SHGFI_EXETYPE) != 0)
272 throw css::security::AccessControlException(
273 "XSystemShellExecute.execute, cannot process <" +
aCommand +
">", {}, {});
275 if (SHGetFileInfoW(path, 0, &info,
sizeof info, SHGFI_ATTRIBUTES) == 0)
277 throw css::lang::IllegalArgumentException(
278 OUString::Concat(
u"XSystemShellExecute.execute, SHGetFileInfoW(") + o3tl::toU(path) +
") failed", {},
281 if ((info.dwAttributes & SFGAO_LINK) == 0) {
284 sal::systools::COMReference<IShellLinkW> link;
287 link.CoCreateInstance(CLSID_ShellLink,
nullptr, CLSCTX_INPROC_SERVER);
289 catch (sal::systools::ComError& e)
291 throw css::lang::IllegalArgumentException(
292 (
"XSystemShellExecute.execute, CoCreateInstance failed with "
293 + OUString::number(e.GetHresult())),
296 sal::systools::COMReference<IPersistFile> file;
298 file = link.QueryInterface<IPersistFile>(sal::systools::COM_QUERY_THROW);
299 }
catch(sal::systools::ComError & e3) {
300 throw css::lang::IllegalArgumentException(
301 (
"XSystemShellExecute.execute, QueryInterface failed with: "
305 HRESULT e2 = file->Load(path, STGM_READ);
307 throw css::lang::IllegalArgumentException(
308 (
"XSystemShellExecute.execute, IPersistFile.Load failed with "
309 + OUString::number(e2)),
312 e2 = link->Resolve(
nullptr, SLR_UPDATE | SLR_NO_UI);
314 throw css::lang::IllegalArgumentException(
315 (
"XSystemShellExecute.execute, IShellLink.Resolve failed with "
316 + OUString::number(e2)),
319 WIN32_FIND_DATAW wfd;
320 e2 = link->GetPath(path,
SAL_N_ELEMENTS(path), &wfd, SLGP_RAWPATH);
322 throw css::lang::IllegalArgumentException(
323 (
"XSystemShellExecute.execute, IShellLink.GetPath failed with "
324 + OUString::number(e2)),
329 throw css::lang::IllegalArgumentException(
330 "XSystemShellExecute.execute, link depth exceeded for <" +
aCommand +
">",
334 pathname = o3tl::toU(path);
336 while (pathname.endsWith(
".", &pathname)) {}
337 auto const n = pathname.lastIndexOf(
'.');
338 if (
n > pathname.lastIndexOf(
'\\')) {
339 auto const ext = pathname.copy(
n + 1);
340 if (!ext.isEmpty()) {
342 if (osl_getEnvironment(OUString(
"PATHEXT").
pData, &
env.pData)
343 != osl_Process_E_None)
345 SAL_INFO(
"shell",
"osl_getEnvironment(PATHEXT) failed");
347 if (!(checkExtension(ext,
env)
350 u".ADE;.ADP;.APK;.APPLICATION;.APPX;.APPXBUNDLE;.BAT;.CAB;.CHM;.CLASS;"
351 ".CMD;.COM;.CPL;.DLL;.DMG;.EX;.EX_;.EXE;.GADGET;.HTA;.INF;.INS;.IPA;"
352 ".ISO;.ISP;.JAR;.JS;.JSE;.LIB;.LNK;.MDE;.MSC;.MSH;.MSH1;.MSH2;.MSHXML;"
353 ".MSH1XML;.MSH2XML;.MSI;.MSIX;.MSIXBUNDLE;.MSP;.MST;.NSH;.PIF;.PS1;"
354 ".PS1XML;.PS2;.PS2XML;.PSC1;.PSC2;.PY;.REG;.SCF;.SCR;.SCT;.SHB;.SYS;"
355 ".VB;.VBE;.VBS;.VXD;.WS;.WSC;.WSF;.WSH;")))
357 throw css::security::AccessControlException(
358 "XSystemShellExecute.execute, cannot process <" +
aCommand +
">", {},
366 SHELLEXECUTEINFOW sei;
367 ZeroMemory(&sei,
sizeof( sei));
369 sei.cbSize =
sizeof(sei);
370 sei.lpFile = o3tl::toW(preprocessed_command.getStr());
371 sei.lpParameters = o3tl::toW(aParameter.getStr());
372 sei.nShow = SW_SHOWNORMAL;
373 sei.fMask = SEE_MASK_NOCLOSEPROCESS;
375 if (NO_SYSTEM_ERROR_MESSAGE & nFlags)
376 sei.fMask |= SEE_MASK_FLAG_NO_UI;
380 bool bRet = ShellExecuteExW(&sei);
382 if (!bRet && (nFlags & NO_SYSTEM_ERROR_MESSAGE))
386 sal_Int32 psxErr = GetLastError();
387 if (ERROR_SUCCESS == psxErr)
392 throw css::system::SystemShellExecuteException(
393 "Error executing command",
394 static_cast< css::system::XSystemShellExecute*
>(
this),
400 const DWORD procId = GetProcessId(sei.hProcess);
403 AllowSetForegroundWindow(procId);
404 WaitForInputIdle(sei.hProcess, 1000);
405 EnumWindows(FindAndActivateProcWnd,
static_cast<LPARAM
>(procId));
410 CloseHandle(sei.hProcess);
417 return "com.sun.star.sys.shell.SystemShellExecute";
427 return {
"com.sun.star.system.SystemShellExecute" };
430extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
432 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const&)
434 return cppu::acquire(
new CSysShExec(context));
Reference< XComponentContext > m_xContext
#define E_UNKNOWN_EXEC_ERROR
#define MapError(oserror)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * shell_CSysShExec_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL execute(const OUString &aCommand, const OUString &aParameter, sal_Int32 nFlags) override
CSysShExec(const css::uno::Reference< css::uno::XComponentContext > &xContext)
int mnNbCallCoInitializeExForReinit
css::uno::Reference< css::uno::XComponentContext > m_xContext
#define SAL_INFO(area, stream)
#define SAL_N_ELEMENTS(arr)
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
std::unique_ptr< sal_Int32[]> pData
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
OUString runtimeToOUString(char const *runtimeString)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
bool parse(OUString const &uri, SourceProviderScannerData *data)
const wchar_t *typedef BOOL