Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

.Net web application to start another application as a process

Posted on 2011-02-17
12
Medium Priority
?
926 Views
Last Modified: 2012-05-11
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);
}
}
******************************************************
0
Comment
Question by:ToneElec
  • 6
  • 5
12 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34920139
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
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34921617
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
 

Author Comment

by:ToneElec
ID: 34928797
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ToneElec
ID: 34929989
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
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34930964
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
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34931038
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
 

Author Comment

by:ToneElec
ID: 34952909
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
 

Author Comment

by:ToneElec
ID: 34953168
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
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34953839
It doesn't have to be command line to execute from this. Let met get home and I'll write a specified one.
0
 
LVL 15

Accepted Solution

by:
Russell_Venable earned 2000 total points
ID: 34954237
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
 

Author Comment

by:ToneElec
ID: 34961175
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
 
LVL 15

Expert Comment

by:Russell_Venable
ID: 34964475
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question