Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Article

RunAs Class

Rate me:
Please Sign up or sign in to vote.
4.74/5 (27 votes)
14 Feb 20054 min read 324.8K   8.4K   69   71
Class that wraps CreateProcessWithLogonW as well as a simple control that makes use of the RunAs class.

Sample Image - RunAs.jpg

Introduction

There are three projects in this solution: RunAs, UseRunAsControl, and ProcessToStart. RunAs is the focus of this solution; it contains the class that wraps CreateProcessWithLogonW. UseRunAsControl defines and makes use of a simple control implementing the RunAs class and is meant to test and show its functionality. ProcessToStart is simply a form that shows the domain and username of the user whose syntax it is running under. This is merely there to start with the UseRunAsControl to demonstrate its functionality.

Demo

To see the solution in action, grab a second set of credentials (make some dummy ones on your local machine, perhaps). Run UseRunAsControl.exe and provide the credentials. Click on "Command..." and browse to ProcessToStart.exe. Click on "Run Command". Provided the credentials are correct, you will see a MessageBox containing the process ID of the new process. ProcessToStart will display the username that it is running as. If the credentials that UseRunAsControl.exe is running under has enough privileges, when you close ProcessToStart, you will see another MessageBox notifying you that the process has ended. If the user does not have privileges to the new process, you will see a MessageBox notifying you of this, and when ProcessToStart.exe exits, you will not receive any notice.

Class Usage

Using the RunAs class is simple. Add a reference to the assembly and include the namespace VastAbyss. There is an overloaded static method named StartProcess in the class. This simple overload provides standard functionality that starts the executable as the user and loads the profile. A word of caution with using this method is that if the command line is C:\Program Files\Some Directory\Some.exe, if a Program.exe exists in C:\, it will be started and this may be seen as a security flaw. It is due to the way that CreateProcessWithLogonW parses and searches the command line (space-delimited). To avoid this, surround the command line in quotes. All of the overloads return a Process. If the process failed to start, it will be null and a Win32Exception will be thrown. Below is a sample of the simple usage:

C#
string username = "SomeUser";
string domain = "SomeDomain";
string password = "I'll never tell!";
string commandline = "\"C:\\Program Files\\Some Directory\\Some.exe\"";

// Resulting string is
//  "C:\Program Files\Some Directory\Some.exe"
// with the quotes included.
try
{
  Process proc = RunAs.StartProcess(username, domain, password,
                                                  commandline);
  try
  {
    proc.EnableRaisingEvents = true;
    proc.Exited += new EventHandler(processExited);
  }
  catch
  {
    //The process started but you don't have access to it.
  }
}
catch (Win32Exception w32e)
{
  // The process didn't start.
}

To avoid the security risk of using command line, use one of the other overloads of StartProcess() to provide the executable in appname instead of command line (command line must be used to provide parameters to the executable if needed; i.e., c:\myapp.exe /q /t). These overloads provide many more options for creating the new process. Enums are provided for supplying the values of the flags. Additional overloads can easily be added to provide full control over creating the new process. The struct definition for StartUpInfo is public and can be used with the last overload to provide the maximum amount of control.

I have added a default constructor to the RunAs class. This constructor initializes the properties to the following values: UserName (System.Environment.UserName), Domain (System.Environment.UserDomainName), Password (empty string ""), ApplicationName (CurrentProcess.StartInfo.FileName), LogonFlagsInstance (LogonFlags.WithProfile), CommandLine (System.Environment.CommandLine), CreationFlagsInstance (CreationFlags.NewConsole), CurrentDirectory (System.Environment.CurrentDirectory), Environment (IntPtr.Zero), ProcessInfo (new ProcessInformation instance), StartupInfo (new StartUpInfo instance with the following values set: cb is set to the size of the new instance, dwFlags is set to StartUpInfoFlags.UseCountChars, dwYCountChars is set to 50, lpTitle is set to CurrentProcess.MainWindowTitle). After initialization, these values can be changed and the non-static method StartProcess can be called.

Control Usage

I will leave the below code included although the focus of this project is to implement the RunAs class and not this control. This control merely serves as an example of how the RunAs class can be used. I removed the RunAsControl from the RunAs project and placed it in the UseRunAsControl project.

The RunAsControl can be quickly added to a Windows Form and the four events wired up. That's all there is to it. Below is an example usage:

C#
RunAsControl m_runAsCtl = new RunAsControl();
m_runAsCtl.ProcessStarted += new ProcessStartedEventHandler(m_pStarted);
m_runAsCtl.ProcessFailed += new ProcessFailedEventHandler(m_pFailed);
m_runAsCtl.ProcessEnded += new ProcessEndedEventHandler(m_pEnded);
m_runAsCtl.ProcessAccessFailed +=
    new ProcessAccessFailedEventHandler(m_pAccessFailed);

Comment Disclaimer

I referred to the MSDN documentation for the CreateProcessWithLogonW, PROCESS_INFORMATION, STARTUPINFO, etc... functions, structs, and constants. Most of the comments in the source code are either direct quotes from this documentation or adaptations of information from that documentation.

Thanks

I would like to thank those who provided feedback to this project. I have incorporated the suggestions and fixed the bugs that I found. I hope that this makes the project better, but if there are still things that you think are wrong with it, I welcome more constructive criticism.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
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

 
QuestionHow can i run batch file (.bat) using this class? Pin
Tempeck15-Jul-15 2:31
Tempeck15-Jul-15 2:31 
QuestionContact information Pin
manojrajgarhia30-Sep-14 8:21
manojrajgarhia30-Sep-14 8:21 
QuestionOh you lifesaver! Pin
Perrillo17-Oct-12 0:19
Perrillo17-Oct-12 0:19 
AnswerRe: Oh you lifesaver! Pin
Dewey Vozel17-Oct-12 4:28
Dewey Vozel17-Oct-12 4:28 
GeneralRe: Oh you lifesaver! Pin
Perrillo17-Oct-12 5:07
Perrillo17-Oct-12 5:07 
GeneralSpecific issue Pin
SRoyalty10-Jan-11 5:05
SRoyalty10-Jan-11 5:05 
GeneralRe: Specific issue Pin
Dewey Vozel12-Jan-11 8:29
Dewey Vozel12-Jan-11 8:29 
GeneralLifesaver!!!! Pin
lemur14-Sep-10 6:55
lemur14-Sep-10 6:55 
GeneralRe: Lifesaver!!!! Pin
Dewey Vozel16-Sep-10 1:53
Dewey Vozel16-Sep-10 1:53 
QuestionWhen press enter the app. runs Pin
melankolik6610-Sep-09 0:38
melankolik6610-Sep-09 0:38 
GeneralWait Extension Pin
JoPa317-Aug-09 3:19
JoPa317-Aug-09 3:19 
QuestionRunAs error in XP sp2 Pin
Jake1234565-May-07 1:39
Jake1234565-May-07 1:39 
AnswerRe: RunAs error in XP sp2 Pin
Jake1234565-May-07 2:23
Jake1234565-May-07 2:23 
AnswerRe: RunAs error in XP sp2 Pin
Dewey Vozel5-May-07 7:54
Dewey Vozel5-May-07 7:54 
GeneralRe: RunAs error in XP sp2 Pin
Jake1234567-May-07 2:31
Jake1234567-May-07 2:31 
GeneralRe: RunAs error in XP sp2 Pin
Naresh N Jamadagni6-Nov-08 16:42
Naresh N Jamadagni6-Nov-08 16:42 
Questioncan u runas user from other computer? Pin
mikilior17-Jul-06 20:52
mikilior17-Jul-06 20:52 
QuestionCan I??? Pin
tknman070019-Jun-06 8:10
tknman070019-Jun-06 8:10 
AnswerRe: Can I??? Pin
Dewey Vozel19-Jun-06 13:38
Dewey Vozel19-Jun-06 13:38 
GeneralRedirecting stdout, stderr, stdin Pin
davelogie27-Feb-06 7:40
davelogie27-Feb-06 7:40 
This is a great class. I kept getting "...failed to init properly (0xc0000142)" error when using System.Diagnotics.Process directly. I found thru another source that I needed to set the StartInfo.lpDesktop = @"winsta0\default". This solution solved that major headache, however, I still need the stream redirection of the Process class. Anyone taken the trouble to do that or would know how? I don't see it.

Thanks,
Dave
AnswerRe: Redirecting stdout, stderr, stdin Pin
Dewey Vozel28-Feb-06 13:20
Dewey Vozel28-Feb-06 13:20 
GeneralLicense Pin
Stefan Rusek11-Jan-06 3:19
Stefan Rusek11-Jan-06 3:19 
GeneralRe: License Pin
Dewey Vozel13-Jan-06 12:27
Dewey Vozel13-Jan-06 12:27 
GeneralStarting process from Windows Service Pin
mrdance13-Jul-05 7:16
mrdance13-Jul-05 7:16 
AnswerRe: Starting process from Windows Service Pin
davelogie27-Feb-06 7:44
davelogie27-Feb-06 7:44 

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.