CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2007
    Location
    Pune, INDIA
    Posts
    128

    Question Closing a MFC application from command prompt

    Hi,

    I have created a MFC dialog based application. Now I want to implement such a functionailty that I will be closing my application through command prompt.
    And while closing of my application the ExitInstance() method of my application should get called.

    Please guide me with the command which I should put in the command prompt to close my application in such a way.


    Thanks in Advance,
    Mayank

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Closing a MFC application from command prompt

    To gracefully close some GUI Windows application from outside of it one has to PostMessage WM_CLOSE to its main window.
    I have no idea, however, how it could be dome from a command prompt.
    See http://support.microsoft.com/kb/178893
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Closing a MFC application from command prompt

    You could write a little console app to send the WM_CLOSE message, or perhaps initiate a DDE conversation that would close other app.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Closing a MFC application from command prompt

    In case you have some application running, no matter it's MFC or not, and run it another time in command prompt, you have two different processes. So, to close some running process by running another one, you have to implement an inter-process communication of some kind, which is not just a command that you can "put in the command prompt."

    In one of my apps I had a dedicated thread waiting for a global event to signal. On catching the signal it sent a closing message to main window, so the application could shut down normal way (including ExitInstance). Of course, another application opened the global event object and signaled it. As you can see, here I created an inter-process communication based on system event object shared between the applications. You can replicate the scheme, or come up with your own version of IPC.
    Best regards,
    Igor

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Closing a MFC application from command prompt

    Quote Originally Posted by GCDEF View Post
    You could write a little console app to send the WM_CLOSE message, or perhaps initiate a DDE conversation that would close other app.
    Sure one could!
    But as long as some program would be designed for this goal then it should NOT matter whether such a program would start via command prompt or any other way.
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2007
    Location
    Pune, INDIA
    Posts
    128

    Re: Closing a MFC application from command prompt

    Quote Originally Posted by VictorN View Post
    Sure one could!
    But as long as some program would be designed for this goal then it should NOT matter whether such a program would start via command prompt or any other way.
    Thanks To all for your valuable inputs. Well I found this following command which is shutting down my application gracefully from command prompt in Windows 7.

    TASKKILL /IM TestApp.exe /T

    This is also calling the ExitInstance() method of my application. Still I need to check how much successfull it is.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Closing a MFC application from command prompt

    Quote Originally Posted by VictorN View Post
    Sure one could!
    But as long as some program would be designed for this goal then it should NOT matter whether such a program would start via command prompt or any other way.
    I'm not quite sure what you're saying here, but the OP did say he wrote the app that needs to close. DDE's pretty simple to implement and it's one of several approaches. Seems like there ought to be an easier way, but if there is, I don't know what it is.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Closing a MFC application from command prompt

    Quote Originally Posted by mayank_3103 View Post
    Thanks To all for your valuable inputs. Well I found this following command which is shutting down my application gracefully from command prompt in Windows 7.

    TASKKILL /IM TestApp.exe /T
    Wow!
    Good to know such a command exists (also in XP, according to http://technet.microsoft.com/en-us/l.../bb491009.aspx
    I never knew about this command...
    So, we still need to learn!

    Quote Originally Posted by GCDEF View Post
    I'm not quite sure what you're saying here, but the OP did say he wrote the app that needs to close. DDE's pretty simple to implement and it's one of several approaches. Seems like there ought to be an easier way, but if there is, I don't know what it is.
    I was saying exactly what I wrote (however I did not concern DDE):
    If we design a program for this purpose then the program has to work independent of the way it will start.

    Fortunately, OP has found a solution he needed to use (very simple!) from command prompt!
    Victor Nijegorodov

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Closing a MFC application from command prompt

    Keep in mind that taskkill may not gracefully close your app, so your ExitInstance() method may not always get called (if taskkill needs to call terminateprocess on your app).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured