1

I am working on an application in qt where I am required to use ShellExecuteEx to launch an application. I am running a batch file and I need to pass it 2 arguments. The first argument is just a letter, but the second is a path that may or may not contain spaces. No matter what I do, it will not escape the spaces. It seems like including any escaped quotations actually breaks the entire call. I think the issue is with the runas. I'm not 100% sure how it works, but it seems like it is copying the call and running it in an escalated shell. It seems like this process breaks with the added quotations.

Here is my ShellExecute code. I have simplified it to passing in 1 argument, but this still breaks.

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = L"runas";
ShExecInfo.lpFile = L"test.bat";
ShExecInfo.lpParameters = L"\"test param\"";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);

Here is test.bat just for completeness

@echo off
pause

The shell output I get is.

'J:\Repositories\Qt' is not recognized as an internal or external command, operable program or batch file.

The path to the script is

J:\Repositories\Qt Project\debug\test.bat

You can see that the initial command fails because its messing up the original path.

Any idea how to include quotations in the arguments with a runas command like this?

thecaptain0220
  • 2,098
  • 5
  • 30
  • 51
  • *I am required to use ShellExecuteEx* - Why, what's wrong with [QProcess](http://doc.qt.io/qt-5/qprocess.html)? – TheDarkKnight Sep 17 '15 at 13:54
  • 1
    From what I have read, using QProcess there is no way to run the application with admin privileges without running the whole application with admin privileges. – thecaptain0220 Sep 17 '15 at 13:56
  • Sorry, I don't quite understand. How can you only partly run with Admin privileges? – TheDarkKnight Sep 17 '15 at 14:12
  • 1
    My application does not require admin privileges under normal circumstances. In this case I need to launch a 3rd party tool that does require admin privileges. If I use ShellExecuteEx and runas it runs the 3rd party application as administrator and the user gets a UAC prompt to allow it. – thecaptain0220 Sep 17 '15 at 14:17
  • Call QProcess with the runas command, passing the necessary parameters. – TheDarkKnight Sep 17 '15 at 14:23
  • How do you do this? From what I was reading it is not possible. – thecaptain0220 Sep 17 '15 at 14:45
  • I don't have Windows here for the exact syntax, but it would be something like this **Qprocess::execute("runas /user:Administrator test.bat");** You may need to call cmd first **Qprocess::execute(" cmd /c \"runas /user:Administrator test.bat\");** – TheDarkKnight Sep 17 '15 at 15:09

1 Answers1

0

Try next:

ShExecInfo.lpFile = NULL;
ShExecInfo.lpParameters = L"test.bat \"test param\"";
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61