0

In my Qt5.5 app I'm trying to write to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

registry key, to enable autorun on every account, using QSettings, but I can't do it even with Administrator Priviliges. Could you tell me how should I do it right way? If I try to use this code with HKCU to enable autorun to current user, it's working.

QSettings bootUp("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
bootUp.setValue("/MyApp", "\"" + QDir::currentPath() + "/MyApp.exe\"" + " -a -u");
km2442
  • 779
  • 2
  • 11
  • 31
  • 1
    Note that a better solution is to add a shortcut to your application in the Start Menu's "Startup" folder. That can be done for either a specific user or for all users. Leave the registry alone. – Cody Gray - on strike Feb 07 '16 at 13:18
  • Is it the exact code that you are running? Could you provide your working code for `HKCU` too to compare? – John_West Feb 07 '16 at 13:24
  • @John_West I've only replaced HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE, and new key is not showing. – km2442 Feb 07 '16 at 13:28
  • @CodyGray How does one switch between current user and all users? I've read of some `AllUsers`-setting supporting `true` and `false`, but can't find anything in the docs on how to use it. https://stackoverflow.com/questions/59510656/how-to-distinguish-per-user-vs-system-wide-installation-in-qt-installer-framewo – Thorsten Schöning Jan 05 '20 at 19:21

1 Answers1

3

What's confusing you is that you have a 32 bit process running on 64 bit Windows and you are writing to a part of the registry that is redirected by the registry redirector. So

HKLM\Software

is redirected to

HKLM\Software\Wow6432Node

You'll find your entries under there. This is nothing to worry about. The system will reads keys from both 32 and 64 views on startup.

Remember that if your code was failing to write to the registry then it would be throwing an exception.

In short, your code works, you are just looking in the wrong place in the Registry Editor.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @Ilya that's a little different because virtualization is involved there. I'm assuming that we aren't virtualized here. – David Heffernan Feb 07 '16 at 14:26
  • @DavidHeffernan Yes, it's true, this key really exists... My problem is solved :) Thank you. – km2442 Feb 07 '16 at 14:32
  • Your psychic debugging skills are better than mine. Last time I remember looking at it, Qt didn't use exceptions (has that changed?) and that QSettings::setValue method returns void, so there seemed to be little chance of an error getting reported if one were to occur. – Cody Gray - on strike Feb 07 '16 at 15:28
  • @Cody I don't know Qt. I just assume it throws. – David Heffernan Feb 07 '16 at 15:49