6

I have one major problem with my app. I have an app & updater as a separate exe files. When an update is available, updater does the update, and on process completion it starts my app. The main problem is - app is installed in program files folder, so updater need UAC admin privileges, and that's ok, but when I need to run my app updater needs to run it as a normal user, because if it's run as an administrator drag and drop doesn't work (not an app problem, UAC blocks it). I've tried several different solutions, and even this one: How to run NOT elevated in Vista (.NET)

It haven't helped me - my app is run as an administrator.

Community
  • 1
  • 1
n1tr0
  • 269
  • 4
  • 15

2 Answers2

10

You'd better avoid starting a non-elevated process from an elevated one. It's tricky part and error-prone.

This approach is better:

  1. Your updater initially starts as non-elevated application, and its manifest has asInvoker level.
  2. When it starts, it restarts itself with elevated privileges using runas verb, and passes a command-line parameter to indicate it. This instance performs the update and returns.
  3. Here comes the non-elevated updater again, and starts your application with the same non-elevated user token that started the first instance of updater in step 1.

Pretty simple and robust.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
  • 1
    +1; The first line of this answer is quite important. I've read articles that said that it is a bad idea to try to de-elevate (get a de-elevated token to impersonate), no matter what technique you attempt. Not sure where to find that or those articles since I read about it when Vista first came out. Another option is given in this question: http://stackoverflow.com/questions/1173630/how-do-you-de-elevate-privileges-for-a-child-process – Merlyn Morgan-Graham Oct 24 '11 at 18:39
  • Made it this way - works like a charm! Many thanks my friend! ;) – n1tr0 Oct 26 '11 at 01:57
  • @AlexeyIvanov can you please provide a simple example code for you approach? – python_kaa Sep 18 '13 at 08:14
  • @python_kaa I don't know C#, but I can write code snippet on pure Windows API or give you pseudo-code. – Alexey Ivanov Sep 19 '13 at 08:58
  • @AlexeyIvanov the non-elevated updater shouldn't be closed while the elevated-privileges updates is running, correct? I mean: in step 2 there are 2 instances of the updater running (one elevated and one non-elevated)? In this case, it's a good idea to make the non-elevated invisible (no window) while the elevated one is running? – drizin Dec 30 '15 at 02:33
  • 1
    @drizin Yes, you understand it correctly. Non-elevated updater could work without creating a window at all. For example, if the elevated updater succeeds with updating and returns 0 exit code, then the non-elevated one knows it's time to launch the application. Otherwise, it just exits. – Alexey Ivanov Dec 30 '15 at 10:02
0

Look at this post on how to Enable Drag and Drop for an Elevated process. Even though it says MFC app you can cll those Windows API in Any app I suppose

https://helgeklein.com/blog/2010/03/how-to-enable-drag-and-drop-for-an-elevated-mfc-application-on-vistawindows-7/

coolshashi
  • 420
  • 1
  • 6
  • 19