Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The app crashes when the CallWindowProc function is used #10086

Closed
MGGSK opened this issue Oct 18, 2024 · 2 comments
Closed

The app crashes when the CallWindowProc function is used #10086

MGGSK opened this issue Oct 18, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@MGGSK
Copy link

MGGSK commented Oct 18, 2024

Describe the bug

The app crashes when the window receives a message. The thrown exception is a System.ExecutionEngineException. It can't be catched using a try block or the App.UnhandledException event. Visual Studio shows that the call to "CallWindowProc" caused the exception (As seen in the screenshot).

Steps to reproduce the bug

  1. Override the default window procedure (IntPtr oldWndProc = SetWindowLongPtr(hWnd, -4, CustomWindowProc);)
  2. Call the window procedure that is build into WinUI (CallWindowProc(oldWndProc , hWnd, msg, wParam, lParam);)

Expected behavior

The window should have a minimum size and all other messages should be handled by WinUI.

Screenshots

Image

NuGet package version

WinUI 3 - Windows App SDK 1.6.1: 1.6.240923002

Windows version

Windows 11 (22H2): Build 22621

Additional context

The code from my app:

IntPtr oldWndProc = IntPtr.Zero;
void RegisterWindowMessageHandler()
    => oldWndProc = User32.SetWindowLongPtr(hWnd, -4, NewWindowProc);

private IntPtr NewWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
    switch (msg)
    {
        case WM_GETMINMAXINFO:
            var minMaxInfo = Marshal.PtrToStructure<MINMAXINFO>(lParam);
            minMaxInfo.ptMinTrackSize.X = minWidth; // Minimum width
            minMaxInfo.ptMinTrackSize.Y = minHeight; // Minimum height
            Marshal.StructureToPtr(minMaxInfo, lParam, true);
            break;

        default:
            break;
    }

    return User32.CallWindowProc(oldWndProc , hWnd, msg, wParam, lParam);
}
@MGGSK MGGSK added the bug Something isn't working label Oct 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Oct 18, 2024
@castorix
Copy link

You must use

private delegate IntPtr WinProc(IntPtr hWnd, PInvoke.User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
private WinProc newWndProc = null;
newWndProc = new WinProc(NewWindowProc);

like in this thread
(but MS recommends SetWindowSubclass (more recent))

@MGGSK
Copy link
Author

MGGSK commented Oct 18, 2024

You must use

private delegate IntPtr WinProc(IntPtr hWnd, PInvoke.User32.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
private WinProc newWndProc = null;
newWndProc = new WinProc(NewWindowProc);

like in this thread (but MS recommends SetWindowSubclass (more recent))

That fixed it.

@MGGSK MGGSK closed this as completed Oct 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the needs-triage Issue needs to be triaged by the area owners label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants