Solved Call another application with administrator priviliges
-
Hi and welcome to devnet,
Do you also build that other program ?
-
Yes, program is working but it needs admin priviliges and arguments...
-
So if you are building both application yourself, you need to embed a manifest file your Appname application to tell the system that it requires elevated privileges. You have an example on how to to that here
-
Yes, my HelperApp is starting with adminstrator privileges, but I cant call it from main application to show UAC (give privileges), send arguments, and get return code. That's the problem. The way with console don't work with all computers.
-
Did you took a look at QProcess ?
-
Yes, I have looked, but I can't call my helpr application with admin by QProcess....
-
hi
Its not possible to use runas for it ?void MainWindow::on_pushButton_released() {
QString exeFileName;
exeFileName = "notepad.exe";
#ifdef Q_OS_WIN
// Requesting elevation
int result = (int)::ShellExecuteA(0, "runas", exeFileName.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
if (result <= 32) {
// error handling
}
#endif
} -
@mrjj Hi, I can run my helper app (UAC is showing) with code:
QString exeFileName = QDir::toNativeSeparators("\"" + QDir::currentPath() + "/app.exe\""); int result = (int)::ShellExecuteA(0, "runas", exeFileName.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
but, I can't provide any arguments with this method, moreover I'm getting unexpected return code: 42.
I have tried option with arguments:
QString exeFileName = QDir::toNativeSeparators("\"" + QDir::currentPath() + "/app.exe\" arg1 arg2"); int result = (int)::ShellExecuteA(0, "runas", exeFileName.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
-
@km2442 said:
Hi, do you use the right place for the paramters?
They should not be part of the exename/pathShellExecuteA(0, "runas", exeFileName.toUtf8().constData(),** THIS IS FOR PARAMS**, 0, SW_SHOWNORMAL);
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
Else im not sure what u mean with
" I can't provide any arguments " -
@mrjj Ok, Now, your solution is working, but there is one problem: I'm getting Return Code 42 all time. My app is sending code 1 or 24 or 25. Do you know how to fix it?
-
@km2442
Super.
I dont think it can return the "return value"
Docs says
"If the function succeeds, it returns a value greater than 32. "For return value , I think u need to use ( the Ex version)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx
and use
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx
for the return code.SHELLEXECUTEINFO ShExecInfo = {0}; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = L"runas"; ShExecInfo.lpFile = "c:\\MyProgram.exe"; ShExecInfo.lpParameters = ""; ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_SHOW; ShExecInfo.hInstApp = NULL; ShellExecuteEx(&ShExecInfo); WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
(code not tested)
-
Thank you, your solution is working :)
-
super :)
thanks for the feedback.