0

I am trying to Send Data to a running windows form application written in C# from my asp.net Webpage. The Problem that the Application is not starting when I call it from the webpage , but if I try to run it from command Line.

This is the Part of Code launching my application in asp.net

protected void Page_Load(object sender, EventArgs e)
{
   // ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:connectSocketServer(); ", true);
    SecureString sc = new SecureString();
    string Source = "*****";
    foreach (char c in Source.ToCharArray())
    {
        sc.AppendChar(c);
    }
    ProcessStartInfo i = new ProcessStartInfo(@"C:\******\myapp.exe", "test");
    i.UserName = "******";
    i.UseShellExecute = false;
    i.Password = sc;
    i.Verb = "runas";
    Process p = new Process();
    p.StartInfo = i;
    p.Start();    
}

and this is the Code from my application

static System.Threading.Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");

[STAThread]
static void Main(params string[] Arguments)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form1 mf = new Form1();

    if (mutex.WaitOne(TimeSpan.Zero, true))
    {
        Application.Run(mf);
        mutex.ReleaseMutex();
    }
    else
    {
        MessageBox.Show("only one instance at a time");
        mf.SendData(Arguments);

    }
}

and in the form there is a public function Called SendData that will process the Data...

So if I run it from cmd line and Pass arguments , SendData will be excuted , but from webpage not working...

Note : I remove the username and password information the application apears on task manager under IWAP user but form don't show.

  • Is the process visible in the task manager, or does it not execute at all? – Daniel Minnaar Sep 07 '16 at 08:53
  • if i run by cmd yes it will be visible but from webpage nop – Tamer Hatoum Sep 07 '16 at 08:54
  • Have you checked the Windows Event Logs for any detail about why the process won't start? Can you provide some values of the `p` properties, such as `HasExited`, `Responding` etc? – Daniel Minnaar Sep 07 '16 at 08:57
  • 1
    Also, can you explain what you're trying to achieve by sending data to a running Windows Forms application? Would a Windows Service not be better suited for the task? – Daniel Minnaar Sep 07 '16 at 08:58
  • I have Android application that will send data using my webservice and the webservice must send data to the application which is running , this application will process realtime application for any request from any android application. – Tamer Hatoum Sep 07 '16 at 09:01
  • If you have a webservice already, why don't you move the logic from your Windows Forms application into your webservice? – Daniel Minnaar Sep 07 '16 at 09:03
  • Because this windows form is running a websocket that all clients are connected to , and Other clients accessing by android are sending data using the web page , so any data will be send from android user will be directly forwarded to windows form users. Yes my process is responding and not exited. – Tamer Hatoum Sep 07 '16 at 09:15
  • So, what _does_ happen when you run this code? How do you determine it doesn't start the application? You do realize `Process.Start()` runs on the server, not the client? Does the application run on the web server? – CodeCaster Sep 07 '16 at 11:00
  • yes it is on server side . i can see it on task manager .Note : I remove the username and password information the application apears on task manager under IWAP user but form don't show – Tamer Hatoum Sep 07 '16 at 15:18

2 Answers2

0

Your process is starting, but it stopped responding because it has restricted permissions to run within the context of the ASP .NET worker process. Your new process doesn't have permission to access anything outside of this context.

You can either elevate the permission of the worker process, or allow the IIS service to interact with the desktop, as explained in another answer.

However, In the comments you mentioned your Windows Application is acting as a server for connected clients. Unless its just for testing purposes, I would recommend a different approach to these client sockets, by means of an API or Web Service. A Windows Service with .NET remoting is even better, but then I'd recommend just using WCF.

Community
  • 1
  • 1
Daniel Minnaar
  • 5,865
  • 5
  • 31
  • 52
-2

Tested.Hope this will solve your problem.Its a good practice to use Try Catch block

protected void Page_Load(object sender, EventArgs e)
    {

            SecureString sc = new SecureString();
            string Source = "*****";
            foreach (char c in Source.ToCharArray())
            {
                sc.AppendChar(c);

            }
            sc.MakeReadOnly();
            try
            {
                ProcessStartInfo i = new ProcessStartInfo(@"C:\******\myapp.exe", "test");
                i.Domain = "*****";
                i.UserName = "*****";
                i.UseShellExecute = false;
                i.CreateNoWindow = true;
                i.RedirectStandardOutput = true;
                i.Password = sc;
                i.Verb = "runas";
                Process p = new Process();
                p = Process.Start(i);
            }
            catch (Win32Exception w32E)
            {
               // Display Some Message
            }
}
A_D
  • 189
  • 1
  • 8
  • OP didn't say anything about exceptions being thrown. In fact, he specifically mentioned that the process was indeed running, just not doing anything. – Daniel Minnaar Sep 07 '16 at 10:04
  • Above code is working fine for windows form application. Just we need to do is to give correct path for eg. C:\Projects\WinApplication\WinApplication\obj\Debug\WinApplication.exe – A_D Sep 07 '16 at 10:43