Colin
Colin

Reputation: 415

Why does my compiled AutoIt script fail to return when called from C#?

My compiled AutoIt script runs fine by itself but when called from C# it will run but does not finish executing. My compiled C# code:

Process myExe = new Process();
Environment.CurrentDirectory = Path.GetDirectoryName(exeDir); //exeDir is full path of the AutoIt executable directory
ProcessStartInfo StartInfo = new ProcessStartInfo("");
StartInfo.FileName = exeFile; //exeFile is full path of the executable itself
StartInfo.Verb = "runas";
myExe = Process.Start(StartInfo);

It halts on EnvUpdate() in the AutoIt code and eventually again on some other function. None of this happens if I run the executable manually.

I started a batch file from C# that runs this executable. And combinations of CreateNoWindow and UseShellExecute. Also with no success: 1) Run the C# compiled executable as Admin. 2) Use combinations of StartInfo.CreateNoWindow and StartInfo.UseShellExecute. 3) Run AutoIt executable from a batch file. 4) Run AutoIt executable from a Perl file, through a batch file. 5) Run AutoIt executable from a Windows scheduled task. 6) Run AutoIt executable without ProcessStartInfo, with either:

myExe = Process.Start("cmd.exe", "/c start /wait " + exeFile);

or

myExe = Process.Start(exeFile);

Upvotes: 2

Views: 680

Answers (1)

user10815634
user10815634

Reputation:

Here an example C# class for an external call to a compiled AutoIt script (EXE file) using .WaitForExit(), which should be your problem:

using System.Configuration;
using System.Diagnostics;

namespace RegressionTesting.Common.ThirdParty
{
    public class ClickAtBrowserPosition
    {
        public static void CallClickAtBrowserPosition(string currentBrowserWindowTitle, int xPosition, int yPosition)
        {
            var pathClickAtBrowserPosition = ConfigurationManager.AppSettings["pathClickAtBrowserPosition"];
            var clickAtBrowserPositionExe = ConfigurationManager.AppSettings["clickAtBrowserPositionExe"];

            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                WindowStyle = ProcessWindowStyle.Hidden, // don't show the cmd.exe which calls the program
                FileName = "cmd.exe",
                Arguments = @" /C cd " +
                            $@"""{pathClickAtBrowserPosition}"" && {clickAtBrowserPositionExe} ""{currentBrowserWindowTitle}"" {xPosition} {yPosition}"
            };

            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit(); // wait to finish the execution
        }
    }
}

The compiled AutoIt script is used for clicking a relative position of a window (in this case a browser).

Upvotes: 0

Related Questions