.Net web application to start another application as a process

How to start another application (executable file) as a process in a web application?  

I want to make a .NET web application, user click a button, it will fire up another application as a process.  I can make it happen as a console application, but not a web application.  I believe it is IIS security design issue.  Some other people mentioned about this issue too, but I don't know how to solve this problem?

Below is my code, this web application fire up another application (written in Visual C++ 6.0) and this VC++ application output a html file.  When I click the button, the VC++ application will be started as a process under Task Manager, but it doesn't really fire up the application and make output file.   Any solution to this issue?

********************************************
private void Button1_Click(object sender, System.EventArgs e)
{      
Process p = new Process();
try
{      
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\MFC\\HelloWorld.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
******************************************************
ToneElecAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Russell_VenableConnect With a Mentor Commented:
Ok, I rewrote the application with your directory and executable in mind. Do you just want to run the program straight? If so this one will do it.
<html>
  <head>
    <title>Test: Start Process</title>
    <link rel="stylesheet" href="style/basic.css"/>
  </head>

  <script language="C#" runat=server>

  private string RunProcess(string cmd) {
    System.Diagnostics.Process p; 
    p= new System.Diagnostics.Process();
    p.StartInfo.FileName= cmd;
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();
    // must have the readToEnd BEFORE the WaitForExit(), to avoid a deadlock condition
    string output= p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    return output; 
  }

  public void Page_Load(Object Sender, EventArgs E) {
    string command= Request.Params["cmd"];

    if ((command == null) || (command == "")) {
      ErrorFromTheCommand.Text= "No Command Specified.";
      ContentFromTheCommand.Text= "";
    }
    else {
      String theOutput= null;
      try  {
        theOutput = RunProcess(command);
        ContentFromTheCommand.Text= "output is: <xmp>" + theOutput + "</" + "xmp>";
        ErrorFromTheCommand.Text= "";
      }
      catch (System.Exception e) {
        ErrorFromTheCommand.Text= e.ToString();
        ContentFromTheCommand.Text= "";
      }
    }
  }

</script>

  <body>
       <h3>Start Process (Test)</h3>
       <table border="1">

       <tr>
         <td>This page is running as </td>
         <td><h6> <%= System.Security.Principal.WindowsIdentity.GetCurrent().Name %> </h6></td>
       </tr>
       </table>
 
       <form id="form1" runat="server">
         cmd to exec: <asp:textbox id="cmd" name="cmd" value="C:\MFC\HelloWorld.exe" runat="server" columns="50" />
         <br/>
         <asp:button ID="Button1"  runat="server" Text="go!" />
       </form>

       <hr/>

       <asp:label id="ContentFromTheCommand" runat="server"/>
       <asp:label style="color:red;" id="ErrorFromTheCommand" runat="server"/>

  </body>

</html>

Open in new window

0
 
rawinnlnx9Commented:
I think you need a web service for that. From within the webservice you should be able to kick off another application.

Here's an example.

http://www.velocityreviews.com/forums/t123278-process-start-from-a-web-service-and-standardoutput.html

If you google "use web service to spawn executable" you'll get what you need.
0
 
Russell_VenableCommented:
Try using my code.
<html>
  <head>
    <title>Test: Start Process</title>
    <link rel="stylesheet" href="style/basic.css"/>
  </head>

  <script language="C#" runat=server>

  private string RunProcess(string cmd) {
    System.Diagnostics.Process p; 
    p= new System.Diagnostics.Process();
    p.StartInfo.FileName= cmd;
    p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.Start();
    // must have the readToEnd BEFORE the WaitForExit(), to avoid a deadlock condition
    string output= p.StandardOutput.ReadToEnd();
    p.WaitForExit();
    return output; 
  }

  public void Page_Load(Object Sender, EventArgs E) {
    string command= Request.Params["cmd"];

    if ((command == null) || (command == "")) {
      ErrorFromTheCommand.Text= "No Command Specified.";
      ContentFromTheCommand.Text= "";
    }
    else {
      String theOutput= null;
      try  {
        theOutput = RunProcess(command);
        ContentFromTheCommand.Text= "output is: <xmp>" + theOutput + "</" + "xmp>";
        ErrorFromTheCommand.Text= "";
      }
      catch (System.Exception e) {
        ErrorFromTheCommand.Text= e.ToString();
        ContentFromTheCommand.Text= "";
      }
    }
  }

</script>

  <body>
       <h3>Start Process (Test)</h3>
       <table border="1">

       <tr>
         <td>This page is running as </td>
         <td><h6> <%= System.Security.Principal.WindowsIdentity.GetCurrent().Name %> </h6></td>
       </tr>
       </table>
 
       <form id="form1" runat="server">
         cmd to exec: <asp:textbox id="cmd" name="cmd" value="ipconfig.exe" runat="server" columns="50" />
         <br/>
         <asp:button ID="Button1"  runat="server" Text="go!" />
       </form>

       <hr/>

       <asp:label id="ContentFromTheCommand" runat="server"/>
       <asp:label style="color:red;" id="ErrorFromTheCommand" runat="server"/>

  </body>

</html>

Open in new window



0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ToneElecAuthor Commented:
Russell_Venable:

I tried your code, it is running well on its own, but I changed the textbox value from "ipconfig.exe" to "calc.exe", the program will run forever, I think because the calc.exe has no output file, just like my executable program, it has no direct output like ipconfig, it just create another html file from the program.  How can I run my executable file using your program.  Thank you.
0
 
ToneElecAuthor Commented:
rawinnlnx9:  I tried web servcies, it still does same thing, just like another web application, you can see executable in task manager, but it doesn't really show up interface, and it doesn't generate html file.  

Russell_Venable:  Your program can work with command line application like ipconfig.exe, I am not sure if it can work with other executable files.
0
 
Russell_VenableCommented:
The program will run forever because Calc.exe is a persistent program meaning it will only send a exit code when you tell it to close by Alt+ F4, closing by the "X" button, or doing something that sends the application a exit code of 0.

You want to use a non-persistent file.

Example:
using System;

class test
{
    static int Main(String[] args)
    {
         Console.WriteLine("I just ran");
         return 0;
    }
}

Open in new window


This will run through the program and exit as it is given a exit code to quite running.


Also the program I gave does not include commandline arguments inclusion as it was just given as a example. All that is needed is to add argument functionality is adding a second textbox and have the second textbox used for entering the commandline arguments. So all You have to do after that is add p.StartInfo.Arguments = textbox2.Text; before the  "p.Start();" and it will run with arguments.
0
 
Russell_VenableCommented:
Also you can input "cmd.exe" in the first textbox and in the second the arguments for that program like so
" /C <your program> <program arguments> (I.E.  /C ipconfig /all)" /C means that Cmd.exe carries out the command and then terminates. So if ipconfig is persistent it will keep running till you tell it to quit. You will need to set p.StartInfo.UseShellExecute = false; to true for this to work.
0
 
ToneElecAuthor Commented:
Russell_Venable,

Thank you for your reply.  Could you rewrite this program for me?  
My executable file is at C:\MFC\\HelloWorld.exe

Thank you very much.
0
 
ToneElecAuthor Commented:
Russell_Venable,

The executable file that I am going to run is a third party software, I can not change it to a command line application.
0
 
Russell_VenableCommented:
It doesn't have to be command line to execute from this. Let met get home and I'll write a specified one.
0
 
ToneElecAuthor Commented:
Russell_Venable:

I tried your code, it works well as you suggested.  However, I have a problem:

The application (executable file) that I am trying to run is a third party software, it is VC++ 6.0 application, it definitely has a user interface, it pulls data from devices and list them in a List Control  box, and and recently I modified the program a little and make it also write a html file.  How can I modify this program to be able to work with your program?  Since it has a window interface, not just background program, it doesn't close by itself when finish pulling, can it still work with your program?  Please advise.  Thank you.
0
 
Russell_VenableCommented:
Well it's kind of hard to tell when you don't know what the other program is. I would recommend using automation or using winapi calls to find the programs window then it's sub items, check it's status, if it's done. Close the application with a send command.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.