4

Version 6.0 of the common controls (comctl32.dll) implements a new approach for subclassing controls that is not available on older versions of Windows. What is the best way to implement subclassing so that it works on systems that support either version of the common controls library?

Nathan Moinvaziri
  • 5,506
  • 4
  • 29
  • 30
  • 3
    0.15% of Windows machines require code like this. Their owners are not buying software. – Hans Passant Mar 17 '12 at 05:20
  • According to [the documentation for `SetWindowSubclass`](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762102.aspx), that function is available for Comctl32.dll, version 5.8 or later. Is it wrong, or does this not actually require you to opt into v6? (I can't remember that far back.) – Cody Gray - on strike Mar 17 '12 at 05:32
  • If you are targeting Windows 95-2000, that function is not going to be available. – Nathan Moinvaziri Mar 17 '12 at 05:33
  • 1
    Ah, that's what I thought. Actually, it is available on any version of Windows that you might still reasonably support, it's just not documented or exported by name. Comctl32.dll version 4.72 (shipped with Win 98) exported them by ordinal. I don't see any harm in using them this way, since those old versions of Windows aren't going to change. – Cody Gray - on strike Mar 17 '12 at 06:14

1 Answers1

4

First, there is an article on MSDN that discusses the changes that occured in subclassing controls between version 6.0 and prior that you should be familiar with.

The best way to maintain backwards compatibility is to create wrapper functions for subclassing controls. This will require you to dynamically load the functions that are required for subclassing controls on version 6 of comctl32.dll. Here is a rough example of how it can be done.

typedef BOOL (WINAPI *LPFN_SETWINDOWSUBCLASS)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
typedef LRESULT (WINAPI *LPFN_DEFSUBCLASSPROC)(HWND, UINT, WPARAM, LPARAM);
typedef BOOL (WINAPI *LPFN_REMOVEWINDOWSUBCLASS)(HWND, SUBCLASSPROC, UINT_PTR);
typedef BOOL (WINAPI *LPFN_INITCOMMONCONTROLSEX)(LPINITCOMMONCONTROLSEX);

typedef struct SubclassStruct {
    WNDPROC Proc;
} SubclassStruct;

LPFN_SETWINDOWSUBCLASS      SetWindowSubclassPtr = NULL;
LPFN_REMOVEWINDOWSUBCLASS   RemoveWindowSubclassPtr = NULL;
LPFN_DEFSUBCLASSPROC        DefSubclassProcPtr = NULL;
LPFN_INITCOMMONCONTROLSEX   InitCommonControlsExPtr = NULL;

HMODULE ComCtlModule = NULL;

int Subclasser_Init(void)
{
    INITCOMMONCONTROLSEX CommonCtrlEx = {0};


    ComCtlModule = LoadLibrary("comctl32.dll");
    if (ComCtlModule == NULL) 
        return FALSE;

    SetWindowSubclassPtr = (LPFN_SETWINDOWSUBCLASS)GetProcAddress(ComCtlModule, "SetWindowSubclass");
    RemoveWindowSubclassPtr = (LPFN_REMOVEWINDOWSUBCLASS)GetProcAddress(ComCtlModule, "RemoveWindowSubclass");
    DefSubclassProcPtr = (LPFN_DEFSUBCLASSPROC)GetProcAddress(ComCtlModule, "DefSubclassProc");
    InitCommonControlsExPtr = (LPFN_INITCOMMONCONTROLSEX)GetProcAddress(ComCtlModule, "InitCommonControlsEx");

    if (InitCommonControlsExPtr != NULL)
    {
        CommonCtrlEx.dwSize = sizeof(CommonCtrlEx);
        InitCommonControlsExPtr(&CommonCtrlEx);
    }

    return TRUE;
}

int Subclasser_Uninit(void)
{
    if (ComCtlModule != NULL)
        FreeLibrary(ComCtlModule);
    return TRUE;
}

LRESULT CALLBACK Subclasser_SharedSubclassProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam, UINT_PTR SubclassId, DWORD_PTR RefData)
{
    SubclassStruct *Subclass = (SubclassStruct *)SubclassId;
    return CallWindowProc(Subclass->Proc, hWnd, Message, wParam, lParam);
}

int Subclasser_SetProc(HWND hWnd, WNDPROC Proc, WNDPROC *OriginalProc, void *Param)
{
    SubclassStruct *Subclass = NULL;
    int Result = TRUE;



    SetLastError(0);
    if (SetWindowLongPtr(hWnd, GWLP_USERDATA, (__int3264)(UINT_PTR)Param) == 0)
    {
        if (GetLastError() > 0)
            return FALSE;
    }

    if (SetWindowSubclassPtr!= NULL) 
    {
        Subclass = (SubclassStruct*)malloc(sizeof(SubclassStruct));
        Subclass->Proc = Proc;
        *OriginalProc = (WNDPROC)Subclass;
        Result = SetWindowSubclassPtr(hWnd, Subclasser_SharedSubclassProc, (UINT_PTR)Subclass, NULL);
    }
    else
    {
        *OriginalProc = (WNDPROC)(void *)GetWindowLongPtr(hWnd, GWLP_WNDPROC);

        SetLastError(0);
        if (SetWindowLongPtr(hWnd, GWLP_WNDPROC, (__int3264)(intptr)Proc) == 0)
        {
            if (GetLastError() > 0)
                Result = FALSE;
        }
    }

    if (Result == FALSE)
        return FALSE;

    return TRUE;
}

int Subclasser_UnsetProc(HWND hWnd, WNDPROC Proc, WNDPROC *OriginalProc)
{
    SubclassStruct *Subclass = NULL;
    int Result = TRUE;


    if (RemoveWindowSubclassPtr != NULL)
    {
        if (*OriginalProc != NULL)
        {
            Subclass = (SubclassStruct *)*OriginalProc;
            Proc = Subclass->Proc;
        }

        Result = RemoveWindowSubclassPtr(hWnd, Subclasser_SharedSubclassProc, (UINT_PTR)Subclass);
        free(Subclass);
    }
    else
    {
        SetLastError(0);
        if (SetWindowLongPtr(hWnd, GWLP_WNDPROC, (__int3264)(UINT_PTR)*OriginalProc) == 0)
        {
            if (GetLastError() > 0)
                Result = FALSE;
        }
    }

    SetLastError(0);
    if (SetWindowLongPtr(hWnd, GWLP_USERDATA, 0) == 0)
    {
        if (GetLastError() > 0)
            Result = FALSE;
    }

    *OriginalProc = NULL;

    if (Result == FALSE)
        return FALSE;

    return TRUE;
}

LRESULT Subclasser_DefProc(WNDPROC OriginalProc, HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    if (OriginalProc == NULL)
        return DefWindowProc(hWnd, Message, wParam, lParam);
    if (DefSubclassProcPtr != NULL)
        return DefSubclassProcPtr(hWnd, Message, wParam, lParam);
    return CallWindowProc(OriginalProc, hWnd, Message, wParam, lParam);
}

The only other example can be found in OpenJDK. The one disadvantage to it is that it uses the the WindowProc as the subclass ID which crashes if you are subclassing more than one control on a dialog with the same WindowProc function. In the example above, we allocate a new memory structure called SubclassStruct and pass it's address as the subclass ID which guarantees that each instance of the control you subclass will have a unique subclass ID.

If you are using the subclassing functions in multiple applications, some that use comctl32.dll < 6 and some that use comctl32.dll >= 6, you could detect which version of the common control library was loaded by getting comctl32.dll's file version information. This can be done through the use of GetModuleFileName and GetFileVersionInfo.

In addition, if you use SetWindowWord/GetWindowWord in the subclass callbacks with comctl32.dll 6.0, such as in the following Dr. Dobbs article on Writing Windows Custom Controls, then you will need to use those code blocks conditionally when comctl32.dll < 6, because they will not work on version 6 or greater and will cause your application to crash.

Nathan Moinvaziri
  • 5,506
  • 4
  • 29
  • 30