Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Can't get DwmGetWindowAttribute to work #527

Closed
Slion opened this issue Oct 5, 2020 · 6 comments · Fixed by #528
Closed

Can't get DwmGetWindowAttribute to work #527

Slion opened this issue Oct 5, 2020 · 6 comments · Fixed by #528
Assignees
Labels

Comments

@Slion
Copy link

Slion commented Oct 5, 2020

I tried a bunch of stuff including the following all I get is null reference exception thrown by DwmGetWindowAttribute.

        PInvoke.RECT rect = new PInvoke.RECT();
        int size = Marshal.SizeOf(typeof(PInvoke.RECT));
        IntPtr rectPtr = Marshal.AllocHGlobal(size);
        PInvoke.DwmApi.DwmGetWindowAttribute(hwnd, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,out rectPtr,size);
        Marshal.PtrToStructure(rectPtr, rect);
        Marshal.FreeHGlobal(rectPtr);

It's a lot faster to get it working using that solution:
https://stackoverflow.com/a/58675103/3969362

@AArnott AArnott added the bug label Oct 5, 2020
@AArnott AArnott self-assigned this Oct 5, 2020
@AArnott
Copy link
Collaborator

AArnott commented Oct 5, 2020

It looks like we incorrectly use out and a pointer type, which turns into a double-pointer.

AArnott added a commit that referenced this issue Oct 5, 2020
The native type for the pvAttribute parameter is PVOID but we were using out as well as �oid* so we were effectively using double-pointers.

Fixes #527
@AArnott AArnott closed this as completed Oct 5, 2020
@AArnott
Copy link
Collaborator

AArnott commented Oct 5, 2020

Thanks for reporting, @Slion

@Slion
Copy link
Author

Slion commented Oct 6, 2020

Congrats on the quick turn around. @AArnott Any chance of having an overload taking RECT as parameter much like on that stackoverflow post linked above?

@AArnott
Copy link
Collaborator

AArnott commented Oct 6, 2020

No. Our convention is to use the same types used in the native signature. But it shouldn't be hard for you to use RECT. Much easier than your original code nsippet.

Something like this:

unsafe void Foo()
{
        PInvoke.RECT rect = new PInvoke.RECT();
        PInvoke.DwmApi.DwmGetWindowAttribute(hwnd, DwmApi.DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(rect));
}

Don't worry about the 'unsafe' code. It doesn't mean your caller has to be unsafe. And honestly, this way you're not responsible for allocating, freeing, and copying native memory that could leak. C# pointer code is much easier than all this IntPtr nonsense that was invented to support poor VB.

@Slion
Copy link
Author

Slion commented Oct 6, 2020

@AArnott Thanks for that insight. When will that fix make it into a release?

@AArnott
Copy link
Collaborator

AArnott commented Oct 6, 2020

It looks like it's been quite a while, with quite a bit of work, since our last release. I'll go ahead and release to nuget.org today.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants