Click here to Skip to main content
15,887,083 members
Articles / Programming Languages / C++

Impersonate as another user to run a program

Rate me:
Please Sign up or sign in to vote.
1.82/5 (4 votes)
23 Feb 2010CPOL2 min read 53.9K   2.3K   12   9
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:

C++
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
szokelaszlo6-Jan-11 13:59
szokelaszlo6-Jan-11 13:59 
Generalsome correction [modified] Pin
nikogda15-Oct-10 0:47
nikogda15-Oct-10 0:47 
GeneralI like your article, please develop it Pin
Alexandru Matei4-Mar-10 7:43
Alexandru Matei4-Mar-10 7:43 
GeneralNot as complete as it should have been. Pin
TommyTooth24-Feb-10 18:47
TommyTooth24-Feb-10 18:47 
GeneralRe: Not as complete as it should have been. Pin
zlike26-Feb-10 14:15
zlike26-Feb-10 14:15 
Newsrunas ordinary user Pin
Elmue24-Feb-10 13:38
Elmue24-Feb-10 13:38 
GeneralMy vote of 1 Pin
Dave Kreskowiak24-Feb-10 1:46
mveDave Kreskowiak24-Feb-10 1:46 
GeneralRe: My vote of 1 Pin
xliqz24-Feb-10 3:07
xliqz24-Feb-10 3:07 
Although I find the MSDN article more informative.
GeneralRe: My vote of 1 Pin
zlike26-Feb-10 14:16
zlike26-Feb-10 14:16 

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

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