GUI-Based RunAs | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 1, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


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 https://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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.