The correct way of getting the parent window

Here's what I came up with:

//
// Returns the real parent window
// Same as GetParent(), but doesn't return the owner
//
HWND GetRealParent(HWND hWnd)
{
    HWND hParent;

    hParent = GetAncestor(hWnd, GA_PARENT);
    if(!hParent || hParent == GetDesktopWindow())
        return NULL;

    return hParent;
}

Updated in the year 2020 considering the latest Win32 documentation:

HWND GetRealParent(HWND hWnd)
{
    HWND hWndOwner;

    // To obtain a window's owner window, instead of using GetParent,
    // use GetWindow with the GW_OWNER flag.

    if (NULL != (hWndOwner = GetWindow(hWnd, GW_OWNER)))
        return hWndOwner;

    // Obtain the parent window and not the owner
    return GetAncestor(hWnd, GA_PARENT);
}

A slightly better version that goes both "routes" and if it can't find a proper parent it will return the window itself(as to avoid null references). Using GetParent instead of GetAncestor worked in my case and returned the window I was after.

    public static IntPtr GetRealParent(IntPtr hWnd)
    {
        IntPtr hParent;

        hParent = GetAncestor(hWnd, GetAncestorFlags.GetParent);
        if (hParent.ToInt64() == 0 || hParent == GetDesktopWindow())
        { 
            hParent = GetParent(hWnd);
            if (hParent.ToInt64() == 0 || hParent == GetDesktopWindow())
            { 
                hParent = hWnd;
            }

        }

        return hParent;
    }

Tags:

Winapi