3

We've got a legacy C Windows application which pops up a modal window via the MessageBox call when a fatal connection error occurs. Our network engineers may be running many of these applications at once. Occasionally, a network glitch will cause the connections being handled by these applications to fail simultaneously.

On Windows 7, selecting the 'Close all windows' function from the taskbar does work, because the modal dialog does not appear to be processing the WM_QUIT message. I typically work on Linux systems but my MSDN research indicates that the only way I can catch and process this message is by creating my own dialog and handling the messages myself.

My worry is that I've overlooked an easier solution, can anyone offer alternatives?

Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86
  • 1
    To close a window send it a `WM_CLOSE`, if it's the app's main windows the app is also ended. `WM_QUIT` is to end the whole app and with this close all windows owned by it. – alk Jul 02 '13 at 13:43
  • "*... selecting the 'Close all windows' function from the taskbar does work, because the modal dialog does not appear to be processing the WM_QUIT message.*" I do not really get the problem? Do you want to close a dialog pop-up, or quit an application? – alk Jul 02 '13 at 13:50
  • "*... the only way I can catch and process this message ...*" which message? – alk Jul 02 '13 at 14:08
  • I presume it is the WM_QUIT message, the one generated when the user clicks the 'Close All Windows' button in the Win7 taskbar. This is ignored by the modal window and prevents the application from closing – Gearoid Murphy Jul 02 '13 at 14:09
  • 1
    To get insight on issues like this a windows-message-logger is the tool of choice. With the pay-ware-version of VC there comes a tool named something like `spyXYZ.exe` for example. – alk Jul 03 '13 at 17:23

2 Answers2

3

The modal dialogue 's message loop should catch WM_QUIT and in response call EndDialog() and pass on the WM_QUIT message to the application's main window using PostMessage().


Update:

The approach as proposed above would work, if a WM_QUIT would be sent to the modal dialogue ... - but at least on my current win7 machine this isn't the case.

Moreover it is the case that the main window receives a WM_SYSCOMMAND with wParam set to SC_CLOSE and somehow the default message handler does ignore it (which might due to the modal dialogue box's styles...? I did not investigated this further.).

However, adding the following branch to the main window's message loop's switch should do the work of ending the application under the conditions describe by the OP:

  ...

  case WM_SYSCOMMAND:
    if (SC_CLOSE == wParam)
    {
      PostQuitMessage(<whatever code shall be returned>);
    }

    return DefWindowProc(...);
   
  ...
alk
  • 69,737
  • 10
  • 105
  • 255
1

Can be a tricky one this.

Usually, to quit an windows application you have to quit the "Windows message loop". The easiest way to do this is to post a quit message, e.g. PostQuitMessage(retCode), where retCode is a value that your main message loop handler can process. Typically, zero, i.e. ignore.

Typically, PostQuitMessage is posted in response to a WM_DESTROY message.

It really depends on the legacy code, you have my sympathies, I am dealing with legacy code also. My code has a separate message loop and continually displays modal dialog boxes. Call EndDialog and calling PostQuitMessage(0) terminated my application correctly.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Yes indeed, this is a tricky one. If interested in a working solution, please see my answer on this. – alk Jul 03 '13 at 17:06