Skip to content

Commit

Permalink
Added an overloaded versions of the StartRegularProcess() and StartEl…
Browse files Browse the repository at this point in the history
…evatedProcess() public methods with command-line arguments support.
  • Loading branch information
xvitaly committed Apr 22, 2024
1 parent 7a056f3 commit 55b83e5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 53 deletions.
63 changes: 61 additions & 2 deletions src/mhlib/CurrentPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,63 @@ public virtual int StartRegularProcess(string FileName)
return Process.Start(FileName).Id;
}

/// <summary>
/// Start the required application as the current user with specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public virtual int StartRegularProcess(string FileName, string Arguments)
{
ProcessStartInfo ST = new ProcessStartInfo
{
FileName = FileName,
Arguments = Arguments
};
return Process.Start(ST).Id;
}

/// <summary>
/// Start the required application as an administrator.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public virtual int StartElevatedProcess(string FileName)
{
return StartElevatedProcess(FileName, string.Empty);
}

/// <summary>
/// Start the required application as an administrator with the specified
/// command-line arguments and external helper tool.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <param name="ExternalHelper">External helper application for elevating permissions.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
protected virtual int StartElevatedProcess(string FileName, string Arguments, string ExternalHelper)
{
// Setting advanced properties...
ProcessStartInfo ST = new ProcessStartInfo
{
FileName = FileName,
Arguments = Arguments,
Verb = ExternalHelper,
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = true
};

// Starting process...
Process NewProcess = Process.Start(ST);

// Returning PID of created process...
return NewProcess.Id;
}

/// <summary>
/// Get platform-dependent suffix for HTTP_USER_AGENT header.
/// </summary>
Expand Down Expand Up @@ -184,11 +241,13 @@ public virtual int StartRegularProcess(string FileName)
public abstract void OpenExplorer(string FileName);

/// <summary>
/// Start the required application from administrator.
/// Start the required application as an administrator with the specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public abstract int StartElevatedProcess(string FileName);
public abstract int StartElevatedProcess(string FileName, string Arguments);
}
}
24 changes: 21 additions & 3 deletions src/mhlib/IPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,35 @@ public interface IPlatform
void RestartApplicationAsAdmin();

/// <summary>
/// Start the required application as the current user.
/// Start the required application as an administrator with the specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
int StartRegularProcess(string FileName);
int StartElevatedProcess(string FileName, string Arguments);

/// <summary>
/// Start the required application from administrator.
/// Start the required application as an administrator.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <returns>PID of the newly created process.</returns>
int StartElevatedProcess(string FileName);

/// <summary>
/// Start the required application as the current user with specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
int StartRegularProcess(string FileName, string Arguments);

/// <summary>
/// Start the required application as the current user.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <returns>PID of the newly created process.</returns>
int StartRegularProcess(string FileName);
}
}
21 changes: 5 additions & 16 deletions src/mhlib/PlatformLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,16 @@ public override void OpenExplorer(string FileName)
}

/// <summary>
/// Start the required application from administrator.
/// Start the required application as an administrator with the specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public override int StartElevatedProcess(string FileName)
public override int StartElevatedProcess(string FileName, string Arguments)
{
// Setting advanced properties...
ProcessStartInfo ST = new ProcessStartInfo
{
FileName = FileName,
Verb = "pkexec",
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = true
};

// Starting process...
Process NewProcess = Process.Start(ST);

// Returning PID of created process...
return NewProcess.Id;
return StartElevatedProcess(FileName, Arguments, "pkexec");
}

/// <summary>
Expand Down
21 changes: 5 additions & 16 deletions src/mhlib/PlatformMac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,16 @@ public override void OpenExplorer(string FileName)
}

/// <summary>
/// Start the required application from administrator.
/// Start the required application as an administrator with the specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public override int StartElevatedProcess(string FileName)
public override int StartElevatedProcess(string FileName, string Arguments)
{
// Setting advanced properties...
ProcessStartInfo ST = new ProcessStartInfo
{
FileName = FileName,
Verb = "sudo",
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = true
};

// Starting process...
Process NewProcess = Process.Start(ST);

// Returning PID of created process...
return NewProcess.Id;
return StartElevatedProcess(FileName, Arguments, "sudo");
}

/// <summary>
Expand Down
21 changes: 5 additions & 16 deletions src/mhlib/PlatformWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,16 @@ public override void OpenExplorer(string FileName)
}

/// <summary>
/// Start the required application from administrator.
/// Start the required application as an administrator with the specified
/// command-line arguments.
/// </summary>
/// <param name="FileName">Full path to the executable.</param>
/// <param name="Arguments">Command-line arguments.</param>
/// <returns>PID of the newly created process.</returns>
[EnvironmentPermission(SecurityAction.Demand, Unrestricted = true)]
public override int StartElevatedProcess(string FileName)
public override int StartElevatedProcess(string FileName, string Arguments)
{
// Setting advanced properties...
ProcessStartInfo ST = new ProcessStartInfo
{
FileName = FileName,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = true
};

// Starting process...
Process NewProcess = Process.Start(ST);

// Returning PID of created process...
return NewProcess.Id;
return StartElevatedProcess(FileName, Arguments, "runas");
}

/// <summary>
Expand Down

0 comments on commit 55b83e5

Please sign in to comment.