Elevating Permissions Under Windows 7
开发者_JS百科I have an 'auto-upgrade' mechanism that has been working for many years under Windows XP which fails under Windows 7 because the prompt to the User (which never existed under under Windows XP) to grant the program permission to run with administrator rights does not appear.
The win32 program (written in Borland Delphi) uses LogonUser() and ImpersonateLoggedOnUser() to impersonate a user (local account with administrator rights) so that it can replace selected files in the Program Files folder.
If I manually logon to Windows 7 using this user account and then attempt to delete a file in the Program Files folder Windows 7 prompts for my permission to allow the program to proceed - if I give it permission the file is deleted, if I deny permission 'access denied' is reported and the deletion is not allowed.
When attempting the same thing as described above 'access denied' is the result without any prompt to the user being given.
I see in a similar question herabouts (regarding the creation of temporary files (where the main thrust of the answers given is quite rightly 'don't do it') that such a prompt can be raised 'in code' but I cannot see where that method is actually explained.
You just need to mark your auto updater as requiring elevated privileges in your application manifest.
That said you might want to consider installing somewhere the user has write privileges.
You partly seem to be asking how to run code elevated. There are a couple of answers to the question below which provide links to the information you need to invoke functionality running elevated:
Delphi: Prompt for UAC elevation when needed
Well two years have passed, my clients are at last adopting Windows 7 and I have finally got round to circumventing the problem I was trying to solve (temporarily gaing sufficient rights to replace executables in Program Files under Windows 7) by replacing my own installer with an Inno Setup installer (incorporating additionl logic carried over from my installer using Pascal Scripting). Inno Set handles all the permissions issues - hurrah.
精彩评论