I have recently been trying to develop a simple WPF application in C#, and I need to be able to get the HwndSource of a window so I can add an event handler. Everytime I run the code, I get stopped on handleSource = HwndSource.FromHwnd(handle);
and I get the error System.ArgumentException: 'Hwnd of zero is not valid.'
. From my understanding, this happens when the window is not fully initialized or there is no window, or it is being destructed. If I have learned correctly, than I have no idea how this is happening.
I have not found anyone else with the same problem from googling, and my code is below.
public partial class MainWindow:Window {
SimConnect simConnect;
bool connectedToSim = false;
/// Window handle
IntPtr handle;
HwndSource handleSource;
const int WM_USER_SIMCONNECT = 0x0402;
/// <summary>
/// Constructor and starting for the window
/// </summary>
public MainWindow() {
InitializeComponent();
handle = new WindowInteropHelper(this).Handle; // Get handle of main WPF Window
handleSource = HwndSource.FromHwnd(handle); // Get source of handle in order to add event handlers to it
handleSource.AddHook(HandleSimConnectEvents);
Thread pollThread = new Thread(PollThread);
}
~MainWindow() {
if (handleSource != null) {
handleSource.RemoveHook(HandleSimConnectEvents);
}
}
private IntPtr HandleSimConnectEvents(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, ref bool isHandled) {
isHandled = false;
switch (message) {
case WM_USER_SIMCONNECT: {
if (simConnect != null) {
simConnect.ReceiveMessage();
isHandled = true;
}
}
break;
default:
break;
}
return IntPtr.Zero;
}
}
Any help would be appreciated, Thanks!