-1

I have a Qt/C++ application. The application needs to register an npapi browser plugin which is registered using the following command:

regsvr32 npmyplugin.dll

I can use QProcess or even system() function, but to register a service administrative privileges are required. How can I start the service in this case.

adnan kamili
  • 8,967
  • 7
  • 65
  • 125

1 Answers1

2

There is no way to elevate your privileges once you start the process. However, you can require for higher privileges when starting a new process. As you're on Windows, just use ShellExecuteEx and set runas in lpVerb field of SHELLEXECUTEINFO:

    SHELLEXECUTEINFO shExInfo = {0};
    shExInfo.cbSize = sizeof(shExInfo);
    shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    shExInfo.hwnd = 0;
    shExInfo.lpVerb = _T("runas");                 // Operation to perform
    shExInfo.lpFile = _T("regsvr32.exe");          // Application to start    
    shExInfo.lpParameters = _T("npmyplugin.dll");  // Additional parameters
    shExInfo.lpDirectory = 0;
    shExInfo.nShow = SW_SHOW;
    shExInfo.hInstApp = 0;  

    ShellExecuteEx(&shExInfo);
    return 0;

To use this in a Qt application, just #include <windows.h> and make sure include and lib variables are set properly for the Windows SDK.

For setting up Qt creator with Windows SDK, check this question: How can I use the Windows SDK with Qt Creator

The original code is comming from the accepted answer in this question: How can I run a child process that requires elevation and wait?

Community
  • 1
  • 1
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
  • I am getting following error http://stackoverflow.com/questions/14286513/lnk-2019-shellexecuteex-unresolved-external-symbol-qt-creator – adnan kamili Sep 21 '13 at 19:50
  • 1
    You need to setup Qt Creator to work with Windows SDK (see the link in my answer) and you need to link your program against Shell32.lib. – Nemanja Boric Sep 21 '13 at 20:39