Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sending WM_CLOSE message to a Window

Sending WM_CLOSE message to a Window

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    confalonieri
    wrote on last edited by
    #1

    Hi. I'd like to close an application by sending to its main window the WM_CLOSE message. What I do is getting the ID of the process I want to stop, and then I use EnumWindows() function to get all top-level windows in the screen and in the callback function EnumWindowCallBack(), I compare the process ID with the ID of each top-level window. When the ID is the same, I send to the window the WM_CLOSE message thanks to ::PostMessage(). But, unfortunately, it doesn't work all the times. It seems that the windows I get in the callback function EnumWindowCallBack() are not the main windows of the applications I want to stop... For example, with calc.exe, the window is closed after having posted the WM_CLOSE message twice but the process calc.exe is still running without a window... Has anyone already met this problem ? Is there a way to be sure to get the right window of an application (and not a child window that doesn't handle the WM_CLOSE message) ? Thanks.

    T 1 Reply Last reply
    0
    • C confalonieri

      Hi. I'd like to close an application by sending to its main window the WM_CLOSE message. What I do is getting the ID of the process I want to stop, and then I use EnumWindows() function to get all top-level windows in the screen and in the callback function EnumWindowCallBack(), I compare the process ID with the ID of each top-level window. When the ID is the same, I send to the window the WM_CLOSE message thanks to ::PostMessage(). But, unfortunately, it doesn't work all the times. It seems that the windows I get in the callback function EnumWindowCallBack() are not the main windows of the applications I want to stop... For example, with calc.exe, the window is closed after having posted the WM_CLOSE message twice but the process calc.exe is still running without a window... Has anyone already met this problem ? Is there a way to be sure to get the right window of an application (and not a child window that doesn't handle the WM_CLOSE message) ? Thanks.

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      Knowledge Base article Q178893 - "HOWTO: Terminate an Application 'Cleanly' in Win32" - deals with these issues. Basically, you should call WaitForSingleObject after posting WM_CLOSE. You can find the article online: http://support.microsoft.com/support/kb/articles/Q178/8/93.ASP Cheers Tomasz Sowinski http://www.shooltz.com.pl

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups