Thread: Wondering if it's possible to close the application after another application closes.

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    8

    Post Wondering if it's possible to close the application after another application closes.

    I'm wondering if it's possible to listen in the background to if another application is running or not, and if it closes terminate the program?

    I have a game launcher that I've made execute the game itself, and I want this launcher to keep running in the background to disable the use of particular keys while the game is running, and then run a couple commands once it detects the game closes and close itself.

    Thanks ahead of time if you can point me in the right direction.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    if you are on windows you have a handle of the process you have started by your launcher app
    Waiting on this handle is possible with functions like WaitForSingleObject

    Process handle will be signaled as soon as process terminates and your wait function will exit.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why would you disable keys?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    8

    Post

    Quote Originally Posted by vart View Post
    if you are on windows you have a handle of the process you have started by your launcher app
    Waiting on this handle is possible with functions like WaitForSingleObject

    Process handle will be signaled as soon as process terminates and your wait function will exit.
    Thanks vart. I'll check this out. Is this using FindWindow()?

    Why would you disable keys?


    It's purely experimental.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    If you started process using CreateProcess then you have hProcess available as member of pi struct

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You can disable keys with a keyboard hook.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    8
    Quote Originally Posted by vart View Post
    If you started process using CreateProcess then you have hProcess available as member of pi struct

    https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
    Thank you very much vart and Yarin.

    So before getting into trying to create a keyboard hook to the second application to disable a few of it's keys, I created the second application using:

    Code:
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory (&si,sizeof(si));
        si.cb = sizeof(si);
        ZeroMemory (&pi,sizeof(pi));
    
    
        if (!CreateProcess(L"Game.exe", NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
            {
                cout << "Unable to execute Game.exe.";
            }
    Now within my while loop in my main() function I've put:

    Code:
                bool IsClosing = GetExitCodeProcess(pi.hProcess,appstatusifitsclosing);
                if(IsClosing == true)
                {
                    //Exit App (main() is a bool)
                    return false;
                }
    Which I've assumed will check every frame and see if the Game.exe process is closing. I can't understand how to use it from here, or why it's not working...

    I'm kind of a noob to all of this so I don't think I've defined lpExitCode correctly as well...

    EDIT:

    I've fixed the code, but it's still not working. The following is placed inside a loop:

    Code:
    			DWORD ExitDword = 0;
    			if (GetExitCodeProcess(pi.hProcess,&ExitDword) == FALSE)
    			{
    				//Exit App
    				return false;
    			}
    Last edited by markhansaven; 06-22-2015 at 11:07 PM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    you have missed following line
    Code:
    // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    8
    Thank you very much vart. Does WaitForSingleObject(), though, pause the parent application until the child is not selected, or terminates? I want this to instead listen every frame from behind the child to if it has closed or not, then if it doesn't exist terminate itself, although I understand that might be exactly what you're leading me to.

    I'm going to search for info on it and try it out.

  10. #10
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    "CreateProcess" is a very problematic function since Windows 8 and above. Microsoft added some new feature to Windows called UAC (user account control) which cause many trivial things (like creating processes) to need admin privileges. so de-facto, many times you'll only end up with some messagebox asking the user if it's ok to open a process or the process not being opened at all.

    you can try "ShellExecuteEx" instead that is a safer option.

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    8

    Post

    Quote Originally Posted by Dave11 View Post
    "CreateProcess" is a very problematic function since Windows 8 and above. Microsoft added some new feature to Windows called UAC (user account control) which cause many trivial things (like creating processes) to need admin privileges. so de-facto, many times you'll only end up with some messagebox asking the user if it's ok to open a process or the process not being opened at all.

    you can try "ShellExecuteEx" instead that is a safer option.
    Thanks Dave. I'll look into it.

    I think I'm going to study for a couple more days and see what answers and questions I can come up with.

    Tomorrow I'm going to try to get the parent program to close when the child closes out. I will try tinkering with GetExitCodeProcess() within a loop. I still need the application to check within a loop if the child process is in a state of exiting.

    Right now I am still using this code:


    Code:
    if(GetExitCodeProcess(pi.hProcess,&ExitDword) == FALSE){
        //Exit App
        return false;
    }
    Which is not working. I'll post tomorrow with what I can come up with.

    Thanks again.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Look at the doc for GetExitCodeProcess. It returns TRUE is it succeeds. You're terminating if it's false for some reason.
    Regardless, you're better of using WaitForSingleObject to determine if a process has exited or not. If you specify a timeout of 0, the function returns immediately. The return code specifies whether the object is still signaled or not (it's signaled if the process has terminated).
    After it has terminated, you can get its exit code if you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-17-2014, 08:43 PM
  2. Replies: 13
    Last Post: 08-30-2012, 08:51 PM
  3. Replies: 2
    Last Post: 05-18-2012, 04:20 AM
  4. Replies: 1
    Last Post: 07-03-2010, 01:18 PM
  5. how to disable the application close button?
    By e66n06 in forum Windows Programming
    Replies: 8
    Last Post: 01-05-2008, 11:33 AM