Mousa Shawar
Mousa Shawar

Reputation: 1

How to run an external exe in UserInteractive mode?

I have a situation that requires me to run an external console .NET framework app which in turn starts a Windows Forms application that embeds WebView2.

This external exe needs to be called from a Web API controller, which causes this error to appear when calling form.ShowDialog() in exe:

Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

How can I override behavior/allow exe to start the form?

Interesting note, when running the Web API in IIS Express, everything works fine, but that issue arises when running it in local IIS, that's why I try to run process as a different user.

This is how I start the process from Web API controller method:

    public string RunExternalExe(string filename, string arguments = null)
    {
        var process = new Process();

        var ssPwd = new System.Security.SecureString();

        process.StartInfo.Domain = "domain";
        process.StartInfo.UserName = "username";
        process.StartInfo.LoadUserProfile = true;

        string password = "****";

        for (int x = 0; x < password.Length; x++)
        {
            ssPwd.AppendChar(password[x]);
        }

        password = "";
        process.StartInfo.Password = ssPwd;

        process.StartInfo.FileName = filename;

        if (!string.IsNullOrEmpty(arguments))
        {
            process.StartInfo.Arguments = arguments;
        }

        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.StartInfo.UseShellExecute = false;

        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;
        
        var stdOutput = new StringBuilder();
        process.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data); // Use AppendLine rather than Append since args.Data is one line of output, not including the newline character.

        string stdError = null;

        try
        {
            process.Start();
            process.BeginOutputReadLine();
            stdError = process.StandardError.ReadToEnd();
            process.WaitForExit();
        }
        catch (Exception e)
        {
            throw new Exception("OS error while executing " + Format(filename, arguments) + ": " + e.Message, e);
        }

        if (process.ExitCode == 0)
        {
            return stdOutput.ToString().Trim();
        }
        else
        {
            var message = new StringBuilder();

            if (!string.IsNullOrEmpty(stdError))
            {
                message.AppendLine(stdError);
            }

            if (stdOutput.Length != 0)
            {
                message.AppendLine("Std output:");
                message.AppendLine(stdOutput.ToString());
            }

            throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
        }
    }

Upvotes: 0

Views: 62

Answers (0)

Related Questions