Start a Command As Any User | CodeGuru

Start a Command As Any User

Click here for larger image Environment: Windows NT 4.0 (and upwards) VC6 This is a complete console utility to start a command as a specified user on Windows NT. This uses the LogonUser Windows API to programmatically logon to NT as a specified user with a given password. If this succeeds CreateProcessAsUser is used to […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 15, 2001
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 larger image

Environment: Windows NT 4.0 (and upwards) VC6

This is a complete console utility to start a command as a specified user on Windows NT. This uses the

LogonUser

Windows API to programmatically logon to NT as a specified user with a given password. If this succeeds

CreateProcessAsUser

is used to spawn a specified process as the


user.

The main problem with LogonUser is that it requires the caller to possess a fairly high level privilege “Act as part of the operating system” to function. This is a powerful privilege which should not be freely given – the local adminstrators
may often not have this privilege. As a work around for this, the utility first checks to see whether the user has the required privilege and if not the following occurs:


  • The utility installs itself temporarily as a service running under the system account.

  • The new service is started and the parameters (required user, process) are passed to the service via shared memory.

  • The service then attempts to logon as the specified user and start the process. Results are returned back to the utility via shared memory.

  • The utility then ends the service, uninstalls the service and displays results.

This works because the system account has high level privileges and usually the local admins group has permission to install services. For usage instructions type the executable with no parameters.

The source code also demonstrates the following topics:


  • Windows security APIs (including lightweight template classes for dealing with SIDs, ACEs, ACLS, Security Descriptors, Privileges and Access tokens).

  • Installing/configuring services programmatically using the service control manager.

  • Writing an NT service (including a service template class which can implement a simple service in less than 20 lines of code).

  • Using memory mapped files for sharing memory (including template class for automatic management of memory mapped files and other lightwieght kernel objects).


Downloads

Download demo project – 33 Kb

Download source – 165 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.