0

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 Notepad or Skype, but it doesn't work for other applications such as TeamSpeak.
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
            break;
        }
    }
}  

I use InputSimulator to send the input.

ViVi
  • 4,339
  • 8
  • 29
  • 52
Bogdan Molinger
  • 251
  • 1
  • 6
  • 19

2 Answers2

0

Could you just try this? I think this should work for you.

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;
            HwndSource hwndSource = HwndSource.FromHwnd(hWnd);
            Window window = hwndSource.RootVisual as Window;
            window.Activate();
            window.TopMost = true;

            ShowWindow(hWnd, 3);
            SetForegroundWindow(hWnd); //set to topmost
            break;
        }
    }
}
ViVi
  • 4,339
  • 8
  • 29
  • 52
0

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.

Bogdan Molinger
  • 251
  • 1
  • 6
  • 19