I am writing a .Net application trying to automate keystrokes in a Thin Terminal called Persona. I can open the program and set my focus to it but what I need to do is click the connect button in the menu bar. I used Spy++ to look at the window properties.

This is my result.


[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
 public static extern IntPtr FindWindow(string lpClassName,
 string lpWindowName);

 // Activate an application window.
 public static extern bool SetForegroundWindow(IntPtr hWnd);

 protected void Button2_Click(object sender, EventArgs e)
  IntPtr dixiehandle = FindWindow("ToolBar", "Dixie Information Network - Persona");

I would like to do this but the handle is return 0. Any thoughts?

I was working on an app a while ago that dealt with getting handles from window names and I ran into a lot of problems. In short, I wanted the handle of a Notepad window I had open, whose name on the screen appeared to be "Untitled - Notepad". I can't remember exactly what was going on, but it did not work as expected at all. It was incredibly difficult to get a specific window handle by name.

I ended up going with

        public static extern IntPtr GetForegroundWindow();

since the window I wanted was always going to be in the foreground, as my app was meant to be in the background. Not sure if that would apply to you however.

Edit: Just reread your post and realized you're wanting to set the foreground window, so you obviously couldn't GetForegroundWindow(). Doh!

Sorry I can't help more! I'm digging through my old work to see if anything jogs my memory about it.

I actually posted a thread on here sort of concerning this http://www.daniweb.com/software-development/csharp/threads/327070

N4JRY helped me out by pointing me to http://ryanfarley.com/blog/archive/2004/05/10/605.aspx

That article explains how to send messages to a specific window going through its running process. I bet you can do something similar by first getting the process for Persona

Process[] processes = Process.GetProcessesByName("PersonaProcessName");
if (processes.Length > 1)
    //iterate through all running target applications
    foreach (Process p in processes)
        // FYI, this is where I was running into the problems getting the process I wanted. Turns out the process that actually hosted the window I wanted to get the handle for wasn't the "intuitive" one
        //if (p is the process we want)