Click here to Skip to main content
13,290,735 members (80,001 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 19 Dec 2007

Find and Close the Window using Win API

, 19 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Find and Close the window using Win API


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;

        public static extern int FindWindow(string lpClassName,string lpWindowName);
        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);


  • 19th December, 2007: Initial post


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
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.

You may also be interested in...


Comments and Discussions

Generalplease help me! Pin
cchangkhongayngo9-Jul-12 22:07
membercchangkhongayngo9-Jul-12 22: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 window Pin
Ryan Stivenson23-Apr-11 23:49
memberRyan Stivenson23-Apr-11 23:49 
GeneralAny perspectives? ;) Pin
Geni20-Dec-07 5:12
memberGeni20-Dec-07 5:12 
GeneralПрикольная статья Pin
FerlySky200819-Dec-07 2:21
memberFerlySky200819-Dec-07 2:21 
GeneralRe: Прикольная статья Pin
Joergen Sigvardsson19-Dec-07 3:02
memberJoergen Sigvardsson19-Dec-07 3:02 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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