How to request admin rights in Qt?

  • A+
Category:Languages

I tried to create a key in windows registry, but I got this error:

5 Access is denied.

The code snippet is like this:

HKEY hKey; LPCTSTR sk = TEXT("SOFTWARE//Microsoft//Windows NT//CurrentVersion//Image File Execution Options//notepad.exe"); //notepad.exe is the key I want to create     //note: if I change the first parameter to KEY_CURRENT_USER, the key will be created  LONG createResKey = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sk, 0, NULL, REG_OPTION_BACKUP_RESTORE, KEY_ALL_ACCESS, NULL, &hKey, NULL);  if (createResKey == ERROR_SUCCESS) {     qDebug() << "Success creating key."; } else {     qDebug() << "Error creating key.";      showErrorText(createResKey); } 

Maybe it is because my program doesn't have the admin privilege, I tried my best to do research online and tried to get the admin privilege, but failed. I am quite confused about how to solve this problem right now.

Edit: What do I want to achieve?
I am trying to block some specific apps from starting by modifying the registry. For example, if I want to block notepad, first I have to create a "notepad.exe" key, and then set a string value "debugger" to it and set its value to "debugfile.exe". So the notepad will be block from starting.

 


GUI toolkits are so complex that it is usually advised to avoid taking administrative privileges to run them, and many GUI toolkits have (for good reasons) code to detect and disable (or warn against) that. On Linux, that means that you should not run GTK or Qt code as root (and if you do, some warning is emitted, or maybe the toolkit aborts; for Qt, see this; for GTK, see that).

In practice, you should try to have some small program (probably on the command line, or as a daemon) with administrative privilege and have your Qt program start it (e.g. with QProcess) and/or communicate with it (using some kind of inter-process communication), taking advantage of the process isolation and multi-user ability provided by your operating system.

I guess that some general understanding of OSes should help. Then I recommend reading Operating Systems: Three Easy Pieces to get more insight about OSes in general.

Details -i.e. how to start some program with more privileges than your Qt program- are operating system specific (on Linux, read about setuid). Inter-process communication facilities are also OS specific (on Linux, see pipe(7), fifo(7), shm_overview(7), unix(7), sem_overview(7) etc... and read some Linux programming book, perhaps the old ALP or something newer). How to get more privileges or do inter-process communication on Windows is a very different question (unrelated to Qt).

So you need to dive into the WinAPI documentation (perhaps here) to find out how to start a daemon or a small program with administrative privilege and how to perform inter-process communication with it. I guess that Windows has such facilities, but they are not wrapped in Qt (and neither is the access to the registery) and you need to write Windows specific code and learn more about WinAPI. I cannot help you, since I don't know and never used Windows.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: