Bogdan Molinger Bogdan Molinger - 11 months ago 53
C# Question

Bringing another application into foreground and sending input to it

I am trying to restore( or even maximize ) another application and send input to it from my project. I can do it for certain applications like

, but it doesn't work for other applications such as

Any ideas why and how I can solve the problem ?

Here's my code :

private void winact()
IntPtr hWnd; //change this to IntPtr
Process[] processRunning = Process.GetProcesses();

string title, name;
foreach (Process pr in processRunning)
title = pr.MainWindowTitle.ToLower();
name = pr.ProcessName.ToLower();

if (title.Contains("teamspeak".ToLower()) || name.Contains("teamspeak".ToLower()))
hWnd = pr.MainWindowHandle;
ShowWindow(hWnd, 3);
SetForegroundWindow(hWnd); //set to topmost

I use InputSimulator to send the input.

Answer Source

Apparently the problem was that my application did not run with administrator rights. In order to be able to send input to other apps efficiently, the role of administrator is required.

To do this, I've added an app.manifest to the application, and changed requestedExecutionLevel level from asInvoker to requireAdministrator.
Using InputSimulator worked for other applications, but certain applications, such as games, consider the input to be a simple message and do not process keystrokes as actions. They are however passed to the chat. (this is what I've noticed)

In order to solve this issue, I have written a very script in autoIT which is simply taking the parameter passed to it (only 1) and sends the parameter as a keystroke to the window, currently in foreground. The script is compiled resulting in an executable which I am calling.

I am sure that there is a better way of doing it, but this is the best I've managed to do.