In this alternative to closing the Report Destination window, we are using polling instead of event notification.
I will only be presenting the deviations from the original. To use the alternative, replace the Closer (file and class) in the original posting with the included
Step1: Detecting the Report Destination window
Instead of registering a Windows event hook and wait to be notified when the foreground window has changed, we are running a timer, continuously looking for our Report Destination dialog:
void closerTimer_Elapsed(object sender, ElapsedEventArgs e)
closerTimer.Enabled = false;
hWndDlg = FindWindow(null, REPORT_DESTINATION_CAPTION);
if ((int)hWndDlg != 0)
closerTimer.Enabled = true;
We identify the dialog based on its caption using the Win32 API
FindWindow() function. The Report Destination window does not have to be the topmost window on our screen.
Step2: Closing the report destination window
We must ensure the keyboard input is directed to our dialog when we are sending the keystrokes to close it. As such, before issuing the keystrokes we are always checking if the Report Destination window is the topmost window on our screen. For this purpose we use the Win32 API
if (GetForegroundWindow() == hWndDlg)
I do not think that the alternative presented here is any more suitable than the original for our 32-bit Microsoft Dynamics GP Add-in integration library. It may be perhaps a better fit for an out-of-process integration as it alleviates the reentrancy issue of the original. However only the code in the original article was properly tested.