Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
GetClassName (user32)

C# Signature:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,int nMaxCount);

VB.NET Signature:

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function GetClassName(ByVal hWnd As System.IntPtr, _
   ByVal lpClassName As System.Text.StringBuilder, _
   ByVal nMaxCount As Integer) As Integer
    ' Leave function empty    
End Function

Alternate VB.NET Signature:

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

VB Signature

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
         (ByVal hWnd As Long, _
          ByVal lpClassName As String, _
          ByVal nMaxCount As Long) As Long


In .NET Framework 4.0, only CharSet.Ansi seems to produce the result.

The return type int seems to crash the .NET application in 64-bit mode (occasionally), long seems to work better. Maybe it should even be IntPtr? Please modify this note if you have more insights.

Tips & Tricks:

Please add some!

Sample Code:

C# Sample Code

private static bool isIEServerWindow(IntPtr hWnd)
    int nRet;
    // Pre-allocate 256 characters, since this is the maximum class name length.
    StringBuilder ClassName = new StringBuilder(256);
    //Get the window class name
    nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
    if(nRet != 0)
        return (string.Compare(ClassName.ToString(), "Internet Explorer_Server",true,CultureInfo.InvariantCulture) == 0);
        return false;

VB.NET Sample Code

Create a new VB .NET form and add a button Button1 to it.

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
   Dim sClassName As New StringBuilder("", 256)
   'pass in the handle of the object for which to get
   'the class name; for example, the form's handle
   Call GetClassName(Me.Handle, sClassName, 256)
End Sub

Alternative Managed API:

The ManagedWindowsApi project ( provides a

class ManagedWinapi.SystemWindow that has a ClassName property.


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version