Menu

#348 GetClassName is not type-safe

7
closed
1
2021-09-27
2016-09-06
No

Virtual function TWindow::GetClassName has return type LPCTSTR (or LPTSTR in OWL5_COMPAT mode). However, it may not return a valid string, and in this case, using the return value as a string will lead to a crash (access violation). This can lead, and has led, to bugs. For example, see [bugs:#340].

The problem is that a window class name, as defined by Windows, is a special union type. Although it masquerades as a string pointer, it can also be an atom. The Windows documentation for WNDCLASS.lpszClassName (msdn.microsoft.com) states:

"A pointer to a null-terminated string or is an atom. If this parameter is an atom, it must be a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpszClassName; the high-order word must be zero."

For more about atoms, see About Atom Tables.

Proposed solution

Create an abstraction for class names, TWindowClassName, that encapsulates the dual nature of class names, similar to the class TResId. Rename TWindow::GetClassName to GetWindowClassName, and change the return type to TWindowClassName.

Related

Bugs: #340
Discussion: Warning Destroying from TWindow::~TWindow
Feature Requests: #170
Wiki: Upgrading_from_OWL

Discussion

  • Vidar Hasfjord

    Vidar Hasfjord - 2016-09-09

    The solution on the Owlet branch has now been merged into the trunk [r3497]. An addition, a new dummy-declaration of the now obsolete virtual function TWindow::GetClassName has been added [r3498], to prevent accidental non-overriding virtual functions in derived classes. Trying to override this dummy will cause a compilation error, forcing the the user to change the code to override the new function GetWindowClassName instead.

    Edit: The implementation of TWindowClassName::GetString was missing in merge [r3497]. This has now been fixed in [r5304].

     

    Related

    Commit: [r3497]
    Commit: [r3498]
    Commit: [r5304]


    Last edit: Vidar Hasfjord 2020-12-08
  • Vidar Hasfjord

    Vidar Hasfjord - 2016-09-09
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -8,4 +8,4 @@
    
     #### Proposed solution ####
    
    -Create an abstraction for class names, TWindow::TClassName, that encapsulates the dual nature of class names, similar to the class [TResId](http://owlnext.sourceforge.net/help/html/d6/d00/classowl_1_1_t_res_id.html). Change the signature of TWindow::GetClassName to return TClassName.
    +Create an abstraction for class names, TWindowClassName, that encapsulates the dual nature of class names, similar to the class [TResId](http://owlnext.sourceforge.net/help/html/d6/d00/classowl_1_1_t_res_id.html). Rename TWindow::GetClassName to GetWindowClassName, and change the return type to TWindowClassName.
    
    • status: open --> pending
     
  • Vidar Hasfjord

    Vidar Hasfjord - 2020-05-18
    • Status: pending --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB