3

My auto updater requires admin privileges to run since its modifying files in the programs folder. This all works fine.

However, when the update is done I want my updater.exe to start myApplication.exe but without admin privileges.

The code I currently use to start myApplication.exe from updater.exe:

bool started = QDesktopServices::openUrl(QUrl::fromLocalFile(exeFileName));
if (started)
{
    QApplication::quit();
}

The problem is that when myApplication.exe is started as admin it messes up my local paths, for example:

QString addin_path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);

Points to the admin users folder. And not the actual user who is running the application.

Any ideas here?

update

Based on Merlin069's answer I'm testing the following:

TCHAR username[UNLEN + 1];
DWORD size = UNLEN + 1;
GetUserName((TCHAR*)username, &size);

qDebug() << QHostInfo::localHostName();

QProcess::startDetached("runas \"/user:<" + QHostInfo::localHostName() + ">\"" + username + ""\" \"" + exeFileName + "\");
user3490755
  • 955
  • 2
  • 15
  • 33

1 Answers1

0

I'm assuming you're using Windows here.

You can launch your application with QProcess::startDetached and use the Windows RunAs command line program to run as a different user.

For example: -

QProcess::startDetached("runas /user:<localmachinename>\user exeFileName");

If the paths contain spaces, surround them in quotes: -

QProcess::startDetached("runas \"/user:<localmachinename>\user\" \"exeFileName\"");
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • How can i find out what user i should run as? and also how do i find out localmachinename? – user3490755 Apr 28 '14 at 12:12
  • You should be able to search for that easily enough with Google. For example, to get the logged in user: http://msdn.microsoft.com/en-us/library/421tdddx.aspx or the local machine name: http://stackoverflow.com/questions/504810/how-do-i-find-the-current-machines-full-hostname-in-c-hostname-and-domain-info. Alternatively, using Qt for the hostname: QHostInfo::localHostName(). – TheDarkKnight Apr 28 '14 at 12:16
  • Just noticed that the RunAs command requires the user to enter a password – user3490755 Apr 28 '14 at 13:11
  • Can you confirm that? I would expect that would not be the case if coming from an administrator account. – TheDarkKnight Apr 28 '14 at 13:17
  • Yea can confirm, my updater.exe is running as admin but and when the fire the code above I get a cmd prompt asking me to enter the password for my user – user3490755 Apr 28 '14 at 13:17
  • Instead of using runas /user..., does it work if you just specify the trust level? So that would be QProcess::startDetached("runas /TrustLevel:0x20000 exeFileName"); – TheDarkKnight Apr 28 '14 at 13:55
  • Just tried, but myApplication.exe still runs as admin that way – user3490755 Apr 28 '14 at 14:09
  • And if you combine TrustLevel and specifying the user, does it still ask for the password? – TheDarkKnight Apr 28 '14 at 14:12
  • Cant seem to combind the two – user3490755 Apr 28 '14 at 14:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51605/discussion-between-merlin069-and-user3490755) – TheDarkKnight Apr 28 '14 at 14:33