Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call another application with administrator priviliges

Call another application with administrator priviliges

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 6.2k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • km2442K Offline
    km2442K Offline
    km2442
    wrote on last edited by
    #1

    Hi, all!
    In my application I need to call another Qt application (small helper app) with administrator priviliges and some arguments. I don't know exactly how to do this.
    Only one method which works on my computer is (I have Windows 10 pro):

        QString program = QDir::toNativeSeparators("\"" + QDir::currentPath() + "/Appname.exe\"  arg1 arg2");
        system(program.toStdString().c_str());
    

    But I have tested it on another computers, and It'nt work for everyone.
    Could you tell me how to do this in right way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you also build that other program ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • km2442K Offline
        km2442K Offline
        km2442
        wrote on last edited by
        #3

        Yes, program is working but it needs admin priviliges and arguments...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • km2442K Offline
            km2442K Offline
            km2442
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Did you took a look at QProcess ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • km2442K Offline
                km2442K Offline
                km2442
                wrote on last edited by
                #7

                Yes, I have looked, but I can't call my helpr application with admin by QProcess....

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  }

                  1 Reply Last reply
                  2
                  • km2442K Offline
                    km2442K Offline
                    km2442
                    wrote on last edited by
                    #9

                    @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);
                    
                    1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @km2442 said:
                      Hi, do you use the right place for the paramters?
                      They should not be part of the exename/path

                      ShellExecuteA(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 "

                      1 Reply Last reply
                      1
                      • km2442K Offline
                        km2442K Offline
                        km2442
                        wrote on last edited by
                        #11

                        @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?

                        mrjjM 1 Reply Last reply
                        0
                        • km2442K km2442

                          @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?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @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)

                          1 Reply Last reply
                          1
                          • km2442K Offline
                            km2442K Offline
                            km2442
                            wrote on last edited by
                            #13

                            Thank you, your solution is working :)

                            1 Reply Last reply
                            1
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              super :)
                              thanks for the feedback.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved