Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

How to close another process's main window from a C# App without invoking Native Win32 code

Back in the good (or bad :) ) days of Win32 programming, there was a nice little function named FindWindow. It enabled you to find any window belonging to any process on your system

These days are gone with C# .NET, well... not quite!

If you have been looking for an alternative to FindWindow, Read along. There are two alternatives, the first involves invoking FindWindow directly from the Native Win32 DLL API, but that takes us back to the old days.

The second (better) alternative is to use a combination of Process.GetProcessesByName and Process.MainWindowHandle.

Here is an example:

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

foreach (Process p in processes)

{

    IntPtr pFoundWindow = p.MainWindowHandle;

    // Do something with the handle...

    //

}

Closing Another Process Main Window

Not only can you find the main window of another process, you can even close it! So let's say you want to close Notepad main window from your C# application, the following code will do the trick:

using System.Diagnostics;

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

foreach (Process p in processes)

{

    p.CloseMainWindow();

}

And boom... poor Notepad is gone from the screen! Please use the above code responsively :-)

kick it on DotNetKicks.com

Feedback

Posted on 8/8/2010 4:11:54 AM

Is there a way to get the child windows?
And what should I do if I don't know the name of the process?

Posted on 8/6/2010 12:00:10 AM

I so enjoy your writing, thank you for posting an interesting article and good.

Posted on 5/19/2010 7:27:15 PM

Isn't using the native FindWindow a bad idea, because it can hang if there's an unresponsive window on the system? (due to its internal use of sendmessage rather than sendmessagetimeout)

Posted on 11/1/2009 10:06:50 AM

private void Window_Loaded(object sender, RoutedEventArgs e)
{
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.cbData = 0;
IntPtr ptrData = Marshal.AllocCoTaskMem(cds.cbData);
cds.dwData = IntPtr.Zero;
cds.lpData = ptrData;

HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
//HwndSource dest = HwndSource.FromHwnd()

source.AddHook(new HwndSourceHook(WndProc));
//SendMessage(source.Handle, WM_DESTROY, 0, ref cds);
IntPtr dest_hwd = (IntPtr)FindWindow("WMPlayerApp", "Windows Media Player");

if (dest_hwd != IntPtr.Zero)
{
MessageBox.Show("Found WMP");
SendMessage((int)dest_hwd, WM_COMMAND, 0x00004978, 0x00000000);
}

}

Posted on 3/25/2009 9:07:47 AM

It's worth noting that the MainWindowHandle property will only be populated with a window handle if the window is visible (it'll be zero otherwise), so if you want to find an invisible window it's back to the good old FindWindow API!

Posted on 2/4/2009 12:48:07 AM

Hi, I have to terminate an application running in command line say an exe invoked in a backgroundwoker do work method.

Now It is running on console in background. I have to terminate this. i have one option sending keystroke Ctrl + C to this but this is not working properly. Help me.

Posted on 1/2/2009 10:52:07 AM

How can i send keys to a specific window with SendMessage API ?

Posted on 12/24/2008 11:35:49 AM

i am using webroser control and its when i invokingmemeber ("click") i get new window , i want print that window , how do i do that

Posted on 11/14/2008 6:26:57 AM

Thanks it works fine

Posted on 7/25/2008 9:22:16 AM

I think you worry too much on this subject but you posted a good question there !

Posted on 8/30/2007 4:08:19 AM

I am trying about finding co - ordinates
Which property can help me??
Thank,

Posted on 5/31/2007 4:29:55 PM

This is all good but how do you get the child windows?

Posted on 4/9/2007 9:13:42 AM

Hi Sagar,

Process.GetProcessesByName finds processes on your system by their friendly name, not by the name of their main window. Shell_TrayWnd belongs to the "explorer.exe" process, whos friendly name is explorer, so simply, change your code to find explorer as follows:

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

and that should get you back the window handle you are looking for.

Just curious... why Shell_TrayWnd? hope you are not writing any new viruses! ;-)

Posted on 4/9/2007 8:49:39 AM

I am trying the below code. It does not returna handler. Am I doing any thing wrong?

Process[] processes = Process.GetProcessesByName("Shell_TrayWnd");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;

// Show Win Start Button Menu ...
Win32.SendMessage((int)pFoundWindow, Win32.WM_COMMAND, 305 , null);
}

Posted on 4/8/2007 5:13:30 AM

very elegant! =)

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author