-
Hi, My current attempt is
I've seen the post saying so soon you'll see out object instead of void** or out void* , but a few months have passed since then. thank you in advance Context
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The comment about seeing For most cases, including this one, you need to read the API docs to learn what is required. In this case, this is an It looks like you're trying too hard by using using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Dwm;
unsafe static RECT GetExtendedFrameBounds(HWND hwnd)
{
IntPtr pRect = Marshal.AllocHGlobal(sizeof(RECT));
try
{
PInvoke.DwmGetWindowAttribute(
hwnd,
DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,
pRect.ToPointer(),
(uint)sizeof(RECT)).ThrowOnFailure();
return Marshal.PtrToStructure<RECT>(pRect);
}
finally
{
Marshal.FreeHGlobal(pRect);
}
} Note how use of IntPtr requires manually allocating memory, and taking care to free it within a Here's the easy way: using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Dwm;
unsafe static RECT GetExtendedFrameBounds(HWND hwnd)
{
RECT rect = default;
PInvoke.DwmGetWindowAttribute(
hwnd,
DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS,
&rect,
(uint)sizeof(RECT)).ThrowOnFailure();
return rect;
} See how we don't even need to allocate memory? It's enough that we have a local, and we point to that memory. No memory leak danger, and no try/finally blocks required. |
Beta Was this translation helpful? Give feedback.
The comment about seeing
out object
was specifically for that API, not this one.For most cases, including this one, you need to read the API docs to learn what is required. In this case, this is an
[Out] void*
that receives data based on the second argument.It looks like you're trying too hard by using
IntPtr
. IntPtr could be made to work, but it's the harder way: