Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

looping through IntPtr pointer?

calculator loop

  • Please log in to reply
3 replies to this topic

#1 Melissa


    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 20 May 2010 - 08:11 AM

I'm trying to loop through each window of a certain process. Since the process has many child windows which do not appear in the processes list I'm using this bit of of code I found online which finds the windows based on the window class name...

// Get a handle to an application window.
[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);

private void button1_Click(object sender, EventArgs e)
    // Get a handle to the Calculator application. The window class
    // and window name were obtained using the Spy++ tool.
    IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");

    // Verify that Calculator is a running process.
    if (calculatorHandle == IntPtr.Zero)
        MessageBox.Show("Calculator is not running.");

My question is now, how can I loop though each one of these "calculatorHandle"'s? Assuming I had more than one open. Something like....foreach(calculatorHandle in ???)

Thanks in advance!
  • 0

#2 Melissa


    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 20 May 2010 - 03:04 PM

I recevied some advice suggesting I will need to get a Collection of all windows and search through those in my foreach loop like...

foreach(calculatorHandle in WINDOWSCOLLECTION)

Could someone explain how to get that collection :)
  • 0

#3 Melissa


    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 20 May 2010 - 08:45 PM

I guess I could do it complety different, and easier assuming there is some way to get all applications that are running from the taskbar. Not the processes. Anything to do with processes like below, will just show me the main window, not ALL applications running. Basically I want a list of programs similar to when you hit Alt+Tab. Thanks.

Process[] processes = Process.GetProcessesByName("notepad");

foreach (Process p in processes)


    IntPtr pFoundWindow = p.MainWindowHandle;

    // Do something with the handle...



  • 0

#4 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 22 April 2012 - 07:58 PM

With "Process.GetProcesses()" way, you will get all the processes currently running on your system. And the main-window handle (MainWindowHandle property of Process class) for each process. But if you want to get all the child-window (button, textbox, label etc) handle of each main-widnow, you can use the EnumChildWindow Win32 API. As example, if you want to find all the child-window, of the 'Calculator' process, you can use the following code...
// First declare a delegate of signature "EnumWindowsProc"
delegate int EnumWindowsProcDelegate(IntPtr hWnd, int lParam);

//Declare (through DllImport) signature for "EnumChildWindows" Win32 API
public static extern int EnumChildWindows(IntPtr hParent, EnumWindowsProcDelegate lpEnumFunc, int lParam);

// Now let us use EnumChildWindows method to enumerate all the children of the "Calculator" process.
void UsageOfEnumChildWindows()
	  // You can use "EnumWindows" instead of Process.GetProcessByName or FindWindow API to find the handle of Calculator.
	  // EnumWindows enumerate through all top level windows. Perhaps .NET
	  // internally uses EnumWindows??? or WMI (Windows Management Instrumentation) ???
	  Process[] calcs = Process.GetProcessesByName("Calculator");

	  WindowsAPI.EnumWindowsProcDelegate lpdelegeate = new WindowsAPI.EnumWindowsProcDelegate(EnumChildProc);
	  WindowsAPI.EnumChildWindows(calcs[0].MainWindowHandle, lpdelegeate, 0);

// This is the method whether you will get each child-window handle.
public static int EnumChildProc(IntPtr hChildWnd, int lParam)
		//  hChildWnd is the handle to the child window of 'Calculator'
		// you need to use hChildWnd here to meet your desire

		// return 1 to continue enumerating through child windows; 0 to stop enumerating.
		return 1;

  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download