DwmGetWindowAttribute returns 0 with PInvoke
Asked Answered
M

1

6

I'm trying to do screen captures by capturing a specific window and in order to accurately figure out the size of the window to capture I want to use DwmGetWindowAttribute(). When I call this function with PInvoke on Windows 10 the Rect structure is always empty even though the result value is 0 (success). The Window handle passed in is valid as well because there is fallback code that calls GetWindowRect() which works (albeit with border problems).

I'm a bit at a loss. I used this same code a while back (perhaps on Windows 8.1?) and the same code seemed to be working but now no matter what I do the call to the function always returns an empty structure.

Here's the relevant code.

Definitions:

    [DllImport("dwmapi.dll")]
    static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);


    [Flags]
    public enum DwmWindowAttribute : uint
    {
        DWMWA_NCRENDERING_ENABLED = 1,
        DWMWA_NCRENDERING_POLICY,
        DWMWA_TRANSITIONS_FORCEDISABLED,
        DWMWA_ALLOW_NCPAINT,
        DWMWA_CAPTION_BUTTON_BOUNDS,
        DWMWA_NONCLIENT_RTL_LAYOUT,
        DWMWA_FORCE_ICONIC_REPRESENTATION,
        DWMWA_FLIP3D_POLICY,
        DWMWA_EXTENDED_FRAME_BOUNDS,
        DWMWA_HAS_ICONIC_BITMAP,
        DWMWA_DISALLOW_PEEK,
        DWMWA_EXCLUDED_FROM_PEEK,
        DWMWA_CLOAK,
        DWMWA_CLOAKED,
        DWMWA_FREEZE_REPRESENTATION,
        DWMWA_LAST
    }

    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;

        public Rectangle ToRectangle()
        {
            return Rectangle.FromLTRB(Left, Top, Right, Bottom);
        }
    }

Code to do the capture:

    public static Rectangle GetWindowRectangle(IntPtr handle)
    {
        Rectangle rected = Rectangle.Empty;

        Rect rect = new Rect();
        if (Environment.OSVersion.Version.Major < 6)
        {
            GetWindowRect(handle, out rect);
            rected = rect.ToRectangle();
        }      
        else
        {

            int size = Marshal.SizeOf(typeof(Rect));
            int res = DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, size);

            Debug.WriteLine(res.ToString("x") + " " + size + " " + handle + " " + (int) DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS);

            // allow returning of desktop and aero windows
            if (rected.Width == 0)
            {
                GetWindowRect(handle, out rect);
                rected = rect.ToRectangle();
                Debug.WriteLine("Using GetWindowRect");
            }
        }

        Debug.WriteLine(rected.ToString());
        return rected;
    }

It feels like something simple is missing here. Any ideas?

Marilynnmarimba answered 31/7, 2016 at 15:17 Comment(4)
It works fine, you have a simple bug in your code. You must call rect.ToRectangle() before testing the rected.Width property. Do favor taking advantage of C#'s definite assignment tracking feature, initializing the rected variable was a mistake.Zavala
Duh. Yes, you're right. Want to make that an answer? Else I can remove it as 'Stupid user error" :-)Marilynnmarimba
I already voted to close as "typo". This doesn't help anybody else.Zavala
It did help me. I got a working sample of something i needed :)Wag
J
0

Use GetWindowRect instead of DwmGetWindowAttribute to receive RECT of the window.

[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
Jutland answered 8/6, 2018 at 12:41 Comment(1)
rect is not empty but with the wrong (offset) coordinates..Afterburning

© 2022 - 2024 — McMap. All rights reserved.