Solved

.Net web application to start another application as a process

Posted on 2011-02-17
12
922 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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