Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a windows program with updating, Permission problems

Tags:

c

windows

uac

nsis

I want to create a program Foo.exe with error logging into foo.log. That program is installed (NSIS Installer) into C:\Program Files (x86)\Foo\. During installation NSIS added a shortcut (CreateShortCut "$SMSTARTUP\Foo.lnk" "$INSTDIR\Foo.exe") to start Foo.exe when windows starts.

  • When windows executes my program after startup it starts my program with "non-administrative rights.". So my program can not edit C:\Program Files (x86)\Foo\foo.log. Workaround for that is to store editable files in another directory e.g. in $APPDATA.
  • But here come's the real problem: I added a update function into my program which works in the following way:
    • download the new binary and save as FooNew.exe. This file does not have any permissions (explicitly not the execute-flag).
    • create a self-copy with execute permission: FooCopy.exe
    • After starting it, FooCopy.exe opens FooNew.exe and writes it content into Foo.exe. Now, Foo.exe is updated and has the execute-permission.
    • FooCopy.exe will be removed.

So I have not testet the updating but most likely it wont work. I assume that Foo.exe can not create files in C:\Program Files (x86)\Foo\ so the updating fails at point 1.

An idea is to save Foo.exe not in C:\Program Files (x86)\ but in $APPDATA. So the program has all needed rights. The question is starting that program: It is compiled for 32-bit. Is it possible to have a "launcher" C:\Program Files (x86)\Foo\launcher.exe which starts the real application in $APPDATA?

That's confused, isn't it? How could I change the auto-update procedure so that UAC will not kick me?

As an example I see firefox.exe. How does it updating, thus, writing directly into C:\Program Files (x86)\... when normally no one could write into that directory without prompting the user.

Chris

like image 767
Christoph Avatar asked Jan 23 '26 01:01

Christoph


1 Answers

Firefox uses a NT service. Services normally run as SYSTEM so it can copy files anywhere and Mozilla configures their service so it can be started by a normal user.

It does not make sense to have a launcher in program files and the main app in appdata, you might as well do a per user install in that case (Like Chrome etc).

I assume your application is not a web-browser so keeping it up to date is probably not that critical. I would suggest that your updater just checks for updates and if there is a update you can spawn yourself again (or the main updater?) as Administrator using UAC and then do the update.

like image 176
Anders Avatar answered Jan 24 '26 19:01

Anders