Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
GetWindowLong (user32)
.
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
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.
// 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.
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);
WindowLongFlags - Flags for GetWindowLong, GetWindowLongPtr, SetWindowLong & SetWindowLongPtr
4/2/2012 2:14:08 AM - -202.74.138.1
The GetWindowLongPtr API
2/16/2012 2:32:18 AM - -202.74.138.1
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
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).