Overview
.NET plugins can execute TCC commands on the host process using the `TakeCommand.PluginHost` static class. This allows a plugin to run any
command that TCC supports (internal commands, aliases, batch files, etc.) and capture the text output.
The API is available automatically when your plugin is loaded by TCC — no additional references or initialization are required beyond the
standard plugin SDK (`DotNetPluginSDK.cs`).
`TakeCommand.PluginHost.InvokeCommand(string command)`
Executes a TCC command string and returns its captured output.
**Namespace:** `TakeCommand`
**Class:** `PluginHost` (static)
**Parameters:**
| Parameter | Type | Description |
|-----------|----------|-------------------------------------------------|
| `command` | `string` | The TCC command to execute (e.g. `"dir /b C:\\"`) |
**Returns:** `string` — the text output produced by the command.
**Exceptions:**
| Exception | Condition |
|----------------------------|-----------------------------------------------------|
| `InvalidOperationException`| The host callback is not initialized (plugin loaded outside TCC), or the command failed with an error. |
| `ArgumentException` | `command` is `null` or empty. |
Usage Example
C#
using System;
using System.Text;
using TakeCommand.Plugin;
public class MyPlugin : ITCCPlugin
{
private static readonly TCCPluginInfo _info = new TCCPluginInfo
{
Name = "MyPlugin",
Author = "Your Name",
Email = "[email protected]",
WWW = "https://example.com",
Description = "Demonstrates calling TCC commands from .NET",
Functions = "DIRCOUNT,@TCCEVAL",
Major = 1, Minor = 0, Build = 1
};
public TCCPluginInfo GetPluginInfo() => _info;
public bool Initialize() => true;
public bool Shutdown(bool end) => true;
// Command: DIRCOUNT <path>
// Prints the number of files in the given directory.
public int DIRCOUNT(StringBuilder args)
{
string path = args.ToString().Trim();
if (string.IsNullOrEmpty(path))
path = ".";
try
{
string output = TakeCommand.PluginHost.InvokeCommand($"dir /b \"{path}\"");
int count = output.Split(
new[] { '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries).Length;
Console.WriteLine($"{count} file(s) in {path}");
return 0;
}
catch (InvalidOperationException ex)
{
Console.Error.WriteLine($"DIRCOUNT: {ex.Message}");
return 1;
}
}
// Variable function: %@TCCEVAL[command]
// Returns the captured output of an arbitrary TCC command.
public int f_TCCEVAL(StringBuilder args)
{
string command = args.ToString().Trim();
if (string.IsNullOrEmpty(command))
return 1;
try
{
string result = TakeCommand.PluginHost.InvokeCommand(command);
args.Clear();
args.Append(result.TrimEnd('\r', '\n'));
return 0;
}
catch (InvalidOperationException)
{
args.Clear();
return 1;
}
}
}
Using the command in TCC
C:\> dircount C:\Windows\System32
14326 file(s) in C:\Windows\System32
C:\> echo %@tcceval[echo Hello from TCC]
Hello from TCC
Error Handling
`InvokeCommand` throws `InvalidOperationException` when the host reports an error. Always wrap calls in a `try`/`catch` block in production plugins:
C#
try
{
string output = TakeCommand.PluginHost.InvokeCommand("copy file1 file2");
}
catch (InvalidOperationException ex)
{
// ex.Message contains the TCC error text or a generic failure message
Console.Error.WriteLine(ex.Message);
}
Notes:
Thread safety: `InvokeCommand` calls back into TCC synchronously on the calling thread. Avoid calling it from background threads unless
you are certain TCC's command processor is re-entrant for the command you are invoking.
Output capture: The command's standard output is redirected to a temporary file and read back, so commands that write to the console
will have their output captured and returned as a string. Commands that produce no output return an empty string.
Return value vs. output: The method returns the command's *text output*, not its exit code. If the command fails (non-zero exit code), `InvalidOperationException` is thrown with the error description.
Availability: The host callback is registered when TCC loads the plugin. If your plugin is loaded in a different host (unit test
runner, standalone app), `InvokeCommand` throws `InvalidOperationException` with the message *"Take Command command interface is not initialized."*