Solved

.Net web application to start another application as a process

Posted on 2011-02-17
12
907 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now