SetWindowLong Function

The SetWindowLong function changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.

Note  This function has been superseded by the SetWindowLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Microsoft Windows, use the SetWindowLongPtr function.

Syntax

LONG SetWindowLong(      
    HWND hWnd,     int nIndex,     LONG dwNewLong );

Parameters

hWnd
[in] 

Handle to the window and, indirectly, the class to which the window belongs.

Windows 95/98/Me: The SetWindowLong function may fail if the window specified by the hWnd parameter does not belong to the same process as the calling thread.

nIndex
[in] Specifies the zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of an integer. To set any other value, specify one of the following values.
GWL_EXSTYLE
Sets a new extended window style. For more information, see CreateWindowEx.
GWL_STYLE
Sets a new window style.
GWL_WNDPROC

Sets a new address for the window procedure.

Windows NT/2000/XP: You cannot change this attribute if the window does not belong to the same process as the calling thread.

GWL_HINSTANCE
Sets a new application instance handle.
GWL_ID
Sets a new identifier of the window.
GWL_USERDATA
Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
The following values are also available when the hWnd parameter identifies a dialog box.
DWL_DLGPROC
Sets the new address of the dialog box procedure.
DWL_MSGRESULT
Sets the return value of a message processed in the dialog box procedure.
DWL_USER
Sets new extra information that is private to the application, such as handles or pointers.
dwNewLong
[in] Specifies the replacement value.

Return Value

If the function succeeds, the return value is the previous value of the specified 32-bit integer.

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

If the previous value of the specified 32-bit integer is zero, and the function succeeds, the return value is zero, but the function does not clear the last error information. This makes it difficult to determine success or failure. To deal with this, you should clear the last error information by calling SetLastError(0) before calling SetWindowLong. Then, function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.

Remarks

Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.

If you use SetWindowLong with the GWL_WNDPROC index to replace the window procedure, the window procedure must conform to the guidelines specified in the description of the WindowProc callback function.

If you use SetWindowLong with the DWL_MSGRESULT index to set the return value for a message processed by a dialog procedure, you should return TRUE directly afterward. Otherwise, if you call any function that results in your dialog procedure receiving a window message, the nested window message could overwrite the return value you set using DWL_MSGRESULT.

Calling SetWindowLong with the GWL_WNDPROC index creates a subclass of the window class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process. The SetWindowLong function creates the window subclass by changing the window procedure associated with a particular window class, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.

Reserve extra window memory by specifying a nonzero value in the cbWndExtra member of the WNDCLASSEX structure used with the RegisterClassEx function.

You must not call SetWindowLong with the GWL_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function.

If the window has a class style of CS_CLASSDC or CS_OWNDC, do not set the extended window styles WS_EX_COMPOSITED or WS_EX_LAYERED.

Windows XP/Vista: Calling SetWindowLong to set the style on a progressbar will reset its position.

Windows 95/98/Me: SetWindowLongW is supported by the Microsoft Layer for Unicode (MSLU). SetWindowLongA is also supported to provide more consistent behavior across all Windows operating systems. To use these versions, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Subclassing a Window.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

See Also

Tags :


Community Content

Đonny
Possible VB9 declaration
Friend Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hwnd As Int32, ByVal nIndex As WindowLongs, ByVal dwNewLong As Int32) As Int32

        Friend Enum WindowLongs As Int32
GWL_EXSTYLE = -20
GWL_STYLE = -16
GWL_WNDPROC = -4
GWL_HINSTANCE = -6
GWL_HWNDPARENT = -8
GWL_ID = -12
GWL_USERDATA = -21
DWL_DLGPROC = 4
DWL_MSGRESULT = 0
DWL_USER = 8
End Enum

In order to change window procedure (wnd proc) you should use another declaration:

Friend Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hwnd As Int32, ByVal nIndex As WindowProcs, ByVal NewProc As Messages.WndProc) As Boolean
Public Delegate Function WndProc(ByVal hWnd As Integer, ByVal msg As Messages.WindowMessages, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Friend Enum WindowProcs As Int32
GWL_WNDPROC = WindowLongs.GWL_WNDPROC
DWL_DLGPROC = WindowLongs.DWL_DLGPROC
End Enum

Messages.WindowMessages represents enumeration of all known window messages and is of type Int32.

If you are passing delegate to unmanaged code, you must ensure that the delegate instance is kept alive as long as it is used by unmanaged code. It the delegeta is garbage collected and then called from unmanaged code uncatchable error occurs.


Jack Tripper
SetWindowLong will trigger a WM_STYLECHANGE message.

The WM_STYLECHANGED message is sent to a window after the SetWindowLong function has changed one or more of the window's styles. A window receives this message through its WindowProc function.

Since the notification is sent (i.e. using SendMessage) there is a potential for a block or deadlock.

http://blogs.msdn.com/oldnewthing/archive/2007/12/28/6882760.aspx

Tags : sss

pulp
Function will block on windows which do not respond

Using GWL_EXSTYLE or GWL_STYLE on a window which do not respond (is hang up and not yet in 'Ghost-mode') for example through a lengthy task in it's window proc this function will block until the window is responding again. To avoid this I call the function in a separate thread like this with a timeout in ms:


struct _data{HWND hWnd; int nIndex; LONG dwNewLong;};

static DWORD WINAPI SetWindowLongPtrTimeoutThread(LPVOID lpParam)
{
_data *data = (_data *) lpParam;
HWND hWnd=data->hWnd;
int nIndex=data->nIndex;
LONG dwNewLong=data->dwNewLong;
delete data;

SetWindowLongPtr(hWnd, nIndex, dwNewLong);
return 0;
}

bool SetWindowLongPtrTimeout(HWND window, int nIndex, LONG dwNewLong, int timeout)
{
_data *data=(_data*) new _data;
data->hWnd=window;
data->nIndex=nIndex;
data->dwNewLong=dwNewLong;

HANDLE threadHandle;
unsigned long threadId;
threadHandle = CreateThread(0, 0, SetWindowLongPtrTimeoutThread, data,
0, &threadId);
if (WaitForSingleObject(threadHandle, timeout) == WAIT_TIMEOUT)
{
return false;
}
return true;
}

Timeout of 200 ms is a good value. Terminating the thread after WAIT_TIMEOUT with TerminateThread() is not a good because TerminateThread() is leaking memory. The thread will end if the window is responding again or if the window is dead.


Tags :

TimJohns
DWL_DLGPROC and GWL_WNDPROC Return Internal Handles
Note that the value returned when using DWL_DLGPROC and GWL_WNDPROC shouldn't be expected to be the actual address of the WndProc, but rather an internal handle. See GetWindowLong[Ptr] and CallWindowProc for more details.
Tags :

Page view tracker