Click here to Skip to main content
Licence CPOL
First Posted 23 Feb 2010
Views 8,505
Downloads 384
Bookmarked 8 times

Impersonate as another user to run a program

By | 23 Feb 2010 | Article
Extend the "runas" command to run a program as a specified user.

Introduction

This is an extension to the "runas" command in windows, so the current user can impersonate as another user to run a program. The motivation is to let a standard user run a program as Administrator without inputting the password every time, as discussed here:

Background

The link above gives an in-depth insight for the "runas" command in Windows. However, although it makes sense that the "runas" command doesn't take password as parameter, it does give a lot of trouble to personal users, and to me.

Let's say, we have Windows 7 installed on a home computer, and we don't want other "non-skilled" family members (your wife, children, grandma, etc.) to change critical settings or install software. We want them to use only the software that we allow them to run.

OK, it's easy, we create a "Standard User" account and give to them, and install software under the Administrator account, which is owned and only owned by us. So your wife/children/grandma can only run the programs you install.

So, things fixed?

Now, you install a program which needs administrator privilege, which means unless you turn off user account control, the "standard user" will be prompted to input Administrator password each time they run the program. And, your wife will be yelling: "hey, what is the ****ing password?" "Why do you make such complicated settings?"

Unfortunately, there are quite a number of applications that work this way and we simply cannot let a "standard user" run the program without inputting the admin password.

This program was written to solve the problem.

Using the Code

There is really not much to say about the code. It's almost the same as the sample code on MSDN: http://msdn.microsoft.com/en-us/library/ms682431(VS.85).aspx.

The only tweak is removing several unimportant parameters (such as environment settings) and the detection for the local user and the domain user.

The core part is no more than four lines:

if (wcschr(argv[1], L'@'))
{
  isLocalUser = TRUE;
}
if (!CreateProcessWithLogonW(argv[1], isLocalUser ? L"." : NULL, argv[2], 
            0, NULL, argv[3], 
            CREATE_UNICODE_ENVIRONMENT, NULL, NULL, 
            &si, &pi))
        DisplayError(L"CreateProcessWithLogonW");

Points of Interest

The code is simple, but it could be useful for people who setup different accounts for family members. Let's say you have a program at c:\Program Files\ppstream.exe which requires admin permission to run, but you want all your family members to be able to run the program without asking you to input the password, then you can create a shortcut to c:\Program Files\ppstream.exe, and modify the shortcut to: c:\runasuser.exe administrator your_password "c:\program files\ppstream.exe".

History

Submitted on 2/24/2010.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

zlike



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 Pinmemberszokelaszlo13:59 6 Jan '11  
Generalsome correction [modified] Pinmembernikogda0:47 15 Oct '10  
GeneralI like your article, please develop it PinmemberAlexandru Matei7:43 4 Mar '10  
GeneralNot as complete as it should have been. PinmemberTommyTooth18:47 24 Feb '10  
GeneralRe: Not as complete as it should have been. Pinmemberzlike14:15 26 Feb '10  
Newsrunas ordinary user PinmvpElmue13:38 24 Feb '10  
GeneralMy vote of 1 PinmvpDave Kreskowiak1:46 24 Feb '10  
GeneralRe: My vote of 1 Pinmemberxliqz3:07 24 Feb '10  
GeneralRe: My vote of 1 Pinmemberzlike14:16 26 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 24 Feb 2010
Article Copyright 2010 by zlike
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid