Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Add the ability to allow output from a TCC .NET plugin to be re-directed.

Aug
2,819
147
Please add the ability to allow output from a TCC .NET plugin to be re-directed.

Using Console.WriteLine from a .NET plugin will not allow the output to be re-directed from TCC.

Here is my work-around.

Code:
internal static class NativeStdout
{
    private const int STD_OUTPUT_HANDLE = -11;

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool WriteFile(
        IntPtr hFile,
        byte[] buffer,
        uint bytesToWrite,
        out uint bytesWritten,
        IntPtr overlapped);

    public static void WriteLine(string text)
    {
        // Choose an encoding that matches the output encoding expected by TCC.
        byte[] bytes = Encoding.UTF8.GetBytes(text + "\r\n");
        IntPtr stdout = GetStdHandle(STD_OUTPUT_HANDLE);

        if (stdout == IntPtr.Zero || stdout == new IntPtr(-1))
            throw new InvalidOperationException("Standard output handle is unavailable.");

        if (!WriteFile(stdout, bytes, (uint)bytes.Length, out uint written, IntPtr.Zero)
            || written != bytes.Length)
        {
            throw new System.ComponentModel.Win32Exception(
                Marshal.GetLastWin32Error());
        }
    }
}

This cannot be re-directed from TCC;
Code:
Console.WriteLine(script.Hello());

This can be re-directed from TCC;
Code:
NativeStdout.WriteLine((string)script.Hello());

This cannot be re-directed from TCC;
Code:
Console.WriteLine(
  "{0},{1},{2},{3},{4}",
  fields[0],
  fields[1],
  fields[2],
  Csv(fields[3]),
  Csv(fields[4]));

This can be re-directed from TCC;
Code:
NativeStdout.WriteLine(string.Format(
  "{0},{1},{2},{3},{4}",
  fields[0],
  fields[1],
  fields[2],
  Csv(fields[3]),
  Csv(fields[4])));

I had to do the same when I was writing TCC Plugins using PureBasic,
but I'm hoping that @rconn can add this feature to the .NET Plugin SDK,
so that I don't have to add this work-around to each plugin .cs code file,
as I had to when using PureBasic.

Reference: jlcUtils/ReDirectionPureBasic.md at main · joec4281/jlcUtils
Reference: jlcUtils/jlcutils.cs at main · joec4281/jlcUtils

Joe
 
Last edited:
Back
Top