48 out of 77 rated this helpful - Rate this topic

FindWindow function

Applies to: desktop apps only

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.

To search child windows, beginning with a specified child window, use the FindWindowEx function.

Syntax

HWND WINAPI FindWindow(
  __in_opt  LPCTSTR lpClassName,
  __in_opt  LPCTSTR lpWindowName
);

Parameters

lpClassName [in, optional]

Type: LPCTSTR

The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.

If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.

If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

lpWindowName [in, optional]

Type: LPCTSTR

The window name (the window's title). If this parameter is NULL, all window names match.

Return value

Type:

Type: HWND

If the function succeeds, the return value is a handle to the window that has the specified class name and window name.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

If the lpWindowName parameter is not NULL, FindWindow calls the GetWindowText function to retrieve the window name for comparison. For a description of a potential problem that can arise, see the Remarks for GetWindowText.

Examples

For an example, see Retrieving the Number of Mouse Wheel Scroll Lines.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

FindWindowW (Unicode) and FindWindowA (ANSI)

See also

Reference
EnumWindows
FindWindowEx
GetClassName
GetWindowText
RegisterClass
RegisterClassEx
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Return value
Note that a return value of NULL with a GetLastError() of ERROR_SUCCESS indicates that there are no top-level windows matching the search criteria.
Sample Code(C#)

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String sAppName);

Example:

IntPtr hwnd=FindWindow(null,"Form1");

"Form1" is the Text property of the window.

Along with "EnableWindow(..)" , this function can be used to lock a perticular window.

Example:

[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr hwnd, bool bEnable);


public void SampleFunction()
{
IntPtr hwnd=FindWindow(null,"Form1");
EnableWindow(hwnd,false);//to unlock set the boolean value to true
}
How to use this API in VS2010 C++/CLR?
When I use this API, VS2010 will link Error as follow message: $0unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const *,wchar_t const *)" (?FindWindowW@@$$J18YGPAUHWND__@@PB_W0@Z) referenced in function "private: void __clrcall Dump_GUI::Form1::Execute_Dump_Tool(void)" (?Execute_Dump_Tool@Form1@Dump_GUI@@$$FA$AAMXXZ)$0 $0fatal error LNK1120: 2 unresolved externals$0 $0$0 $0 $0I must use "Unicode Set..", so cant change the character set property....$0 $0How to solve!?$0