Skip to content

Commit

Permalink
Prevent garbage collection of Win32 callback
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed Feb 4, 2024
1 parent b95b0de commit 3255969
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ThePBone.Interop.Win32/WndProcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public partial class WndProcClient
public event EventHandler<WindowMessage>? MessageReceived;

private readonly IntPtr _hwnd;
/* prevent garbage collection */
private readonly Unmanaged.WNDCLASSEX _wndClassEx;
public IntPtr WindowHandle => _hwnd;

public WndProcClient()
{
Unmanaged.WNDCLASSEX wndClassEx = new Unmanaged.WNDCLASSEX
_wndClassEx = new Unmanaged.WNDCLASSEX
{
cbSize = Marshal.SizeOf<Unmanaged.WNDCLASSEX>(),
lpfnWndProc = delegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) {
Expand All @@ -36,7 +38,7 @@ public WndProcClient()
lpszClassName = "MessageWindow " + Guid.NewGuid(),
};

ushort atom = Unmanaged.RegisterClassEx(ref wndClassEx);
ushort atom = Unmanaged.RegisterClassEx(ref _wndClassEx);

if (atom == 0)
{
Expand Down

0 comments on commit 3255969

Please sign in to comment.