-1

Simple Question: Is there a way to only ask for administrative privileges if you run the application on Windows 8?

The only method I saw so far is calling an external program to kill and reopen it with admin privileges, if it is on Windows 8. By external program I mean a vb script for example.
But this method is really unsafe and kind of dirty.

I'm asking because on Windows 8 there are a lot of little things that you can't do anymore without admin rights. Like write/delete files in the program files folders and accessing/manipulating other processes that have admin rights.

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • That started in Windows Vista when UAC was introduced. (It actually started in XP when not running as a power user or administrator, but the default was to be a power user, so it didn't have much impact.) There are many answers here on UAC and privileges; you should be able to find them. (Also search for "Windows manifest", which is the solution that will work on all Windows OS versions since XP. For instance, see the [first link](http://stackoverflow.com/q/90674/62576) in the Related list to the right of your question, as well as the second and fifth.) – Ken White Apr 10 '14 at 23:16
  • The problems that I'm having started on Windows 8. The manifest is definitely the way to go if you want to always ask for administrative privileges, but the point of my question was to only ask for these privileges if we are on Windows 8. – Forivin Apr 10 '14 at 23:33
  • 3
    The specific examples you've given are not new to Windows 8. They've always required admin privilege. – Harry Johnston Apr 10 '14 at 23:39
  • I can just repeat that the access problems started on Windows 8. On Windows 7 everything works fine. A project I used to work on did only work on Windows 8 when I manually launched it as administrator. On Windows 7 it worked just fine the normal way. – Forivin Apr 10 '14 at 23:51
  • If you can identify the specific thing that doesn't work on Windows 8 perhaps we can be of more help. – Mark Ransom Apr 11 '14 at 00:16
  • I can just repeat that the issues requiring admin privileges that you describe also exist on Windows Vista and 7 (like "write/delete files in the program files folders and accessing/manipulating other processes that have admin rights"). Unless you have something *specific* that works on Win7 but is different in Win8, the answers are the same for both OSes. – Ken White Apr 11 '14 at 00:41
  • Yeah you already said that and I'm thankful for it, but redundancy won't solve my problem. So maybe there is some other factor that we're missing. If you have a Windows 8 and a Windows 7 machine available, I can try to dig out the project I was talking about and pass it over to you. – Forivin Apr 11 '14 at 00:51
  • Solving your problem will require you to tell us what the problem is. Vague questions lead to vague answers. If you haven't done enough debugging to know what's failing, then you still have work to do. Don't expect us to dig through a huge project to figure it out for you. – Mark Ransom Apr 11 '14 at 01:16
  • The most likely cause of your confusion is that UAC has been turned off on the Windows 7 machine you use for testing, making you think your code works on Windows 7 when really it doesn't. – Harry Johnston Apr 11 '14 at 01:26
  • Well, I haven't disabled it, but it's set to the lowest level on both systems. Maybe the lowest level on Windows 7 differs from the lowest level on Windows 8? – Forivin Apr 11 '14 at 02:02
  • Why don't you stop doing these tasks that require admin rights. Why do you want to make your program unusable to standard users. – David Heffernan Apr 11 '14 at 06:52

1 Answers1

2

To the best of my knowledge, there is no way for a process running without an elevated token to become elevated. It can launch a new process elevated using ShellExecute with the "runas" verb (see http://blogs.msdn.com/b/vistacompatteam/archive/2006/09/25/771232.aspx). Of course a UAC prompt will show. If you really want to detect Windows 8 and only have the UAC prompt come up there and not on Windows 7, you can write a wrapper over your main application which launches your main application with UAC prompt on Win8.

Electron
  • 1,390
  • 10
  • 8
  • +1, but there's no need for a separate wrapper. The application can always relaunch itself if it detects that it needs to, no external application, vbscript, or wrapper required. – Harry Johnston Apr 11 '14 at 01:29
  • I'd really like to see how you'd do that. Any examples? – Forivin Apr 11 '14 at 02:26
  • Electron's answer already contains a link to sample code. Just put your own executable in place of `notepad.exe`. You can use `GetModuleFileName` to find the location of your executable. – Harry Johnston Apr 11 '14 at 03:34