Understanding UAC on windows vista / 7

uac

I don't really understand windows UAC…

I need for my program to be able to update and add files to a specific directory belonging to a program. This directory may be a subdirectory of an application in Program Files, for example c:\Program Files\MyApp\Data or it may be installed elsewhere.

I believe that if it's under Program Files then my program will be prevented from writting there unless it is running as an administrator AND has elevated it's access rights. Is that correct?

I need to be able to update files in that directory preferable without invoking elevated privileges and with the main application still "protected", just allow access to that one directory. I can't move the Data folder elsewhere as this as it's a 3rd party application I need to interface with.

How is it determined that UAC is needed for folders in Program Files? Is Program Files special in some way or is just permissions? If I were to adjust the permissions on that Data subdirectory so that the user account running the program had write access would that allow my application to update files in that directory without special privileges?

Or is there a better way to achieve this that I'm not thinking of? My update program needs to be in java so getting elevated privileges is a pain. I imagine I'll need to write a C++ wrapper to run the java VM so that i can give that wrapper an appropriate manifest. Not impossible but I don't really want to have to do this.

Best Answer

Usually, when you need both protected and unprotected UAC modes you do the following.

  1. Create two executable (one should be the main one and not require privileges for any operation, the second one should be able to perform privileges operations).
  2. Start the first (main) one using limited privileges.
  3. When you need to perform an privileged operation, create a new process with administrative rights (will pop the UAC window) and start the second application in it.
  4. When done with the second application close it and you'll be back to limited mode.

This is how VMWare Workstation does when you change global settings.

Edit: Changing the permissions on a folder is not a good approach. Is just a dirty hack because anybody can write to that folder and this will just invalidate the role of UAC - after all this is the role of UAC: to prevent unprivileged changes in special folders.