TeraTerm macro immediately closes when run as a process from c#

970 Views Asked by At

I'm currently working on a Winforms program from Visual Studio that acts as a control panel for a whole bunch of TeraTerm macros. I did a dumb and didn't add my first working version to version control, and now it's stopped working and I have no idea why/how to get back to what I had.

The function is question is

using System;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;

...

namespace Test
{
    public partial class MainWindow : Form
    {

        ...

        private void ImagesButton_Click(object sender, EventArgs e)
        {
            RunMacro("F:\\Users\\Isaac\\Documents\\LED Sign Commands\\Macros\\StartDisplayImages.ttl");
        }

        ....

        private void RunMacro(string userArgument)
        {
            panel1.Enabled = false;
            StatusLabel.Visible = true;
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                WindowStyle = ProcessWindowStyle.Hidden,
#if DEBUG
                FileName = "F:\\Users\\Isaac\\Documents\\LED Sign Commands\\teraterm\\ttpmacro.exe",
#else
                FileName = "..\\teraterm\\ttpmacro.exe",
#endif
                Arguments = userArgument
            };
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            panel1.Enabled = true;
            StatusLabel.Visible = false;
        }

        ...

    }
}

When run, I see that ttpmacro.exe does start, and if I omit the Arguments assignment then it will prompt me to select a macro; if I select StartDisplayImages.ttl, it will run as expected. If I include it as an argument as above, however, then ttpmacro still opens but immediately closes. No error comes up (and RedirectStandardOutput/Error produce nothing), it's as if ttpmacro accepts the file but won't do anything with it. I've confirmed both filepaths are valid and correct.

While I didn't add version control, I did extract the main file using ILSpy, and my original functions in the working version were:

    private void ImagesButton_Click(object sender, EventArgs e)
    {
        RunMacro("/V ..\\Macros\\StartDisplayImages.ttl");
    }

    private void RunMacro(string userArgument)
    {
        panel1.Enabled = false;
        StatusLabel.Visible = true;
        Process process = new Process();
        ProcessStartInfo startInfo = (process.StartInfo = new ProcessStartInfo
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "..\\teraterm\\ttpmacro.exe",
            Arguments = userArgument
        });
        process.Start();
        process.WaitForExit();
        panel1.Enabled = true;
        StatusLabel.Visible = false;
    }

Being from the published release, the filepaths are relative to the folder of the application. Other than that, the only difference seems to be minor syntax in how process.StartInfo is assigned, but I tried reverting that with no luck. Target framework is .NET Core 3.1. The /V flag isn't the issue; removing it simply makes the ttpmacro window visible for the fraction of a second it runs before closing. If I use a commandline execution of the same file (eg start "F:/Users/.../ttpmacro.exe" "F:/.../StartDisplayImages.ttl"), it also runs as expected.

1

There are 1 best solutions below

0
On

It turned out the problem was the spaces in the full macro filepath, and surrounding it with escaped double quotes resolved the issue. TeraTerm just wasn't telling me that it wasn't finding the file. Should have been obvious, but I was sure it had been working previously when I was debugging, without requiring the quotes.