Security subclass in Win32 (1)
Subclass
SetwindowlongA function is used to subclass an instance of a window. ApplicationProgramYou must know the address of the subclass function. The subclass function is used to receiveWindowsSend the message to the original window. Subclass functions must be in the application orDLLIn the module definition file.
When the application subclass window, use the handle of the window to be quilt, Gwl_wndproc Flag ( In Windows. h Definition ) And the new subclass function address as the parameter to call the Function Setwindowlong . Function Setwindowlong Return One DWORD Type value, which is the original window process address of the window. The application should save the address to pass the intercepted messages to the original window process, and will be used to remove sub-classes of windows in the future. The application uses the address of the original Window Process and Windows The Hwnd ,Message,Wparam And Lparam Parameter call Function Callwindowproc Send messages to the original window process. Generally, an application simply extracts it from Windows The received data is passed to the function. Callwindowproc .
The application also needs the original window process address to remove the window subclass. The application calls the function again.SetwindowlongTo remove the subclass of the window. The application transmits the original window process address to the function,Gwl_wndprocThe flag and the handle of the quilt window.
The followingCodeThis example shows how to subclass an edit box control and remove it from the subclass:
Long Far Pascal subclassfunc (hwnd, uint message, wparam, long lparam );
Farproc lpfnoldwndproc;
Hwnd heditwnd;
//
// Create an edit control and subclass it.
// The details of this particle edit control are not important.
//
Heditwnd = Createwindow ( " Edit " , " Edit Test " ,
Ws_child | Ws_visible | Ws_border,
0 , 0 , 50 , 50 ,
Hwndmain, null, hinst, null );
//
//Now subclass the window that was just created.
//
Lpfnoldwndproc=(Farproc) setwindowlong (heditwnd, gwl_wndproc, (DWORD) subclassfunc );
.
.
.
//
//Remove the subclass for the Edit Control.
//
Setwindowlong (heditwnd, gwl_wndproc, (DWORD) lpfnoldwndproc );
//
// Here is a sample subclass function.
//
Long Far Pascal subclassfunc (hwnd,
Uint message, wparam, long lparam)
{
//
// When the focus is in an edit control inside a dialog box,
// Default enter key action will not occur.
//
If (Message = Wm_getdlgcode)
Return Dlgc_wantallkeys;
ReturnCallwindowproc (lpfnoldwndproc, hwnd, message, wparam, lparam );
}
Potential Defects
As long as you pay attention to the following protection rules, the instance subclass is usually safe.
When subclass a window, you must understand who is responsible for the window's behavior, for example,WindowsIt is responsible for the behavior of all the controls provided by it, while the application is responsible for all the windows it defines. Subclass can be applied to any window in the same process. However, when an application subclass a window that does not belong to itself, the application must ensure that the subclass function does not destroy the original behavior of the window. Because the application does not control that window, it should not rely on any window information that may be changed by the window owner in the future. UnlessExactKnows the meaning of the additional bytes in the window or window class and how to use them in the original window process. Otherwise, a subclass function should not use them. To put it back, even if the application understands everything about the additional bytes of the window or the additional bytes of the window class, do not use them unless the application is responsible for this window. If an application uses an additional window byte of the window under the responsibility of another component, the subclass process may fail when the component decides to update the window and change the structure of the additional byte. For this reason,MicrosoftWe recommend that you do not subclass the control class becauseWindowsIs responsible for the controls it provides, and the next versionWindowsMay change the appearance of these controls. If your application must subclassWindowsProvided controls in the new versionWindowsAfter the release, the Code may also need to be updated.
Because the instance subclass occurs after a window is created, the application subclass window cannot add any additional bytes to the window, the application may need to save all the data required for the quilt-type window instance in the window property list.
Setprop Function You can append attributes to a window. The application uses the window handle, a string indicating the property, and a property data handle as the parameter call. Setprop Function. The data handle is usually Localalloc Or GlobalallocFunction. When the application needs to use the data in the window property list, it can use the window handle and the string indicating the property for parameter calls.GetpropFunction, itReturnSetpropFunctionThe data handle. When the application no longer needs the data or the window is to be destroyed, the application must use the window handle and the string indicating the attribute for parameter calling.RemovepropFunctionRemove the property data from the property list. The handle of the data returned by the function, and then the application uses the handle to call the function.LocalfreeOrFunctionGlobalfree.About FunctionsSetprop,GetpropAndRemovepropFor more information, see PlatformSDKDocumentation.
When an application subclasses a window that has been quilt-encapsulated, all subclasses are removed in reverse order when they are set.
Security subclass in Win32 (4)