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

GetWindowLong (user32)
 
.
Summary
Summary

32 Bit Windows

On this platform this function is in fact a macro that calls GetWindowLong, so the EntryPoint property has to be set to the real function:

C# Signature:

[DllImport("user32.dll", EntryPoint="GetWindowLong")]
static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

VB .NET Signature:

<DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
Private Shared Function GetWindowLongPtr(ByVal hWnd As HandleRef, <MarshalAs(UnmanagedType.I4)>nIndex As WindowLongFlags) As IntPtr
[DllImport("user32.dll", SetLastError=true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);

VB.NET Signature:

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowLong(hWnd As IntPtr, _
     <MarshalAs(UnmanagedType.I4)>nIndex As WindowLongFlags) As Integer
End Function

VB Signature:

Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" _
        (ByVal prmlngWindowHandle As Long, _
         ByVal prmlngIndex As WindowLongFlags) As Long

64 Bit Windows

On this platform the actual function is called

C# Signature:

[DllImport("user32.dll", EntryPoint="GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

VB .NET Signature:

<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr")> _
Private Shared Function GetWindowLongPtr(ByVal hWnd As HandleRef, <MarshalAs(UnmanagedType.I4)>nIndex As WindowLongFlags) As IntPtr
End Function

VB Signature:

Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" _
        (ByVal prmlngWindowHandle As Long, _
         ByVal prmlngIndex As WindowLongFlags) As Long

User-Defined Types:

User Defined Structures

WindowLongFlags

To access the values referred to on MSDN declare the following enumeration.

C# enumeration:

public enum GWL
{
     GWL_WNDPROC =    (-4),
     GWL_HINSTANCE =  (-6),
     GWL_HWNDPARENT = (-8),
     GWL_STYLE =      (-16),
     GWL_EXSTYLE =    (-20),
     GWL_USERDATA =   (-21),
     GWL_ID =     (-12)
}

VB.NET Enumeration:

Public Enum GWL
     GWL_WNDPROC = -4
     GWL_HINSTANCE = -6
     GWL_HWNDPARENT = -8
     GWL_STYLE = -16
     GWL_EXSTYLE = -20
     GWL_USERDATA = -21
     GWL_ID = -12
End Enum

Notes:

None.

Constants C#:

const int GWL_HWNDPARENT = (-8);

GWL_ID = (-12)
GWL_STYLE = (-16)
GWL_EXSTYLE = (-20)

Tips & Tricks:

To make your code work on both Win32 and Win64, you can detect the size of IntPtr, and call the appropriate function. You will have to give the two functions different names, of course.

C# Sample:

[DllImport("user32.dll", EntryPoint="GetWindowLong")]
private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
// Window Styles
const UInt32 WS_OVERLAPPED = 0;
const UInt32 WS_POPUP = 0x80000000;
const UInt32 WS_CHILD = 0x40000000;
const UInt32 WS_MINIMIZE = 0x20000000;
const UInt32 WS_VISIBLE = 0x10000000;
const UInt32 WS_DISABLED = 0x8000000;
const UInt32 WS_CLIPSIBLINGS = 0x4000000;
const UInt32 WS_CLIPCHILDREN = 0x2000000;
const UInt32 WS_MAXIMIZE = 0x1000000;
const UInt32 WS_CAPTION = 0xC00000;      // WS_BORDER or WS_DLGFRAME  
const UInt32 WS_BORDER = 0x800000;
const UInt32 WS_DLGFRAME = 0x400000;
const UInt32 WS_VSCROLL = 0x200000;
const UInt32 WS_HSCROLL = 0x100000;
const UInt32 WS_SYSMENU = 0x80000;
const UInt32 WS_THICKFRAME = 0x40000;
const UInt32 WS_GROUP = 0x20000;
const UInt32 WS_TABSTOP = 0x10000;
const UInt32 WS_MINIMIZEBOX = 0x20000;
const UInt32 WS_MAXIMIZEBOX = 0x10000;
const UInt32 WS_TILED = WS_OVERLAPPED;
const UInt32 WS_ICONIC = WS_MINIMIZE;
const UInt32 WS_SIZEBOX = WS_THICKFRAME;

[DllImport("user32.dll", EntryPoint="GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
// Extended Window Styles
const UInt32 WS_EX_DLGMODALFRAME = 0x0001;
const UInt32 WS_EX_NOPARENTNOTIFY = 0x0004;
const UInt32 WS_EX_TOPMOST = 0x0008;
const UInt32 WS_EX_ACCEPTFILES = 0x0010;
const UInt32 WS_EX_TRANSPARENT = 0x0020;
const UInt32 WS_EX_MDICHILD = 0x0040;
const UInt32 WS_EX_TOOLWINDOW = 0x0080;
const UInt32 WS_EX_WINDOWEDGE = 0x0100;
const UInt32 WS_EX_CLIENTEDGE = 0x0200;
const UInt32 WS_EX_CONTEXTHELP = 0x0400;
const UInt32 WS_EX_RIGHT = 0x1000;
const UInt32 WS_EX_LEFT = 0x0000;
const UInt32 WS_EX_RTLREADING = 0x2000;
const UInt32 WS_EX_LTRREADING = 0x0000;
const UInt32 WS_EX_LEFTSCROLLBAR = 0x4000;
const UInt32 WS_EX_RIGHTSCROLLBAR = 0x0000;
const UInt32 WS_EX_CONTROLPARENT = 0x10000;
const UInt32 WS_EX_STATICEDGE = 0x20000;
const UInt32 WS_EX_APPWINDOW = 0x40000;
const UInt32 WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
const UInt32 WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
const UInt32 WS_EX_LAYERED = 0x00080000;
const UInt32 WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
const UInt32 WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
const UInt32 WS_EX_COMPOSITED = 0x02000000;
const UInt32 WS_EX_NOACTIVATE = 0x08000000;

// This static method is required because Win32 does not support
// GetWindowLongPtr directly
public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
{
     if (IntPtr.Size == 8)
     return GetWindowLongPtr64(hWnd, nIndex);
     else
     return GetWindowLongPtr32(hWnd, nIndex);
}

VB.NET Sample

<DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
Private Shared Function GetWindowLongPtr32(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
End Function

Constants VB.NET:

    ' Window Styles
    Const WS_OVERLAPPED As UInt32 = 0
    Const WS_POPUP As UInt32 = &H80000000&
    Const WS_CHILD As UInt32 = &H40000000
    Const WS_MINIMIZE As UInt32 = &H20000000
    Const WS_VISIBLE As UInt32 = &H10000000
    Const WS_DISABLED As UInt32 = &H8000000
    Const WS_CLIPSIBLINGS As UInt32 = &H4000000
    Const WS_CLIPCHILDREN As UInt32 = &H2000000
    Const WS_MAXIMIZE As UInt32 = &H1000000
    Const WS_CAPTION As UInt32 = &HC00000      ' WS_BORDER or WS_DLGFRAME  
    Const WS_BORDER As UInt32 = &H800000
    Const WS_DLGFRAME As UInt32 = &H400000
    Const WS_VSCROLL As UInt32 = &H200000
    Const WS_HSCROLL As UInt32 = &H100000
    Const WS_SYSMENU As UInt32 = &H80000
    Const WS_THICKFRAME As UInt32 = &H40000
    Const WS_GROUP As UInt32 = &H20000
    Const WS_TABSTOP As UInt32 = &H10000
    Const WS_MINIMIZEBOX As UInt32 = &H20000
    Const WS_MAXIMIZEBOX As UInt32 = &H10000
    Const WS_TILED As UInt32 = WS_OVERLAPPED
    Const WS_ICONIC As UInt32 = WS_MINIMIZE
    Const WS_SIZEBOX As UInt32 = WS_THICKFRAME

<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr")> _
Private Shared Function GetWindowLongPtr64(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
End Function
    ' Extended Window Styles
    Const WS_EX_DLGMODALFRAME As UInt32 = &H1
    Const WS_EX_NOPARENTNOTIFY As UInt32 = &H4
    Const WS_EX_TOPMOST As UInt32 = &H8
    Const WS_EX_ACCEPTFILES As UInt32 = &H10
    Const WS_EX_TRANSPARENT As UInt32 = &H20
    Const WS_EX_MDICHILD As UInt32 = &H40
    Const WS_EX_TOOLWINDOW As UInt32 = &H80
    Const WS_EX_WINDOWEDGE As UInt32 = &H100
    Const WS_EX_CLIENTEDGE As UInt32 = &H200
    Const WS_EX_CONTEXTHELP As UInt32 = &H400
    Const WS_EX_RIGHT As UInt32 = &H1000
    Const WS_EX_LEFT As UInt32 = &H0
    Const WS_EX_RTLREADING As UInt32 = &H2000
    Const WS_EX_LTRREADING As UInt32 = &H0
    Const WS_EX_LEFTSCROLLBAR As UInt32 = &H4000
    Const WS_EX_RIGHTSCROLLBAR As UInt32 = &H0
    Const WS_EX_CONTROLPARENT As UInt32 = &H10000
    Const WS_EX_STATICEDGE As UInt32 = &H20000
    Const WS_EX_APPWINDOW As UInt32 = &H40000
    Const WS_EX_OVERLAPPEDWINDOW As UInt32 = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
    Const WS_EX_PALETTEWINDOW As UInt32 = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
    Const WS_EX_LAYERED As UInt32 = &H80000
    Const WS_EX_NOINHERITLAYOUT As UInt32 = &H100000 ' Disable inheritence of mirroring by children
    Const WS_EX_LAYOUTRTL As UInt32 = &H400000 ' Right to left mirroring
    Const WS_EX_COMPOSITED As UInt32 = &H2000000
    Const WS_EX_NOACTIVATE As UInt32 = &H8000000

' This static method is required because Win32 does not support GetWindowLongPtr dirctly
Public Shared Function GetWindowLongPtr(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
     If IntPtr.Size = 8 Then
      Return GetWindowLongPtr64(hWnd, nIndex)
     Else
      Return GetWindowLongPtr32(hWnd, nIndex)
     End If
End Function

You might want to declare the first parameter as HandleRef rather than IntPtr.

Sample Code:

C# Sample:

//Returns the ID for a window given the handle for that window as returned by EnumChildWindows.
//note that for this to work you will need to change the signature from 'int nIndex' to 'GWL nIndex' in all three places
private string GetID(IntPtr winhandle)
{
     IntPtr result;
     result = GetWindowLongPtr(winhandle, GWL.GWL_ID);
     return result.ToString();
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

This function is NOT 64-bit safe when retrieving handle or pointer values. Use GetWindowLongPtr instead when retrieving an IntPtr.

Also, when compiling for a32 bit version, GetWindowsLongPtr still calls this version of the function. the actual GetWindowsLongPtr function is a 64 bit call. MSDN recommends using this method, because theres an internal macro that will choose the correct version to call.

Documentation

Tips & Tricks:

Please add some!

Sample Code:

C#:

  static public class WndInfo
  {
    [DllImport("user32.dll", SetLastError = true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    ...
    public static bool IsWindowTopMost(IntPtr Handle)
    {
      return (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
    }
    ...
  }

Alternative Managed API:

Do you know one? Please contribute it!

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