You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement IHandle and add associated overloads to methods accepting IntPtr #602
I am looking at converting Winforms to use CsWin32, and stumbled into usages that make use of IHandle. They are used to wrap the pinvoke call and then call GC.KeepAlive on the IHandle.
Should an overload be provided by CsWin32 that provides this functionality? Does it already do so? Or should I keep the wrapper and remove the PInvoke?
The IHandle interface you mention appears to be internal to Windows Forms and this is the first I've ever heard of it.
The standard pattern is to use the public HandleRef struct. The interop stub calls GC.KeepAlive on HandleRef.Object and passes HandleRef.Handle to the native call.
The IHandle interface you mention appears to be internal to Windows Forms and this is the first I've ever heard of it.
It is a pattern we use to "bind" a handle to its owner. A common mistake made in the WinForms runtime was associating the wrong owner with a given handle with GCHandle. Code would either not get the right finalizable object associated or randomly pick a class for handles that came straight from Windows.
It also allows for slightly more efficient handling as you're not creating and filling a temporary struct and making the generated code more complicated. It also allows avoiding boxing of struct handle wrappers when you write wrappers as generic and constrain to the interface. .NET is smart enough to not introduce a box when you pass a struct to GC.KeepAlive (at least in current versions).
Note that I plan to iterate on it and make it an IHandle<T> (IHandle<HWND> for example). I've prototyped, but haven't found the time to implement it yet.
You can also use this pattern to associate extension methods with an "owned" handle. Something like:
public static unsafe Rectangle GetClientRectangle<T>(this T window)
where T : IHandle<HWND>
{
Unsafe.SkipInit(out RECT rect);
Error.ThrowLastErrorIfFalse(PInvoke.GetClientRect(window.Handle, &rect));
GC.KeepAlive(window);
return rect.ToRectangle();
}
Providing the same pattern here might not be practical, but it is useful to understand and consider.
Did you want this all handled in winforms? or did you want CsWin32 to eventually emit it?
It would be nice to have CsWin32 have the type at least and possibly the ability to generate "friendly" overloads that use it. I'm currently working on a PR in WinForms that more fully fleshes this out and will follow up on this issue with details about the patterns.
If there's more details on the request here, feel free to post and re-open. But all SafeHandles are partial now so if you want to implement an interface on them, you can. Customizing the codegen further feels out of scope to me.
Activity
jnm2 commentedon Jul 16, 2022
The IHandle interface you mention appears to be internal to Windows Forms and this is the first I've ever heard of it.
The standard pattern is to use the public HandleRef struct. The interop stub calls GC.KeepAlive on HandleRef.Object and passes HandleRef.Handle to the native call.
JeremyKuhne commentedon Jul 27, 2022
It is a pattern we use to "bind" a handle to its owner. A common mistake made in the WinForms runtime was associating the wrong owner with a given handle with
GCHandle. Code would either not get the right finalizable object associated or randomly pick a class for handles that came straight from Windows.It also allows for slightly more efficient handling as you're not creating and filling a temporary struct and making the generated code more complicated. It also allows avoiding boxing of struct handle wrappers when you write wrappers as generic and constrain to the interface. .NET is smart enough to not introduce a box when you pass a struct to
GC.KeepAlive(at least in current versions).Note that I plan to iterate on it and make it an
IHandle<T>(IHandle<HWND>for example). I've prototyped, but haven't found the time to implement it yet.You can also use this pattern to associate extension methods with an "owned" handle. Something like:
Providing the same pattern here might not be practical, but it is useful to understand and consider.
elachlan commentedon Jul 30, 2022
Probably a good idea to implement it along side #596.
elachlan commentedon Aug 10, 2022
Jeremy Kuhne (@JeremyKuhne) Did you want this all handled in winforms? or did you want CsWin32 to eventually emit it?
JeremyKuhne commentedon Aug 10, 2022
It would be nice to have CsWin32 have the type at least and possibly the ability to generate "friendly" overloads that use it. I'm currently working on a PR in WinForms that more fully fleshes this out and will follow up on this issue with details about the patterns.
jevansaks commentedon Nov 22, 2025
If there's more details on the request here, feel free to post and re-open. But all SafeHandles are partial now so if you want to implement an interface on them, you can. Customizing the codegen further feels out of scope to me.