Click here to Skip to main content
Click here to Skip to main content

Find and Close the Window using Win API

By , 19 Dec 2007
 

Introduction

This article explains how to find and close the window using Win API .

Find and Close the Window

Find the Window

The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

FindWindow(string lpClassName,string lpWindowName) 

Finding ClassName and WindowName using Spy++

Spy++ (SPYXX.EXE) is a Win32-based utility that gives you a graphical view of the system's processes, threads, windows, and window messages. With the Window Finder Tool, you can find the properties of a selected window.

Step 1: Arrange your Windows so that Spy++ and the subject window are visible.

Step 2: From the Spy menu, choose Find Window to open the Find Window dialog box.

Step 3: Drag the Finder Tool to the desired window. As you drag the tool, window details display in the dialog box. (Handle, Caption(Window Name), Class Name)

using Microsoft.Win32;

[DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName,string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
            
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_CLOSE = 0xF060;

private void closeWindow()
        {
            // retrieve the handler of the window  
            int iHandle = FindWindow("Notepad", "Untitled - Notepad");
            if (iHandle > 0)
            {
                // close the window using API        
                SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
            }  
        }	

History

  • 19th December, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohan Kumar
Software Developer Cognizant Technology Solution
India India
Member
I’m Mohan Kumar from Chennai and hold a Masters Degree in Information Technology. I have worked with Opcion Technologies and Atlas systems in Various Technologies.Currently i'm associated with congnizant Technology Solutions.I am a very good fan of Microsoft Technologies. I love Programming, and I am eager to learn New things Curious about Microsoft Products, Interested in Knowledge sharing.I am glad that I have received couple of awards from community Credit and Dotnetspider for submitting .Net related articles and answers. My famous slogan "Awake! Arise! Stop not till the goal is reached.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Layout  Per page   
Generalplease help me!membercchangkhongayngo9 Jul '12 - 21:07 
I tried your way but I am having a problem and I do not know why edit out all! please help me!
 
"because the method 'FindWindow' has no implementation (no RVA)"
 
demo app can you make?
 
thanks alot Wink | ;)
GeneralFinding a windowmemberRyan Stivenson23 Apr '11 - 22:49 
Hello,
 
I'm trying to find a window by using FindWindow() Win Api.
I'm using c# V.S 10 .net 4.0. on Windows 7.
When I type:
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);
 
I'm getting this error:
Error 1 Expected class, delegate, enum, interface, or struct
Error 2 The modifier 'extern' is not valid for this item
 
Any Idea ?
 
Thanks
Ryan
GeneralAny perspectives? ;)memberGeni20 Dec '07 - 4:12 
This material is useful mostly for novice, but novices not using CP.com for studying, they're using sites like firststeps.ru
 
Also such information with a lot of samples places here: http://pinvoke.net/
 
????? ??????, ????.
Nothing to say. But you can send me any questions to ICQ: 94053010

GeneralПрикольная статьяmemberFerlySky200819 Dec '07 - 1:21 
Prikolnay statia. Ob etom znaet luboi student Big Grin | :-D
GeneralRe: Прикольная статьяmemberJoergen Sigvardsson19 Dec '07 - 2:02 
Pravda! Dostojevskij!!! Da! Da! Da! Da!!!!
 

Unsure | :~
--
Kein Mitleid Für Die Mehrheit

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by Mohan Kumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid