Answered Windows API GetNextWindow Problem

  • Saturday, March 03, 2012 6:59 AM
     
      Has Code

    Hi, I'm using the GetNext Window function to find and then show a steam chat window. Knowledge of steam shouldn't be necessary for answering this. There is a window which has handle "Friends," and the next window should be a window with a handle that contains "chat," but there is other text in the handle of that window. The windows appear in the text bar in the same way that an internet explorer window with two tabs open would appear. I am using the following code.

    var friends = FindWindow(null, "Friends");
                ShowWindow(friends, 3);
                var chat = GetNextWindow(friends, 2);
                ShowWindow(chat, 3);                        

    I am using 3 in the second parameter of showWindow because I want the windows  to be maximized. The part that shows the friends window works fine, but the part that shows the chat window makes my program freeze. I think that the get next window method probably isn't the right choice for this situation. Is there another method I should be using, or am I using get next window incorrectly?

    Thanks!


    -Winston

All Replies

  • Saturday, March 03, 2012 11:13 AM
     
     

    Hello,

    Did you check that the returned value is really the value you're expecting ?

    You can try and use FindWindowEx instead of GetNextWindow.

    P.s. var is not contributing to the readability of your code, especially when you're doing P/Invoke.


    Eyal (http://shilony.net), Regards.


    • Edited by Eyal Shilony Saturday, March 03, 2012 11:23 AM
    •  
  • Saturday, March 03, 2012 11:34 PM
     
     

    The var is IntPtr, I didn't know what it was going to be when I wrote the code.

    How can I check that the returned value is what I'm expecting it to be? And can you give me an example of how to use the FindWindowEx method? I didn't really understand the linked page.

    Thanks!


    -Winston

  • Sunday, March 04, 2012 12:46 AM
     
     Answered Has Code

    Here is an example I made long time ago.

    // NativeMethods.cs
    namespace Win32.PostMessageToApplication
    {
    	using System;
    	using System.Runtime.InteropServices;
    	using System.Windows.Forms;
    
    	internal static class NativeMethods
    	{
    		[DllImport("user32.dll", SetLastError = true)]
    		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    		[DllImport("user32.dll", SetLastError = true)]
    		public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
    		[return: MarshalAs(UnmanagedType.Bool)]
    		[DllImport("user32.dll", SetLastError = true)]
    		public static extern bool PostMessage(IntPtr hWnd, int Msg, Keys wParam, IntPtr lParam);
    	}
    }
    
    // Program.cs
    namespace Win32.PostMessageToApplication
    {
    	using System;
    	using System.Windows.Forms;
    
    	class Program
    	{
    		const int WM_KEYDOWN = 0x0100;
    
    		static void Main(string[] args)
    		{
    			IntPtr hWndNotepad = NativeMethods.FindWindow("Notepad", null);
    
    			IntPtr hWndEdit = NativeMethods.FindWindowEx(hWndNotepad, IntPtr.Zero, "Edit", null);
    
    			NativeMethods.PostMessage(hWndEdit, WM_KEYDOWN, Keys.A, IntPtr.Zero);
    
    			Console.ReadKey(true);
    		}
    	}
    }
    

    You need to provide the name of the class to FindWindowEx you can do that either with a call to GetClassName or statically through tools like WinCheat, Spy++.


    Eyal (http://shilony.net), Regards.

  • Monday, March 05, 2012 1:20 AM
     
     
    Well... I can't really get the GetClassName to work... it doesn't return anything. I think I might have the wrong signature. What is it? Do you have any suggestions?

    -Winston



    • Edited by Zemus Monday, March 05, 2012 1:23 AM
    •  
  • Monday, March 05, 2012 2:35 AM
     
     

    You can use the tools I posted to figure the class name or send me the program to "eyalesh at msn dot com" and I'll check it out.


    Eyal (http://shilony.net), Regards.