See the question and my original answer on StackOverflow

Both these apps use UI Automation which is a standard Windows API.

For example, let's say you have a window (a Winform for example) that has a button, then you can click on it using the Invoke Pattern with a code like this, in any other application (like a Console app):

static void Main(string[] args)
    // get the app process
    var process = Process.GetProcessesByName("WindowsFormsApp1")[0];

    // get the element corresponding to the main handle (=> the form)
    var element = AutomationElement.FromHandle(process.MainWindowHandle); // needs a reference to UIAutomationClient

    // find the first button in that element
    var button = element.FindFirst(TreeScope.Subtree,
        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));  // needs a reference to UIAutomationTypes

    // get the pattern and invoke (=> click)
    button.TryGetCurrentPattern(InvokePattern.Pattern, out var p);
    var pattern = (InvokePattern)p;

If you own the form, you can even set the AccessibleName button property to something, for example "MyButton", and get it directly by this name, like this:

var button = element.FindFirst(TreeScope.Subtree,
    new PropertyCondition(AutomationElement.NameProperty, "MyButton"));