System.ExecutionEngineException
HResult=0x80131506
Source=
StackTrace:
Code: Select all
return PInvoke.CallWindowProc(wndProc, hWnd, Msg, wParam, lParam);
[img]https://i .sstatic.net/OtmYvz18.gif[/img]
Ich habe versucht zu überprüfen, ob das Fensterhandle null ist, aber das hat das Problem nicht gelöst. Ich habe auch versucht, durch Behandeln eine bessere Fehlermeldung zu erhalten, war jedoch erfolglos.
Reproduktion
GitHub-Repository
Ich verwende Visual Studio 2022 mit der Blank App, Packaged (WinUI 3 in Desktop) mit installiertem CsWin32-Paket.
Code: Select all
MainWindow.xaml.cs
Code: Select all
using System;
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using Windows.Win32.UI.WindowsAndMessaging;
namespace HotkeySubclassing;
public sealed partial class MainWindow : Window
{
private WNDPROC wndProc = null!;
private LRESULT HotKeyProc(HWND hWnd, uint Msg, WPARAM wParam, LPARAM lParam)
{
uint WM_HOTKEY = 0x0312; // HotKey Window Message
if (Msg == WM_HOTKEY)
{
if (ToggleButton.IsEnabled)
{
ToggleButton.IsChecked = !ToggleButton.IsChecked;
}
}
return PInvoke.CallWindowProc(wndProc, hWnd, Msg, wParam, lParam); // Error happens on this line
}
public MainWindow()
{
this.InitializeComponent();
// Get window handle
HWND hWnd = new(WinRT.Interop.WindowNative.GetWindowHandle(this));
// Register hotkey
int id = 0x0000;
_ = PInvoke.RegisterHotKey(hWnd, id, HOT_KEY_MODIFIERS.MOD_NOREPEAT, 0x75); // F6
// Add hotkey function pointer to window procedure
WNDPROC hotKeyDelegate = HotKeyProc;
nint hotKeyProcPtr = Marshal.GetFunctionPointerForDelegate(hotKeyDelegate);
nint wndPtr = PInvoke.SetWindowLongPtr(hWnd, WINDOW_LONG_PTR_INDEX.GWL_WNDPROC, hotKeyProcPtr);
wndProc = Marshal.GetDelegateForFunctionPointer(wndPtr);
}
}
Code: Select all
NativeMethods.txt
Code: Select all
RegisterHotKey
UnregisterHotKey
SetWindowLongPtr
CallWindowProc
Code: Select all
MainWindow.xaml
Code: Select all
Click Me