Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


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

GetClassName (user32)
 
.
Summary

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

Notes:

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);
    }
    else
    {
        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)
   MessageBox.Show(sClassName.ToString)
End Sub

Alternative Managed API:

The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a

class ManagedWinapi.SystemWindow that has a ClassName property.

Documentation

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 PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions