简体   繁体   中英

Sending Data to Running Windows Application from asp.net

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 .

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.

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
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM