4 out of 5 rated this helpful - Rate this topic

GetClassName function

Applies to: desktop apps only

Retrieves the name of the class to which the specified window belongs.

Syntax

int WINAPI GetClassName(
  __in   HWND hWnd,
  __out  LPTSTR lpClassName,
  __in   int nMaxCount
);

Parameters

hWnd [in]

Type: HWND

A handle to the window and, indirectly, the class to which the window belongs.

lpClassName [out]

Type: LPTSTR

The class name string.

nMaxCount [in]

Type: int

The length of the lpClassName buffer, in characters. The buffer must be large enough to include the terminating null character; otherwise, the class name string is truncated to nMaxCount-1 characters.

Return value

Type:

Type: int

If the function succeeds, the return value is the number of characters copied to the buffer, not including the terminating null character.

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

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

GetClassNameW (Unicode) and GetClassNameA (ANSI)

See also

Reference
FindWindow
GetClassInfo
GetClassLong
GetClassWord
Conceptual
Window Classes

 

 

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
Max length for class name
According to the documentation for the WNDCLASS structure, the maximum length for the class name is 256 characters.
RealGetWindowClass versus GetClassName
As of this writing, the "See Also" section doesn't mention RealGetWindowClass at all, and there's a good chance that's actually the function that's desired.

The difference between RealGetWindowClass and GetClassName is that RealGetWindowClass retrieves the name of the base class for superclassed windows.  See http://blogs.msdn.com/b/oldnewthing/archive/2010/12/31/10110524.aspx for more information.
Visual Basic 10 Declaration
Public Declare Auto Function GetClassName Lib "User32.dll" (ByVal hwnd As IntPtr, <Out()> ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
Return value is excluding the null
The return value is the number of TCHAR copied to the specified buffer excluding the null.

i.e. If the return value is non-zero then it will be the same number that _tcslen(lpClassName) returns if called afterwards.

C# declartion and example
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd,
StringBuilder lpClassName,
int nMaxCount
);

public static string GetWindowClassName(IntPtr hWnd)

{
StringBuilder buffer = new StringBuilder(128);

GetClassName(hWnd, buffer, buffer.Capacity);

return buffer.ToString();

}