GUI-Based RunAs


Click here for a larger image.

Environment: VC6 SP5, Win2k, Platform SDK

To launch a program under another user context, Win2k provides a command line tool called RunAs. And in December of last year, there was an article posted on the codeguru site that give a similar functionality with source code. I noticed someone asked if there was a GUI-based RunAs, so I wrote one myself. And, according to the axiom “do not recreate the wheel,” I extracted code from this site and other books.

The main purpose of this utility is saving you typing, and of course, a more friendly user interface. Besides,

  • You can choose the target desktop on which you want the program to run. I provide the desktop list dialog; you just need to double-click it.
  • The button to the left of the password edit box permits you to test your username-password pair correctness.
  • The button to the right of the user name edit box will lead to a local machine user list dialog box; you can just choose the user by double-clicking.

    CreateProcessAsUser and LogonUser need the following four privileges: SeIncreaseQuotaPrivilege, SeAssignPrimaryTokenPrivilege, SeChangeNotifyPrivilege, and SeTcbPrivilege. By default, the administrator does not have these privileges. You can use the bottom Privilege button to pop up a dialog, set these privileges, and relog yourself. (Yes, you need to relog.) If you dislike this, please go to http://www.codeguru.com/misc/CmdAsUser.html
    contributed by Martyn “Ginner” Brown.

  • When you leave the username and password empty, the program will be launched as “SYSTEM” account.

There is a short list of often-used programs you can choose to “RunAs”. The inside code uses the LSA family API to cope with the account privilege, and uses LogonUser and CreateProcessAsUser to do the work. When you ask for a launch as “SYSTEM”, more work needs to be done — open the system process, adjust its token by adding TOKEN_DUPLICATE, TOKEN_ASSIGN_PRIMARY, TOKEN_QUERY right, and reopen it.

In the end, I would like to thank the book Programming Server-Side Applications for Mircosoft Windows 2000 from Microsoft Press, 1999. This program’s backbone code is from the sample of the book.

Last note, you need a ReLog to use this utillity. If you add the needed privilege, please push the privilege button first to confirm your current privilege, and it will log you off if you like.

Downloads (MFC Library Dynamic Linked)

Download demo executable file – 26 Kb

Download source – 53 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read