S
S
simply_user2016-09-07 17:38:39
C++ / C#
simply_user, 2016-09-07 17:38:39

Is it possible to get Administrator user rights on Windows 7 from code?

Hello!
I need to execute some code with Administrator user rights. To do this, I act according to the following scheme:
1). I log in the user who is an administrator (the first user that is created during Windows installation) using the WinApi function Logon;
2). I create an instance of the WindowsIdentity class based on the token received after login;
3). I impersonate the user represented by the WindowsIdentity instance and get the WindowsImpersonationContext.
After that, and until the Undo method of an instance of the WindowsImpersonationContext class is called (or until Dispose is called, I don’t understand here), I will be able to execute code with elevated rights. But the problem is that this user, although he has administrator rights, is not one himself, therefore, for example, I can write to his folder (which, of course, I could not do with the rights of a regular user), but raise or lower network interface I can't. To do this, you already need to start the process initially on behalf of the Administrator, for example, using runas or from the context menu - "Run as administrator". But I would like to do all this from code. The question is, is it possible? After all, as far as I understand, Windows does not give full administrative rights even to those users who are members of the "
Thank you for your attention!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rifat, 2016-09-08
@serber

You can specify the need for administrator rights in the application manifest. In this case, a UAC window will appear at startup. Example

M
Michael, 2016-09-08
@Sing303

In windows, it is not possible to change the rights of a process at run time. You can only run with rights.
Other than restarting or using a separate application for the desired functionality, there are no other options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question