C# switching windows in .net

.net, c#, dllimport, process

Solution

SwitchToThisWindow is expecting a handle to the window that you want to switch to in that process.

Try

SwitchToThisWindow(proc.MainWindowHandle);

Problem

Hi I'm trying to switch windows to other programs that are running (even if minimized) using C#. I'm wondering why this won't work. Error message: Argument 1: cannot convert from 'System.Diagnostics.Process' to 'System.IntPtr' By the time it hits the loop I would think that the proc variable would refer to the appropriate window handler. Is this not true? I really appreciate the help. ``` //declarations using system.IO; using System.Runtime.InteropServices; //more //namespace here //class here //initialize method //related .dll import [DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hWnd); String ProcWindow = "itunes"; //function which calls switchWindow() is here but not important //now we have switch window. private void switchWindow() { Process[] procs = Process.GetProcessesByName(ProcWindow); foreach (Process proc in procs) { //switch to process by name SwitchToThisWindow(proc); } } ``` For future readers: I got to this point in my code from another question. Correct way (in .NET) to switch the focus to another application

Original source

Related problems