Thread: Close Applications from C program...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    Close Applications from C program...

    Hi,

    I want to open and close an .exe file (e.g; Notepad.exe) from a C program... I can open it using
    Code:
    system(".exe path");
    But I don't know how to close the application from the program... Any ideas?

    Thanks,
    - Manu

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    (Hinted Windows as OS)

    How To Terminate an Application "Cleanly" in Win32

    You should also check out:

    ::CreateProcess(..)
    ::PostThreadMessage(..)

    To start up and post a WM_CLOSE message respectively.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    For UNIX Environmet...

    You can fork() and then exec() (one of the exec family dunctions i mean). The control flow of the child goes to the called program. The process exits when the child terminates...

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Thank you!

    It almost worked, except that I have another problem:

    Code:
    #include <stdio.h> 
    int main() { 
    system("C://WINNT//NOTEPAD.EXE"); 
    system("taskkill /F /IM notepad.exe"); 
    }

    These commands, when they are individually called from the command prompt, they are able to open and close the applications (like this notepad.exe)... But if I use it as the above program, the program block as soon as the "notepad" is opened. The control is not handed back to the program's next line until notepad is closed (for which I wrote the next line!)

    Can you tell me how do I prevent this? Or, are there any workarounds?
    Is there anything equivalent to fork() in Windows?

    I appreciate you help!
    -Manu

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    What about

    Code:
    #include<process.h>
    #include<stdlib.h>
    #include<stdio.h>
    
    void start();
    void end();
    
    int main()
    {
        _beginthread( start, 0, NULL );  // open notepad
        sleep( 1000 );    //pause for 1 sec
        _beginthread( end, 0, NULL ); // kill start end() thread.
    
        
        system( "PAUSE" );
        return 0;
    }
    
    void start()
    {
         system( "notepad" );
    }
    
    void end()
    {
              system("taskkill /F /IM notepad.exe"); 
     }

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    All this "system()" stuff, bleh.

    Code:
    #include <windows.h>
    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
    	DWORD processId;
    	GetWindowThreadProcessId(hwnd, &processId);
    
    
    	if(lParam == processId)
    		PostMessage(hwnd, WM_CLOSE, 0, 0);
    	return TRUE;
    }
    int main(int argc, char *argv[]) {
    	STARTUPINFO         siInfo;
    	PROCESS_INFORMATION piInfo;
    
    	ZeroMemory(&siInfo, sizeof(siInfo));
    	ZeroMemory(&piInfo, sizeof(piInfo));
    
    
    	CreateProcess
    		("C:\\WINDOWS\\Notepad.exe", "", 0, 0, 
    		FALSE, CREATE_DEFAULT_ERROR_MODE, 0, 0, 
    		&siInfo, &piInfo);
    
    
    	WaitForSingleObject(piInfo.hProcess, 1000);
    	EnumWindows(&EnumWindowsProc, piInfo.dwProcessId);
    	return 0;
    }
    Edit: This is the C forum
    Last edited by Tonto; 10-03-2005 at 05:19 PM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Thanks a lot guys... It worked finally....

    I had probelms executing even the small statement: _beginthread... Later I found out that I need to specify "multi-threading" options to the compiler while compiling this program...

    I am still facing problems with the sleep() method... Any better functions to suspend the execution for a while and then resuming it?

    Thanks a lot,
    -Manu

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am still facing problems with the sleep() method
    What problems then?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    The error is while linking:

    Code:
    Error:
    Thread_Run_Tests.obj : error LNK2001: unresolved external symbol _sleep
    Debug/Thread_Run_Tests.exe : fatal error LNK1120: 1 unresolved externals
    This was similar to the error I got for _beginthread()... I specified the "multi-threading" option for compiling, and it got solved... I'm still not able to figure the problem with sleep()

    Thanks a lot for the good responses, guys
    - Manu

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Try Sleep with a capital "S" instead
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Same problem
    Code:
    Linking...
    Thread_Run_Tests.obj : error LNK2001: unresolved external symbol _Sleep
    Debug/Thread_Run_Tests.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your linker cannot find the object file containing that function. Are you linking with the correct parameters? What compiler are you using?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  3. Detecting program close
    By HybridM in forum Windows Programming
    Replies: 2
    Last Post: 01-17-2004, 10:08 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM