• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2113
  • Last Modified:

Getting the list of Open Window Handles in C#

I am trying to mimick the functionality of ALT TAB, by allowing the user to cycle through the list of running applications (open window handles).  This works fine using Process.GetProcesses(), and using the MainWindowTitle property to see if its not empty.  This however does not work for an explorer window, since its process is "explorer", and doesn't have an open window handle associated with it.
0
sfairbanks
Asked:
sfairbanks
  • 7
  • 4
1 Solution
 
abelCommented:
You can use FindWindow(null, null), but you need P/Invoke to use it. Here's how: http://www.pinvoke.net/default.aspx/user32.FindWindow.

If you pass in two null parameters, it will enumerate all the main windows.
0
 
abelCommented:
Don't forget that "null" should be IntPtr.Zero when doing p/Invoke. ;-)
0
 
sfairbanksAuthor Commented:
If I specify FindWindow(null, null) I get a handle, but I don't know what it is.  What do you mean by using P/Invoke to use it?  I don't see anything on that link shows how to use P/Invoke with FindWindow().

Also, I'm still not able to get the explorer window to show in the forground.  Do I have to reference it by it's name in FindWindow()?  Isn't there a call to get the list of all the open window handles without having to know any of their names?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
abelCommented:
No, there's no call to do that. As you found out yourself, from using .NET you can only get the main window handles. To get also all other (top level) window handles, you can use FindWindow and FindWindowEx.

Let's see if I can dig up some code that gives a list of all running windows and captions. Setting the focus can be tricky, because on XP and Vista that can be blocked by the OS, depending on security (iirc)....
0
 
sfairbanksAuthor Commented:
The problem with FindWindow and FindWindowEx for the explorer window is that the name/caption changes according to which folder is currently selected in the left tree view.
0
 
abelCommented:
> Isn't there a call to get the list of all the open window handles without having to know any of their names?

this will give you all the window captions of the explorer windows. On Vista, the explorer has the classname CabinetWClass, not sure if it is the same on other versions of windows, use Spy++ to find out:

IntPtr handleWin = IntPtr.Zero;
while (IntPtr.Zero != (handleWin = FindWindowEx(IntPtr.Zero, handleWin, "CabinetWClass", IntPtr.Zero)))
{
    // Allocate correct string length first
    int length = GetWindowTextLength(handleWin);
    if (length > 0)
    {
        StringBuilder sb = new StringBuilder(length + 1);
        GetWindowText(handleWin, sb, sb.Capacity);
        textBox1.Text += sb.ToString() + "\r\n";
    }
}

Open in new window

0
 
abelCommented:
forgot to include the p/invoke declarations. Here they are:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
 
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);

Open in new window

0
 
sfairbanksAuthor Commented:
I'm working with Windows XP, and  I've never used Spy++.  I don't see it offered in Developer Studio either.  Can you let me know what class name I should be looking for instead of ""CabinetWClass"?    As of now this FindWindowEx doesn't return anything.
0
 
abelCommented:
If you installed developer studio, Spy+ is installed alongside and can be found under the Program Menu.

> Can you let me know what class name I should be looking for instead of ""CabinetWClass"?
I would love to let you know that, but I can't, as I don't have XP here... But googling it gives many posts, along them this one (quote):

I'm running XP service pack 2 and explorer windows have the CabinetWClass classname.
Another rather clumsy post suggests that it may also be "Progman": http://lifehacker.com/355580/mimic-vistas-file-rename-feature-in-xp.

And here (more reliable) they say that it may also be "ExloreWClass": http://www.codeproject.com/KB/files/TrayProdder.aspx

to know this for sure, you really need to get this Spy++ to work. Any tasks with FindWindow or sending win32 messages can benefit greatly from that tool. Unfortunately there's no public download of spy++. Can you check your program files for spyxx.exe?
0
 
sfairbanksAuthor Commented:
hwnd = FindWindowEx(IntPtr.Zero, hwnd, "ExploreWClass", IntPtr.Zero) was the magic line.  And I learned how to run Spy++ for the first time.
0
 
abelCommented:
> hwnd = FindWindowEx(IntPtr.Zero, hwnd, "ExploreWClass", IntPtr.Zero) was the magic line. > And I learned how to run Spy++ for the first time.

cool! I'm sure that you got from this discussion that if you publish your program, you should be aware that the classname can be different on any version of windows....
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now