Thread: Terminate a process

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    20

    Terminate a process

    Hi

    I start a console process like dir with ShellExecute function. Is there any way to stop this process from my program the time I want ? ( it might be in the middle of execution of the dir command or in the end of execution of dir command )

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    ShellExecute return a HINSTANCE handle, which I don't know how to operate in order to terminate the process...

    Like you said, you want to create a process:
    Use the CreateProcess funtion, and after calling the function, you can use the hProcess handle in the PROCESS_INFORMATION structure with TerminateProcess function.
    This should work, only for processes. For command lines like dir, this doesn't work.

    Code:
    HANDLE proc;
    
    BOOL Run(char *exe){
    	STARTUPINFO inf;
    	PROCESS_INFORMATION procinf;
    
    	ZeroMemory(&inf, sizeof(STARTUPINFO));
    	inf.cb=sizeof(STARTUPINFO);
    	ZeroMemory(&procinf, sizeof(PROCESS_INFORMATION));
    
    	BOOL res = CreateProcess(exe,0,0,0,0,0,0,0, &inf, &procinf);
    	proc = procinf.hProcess;
    	return res;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Calling ShellExecuteEx() will give you a handle to the process. You can then use this handle to call TerminateProcess().

  4. #4
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I am pretty sure TerminateProcess() kills with extreme prejudice, no shutdown time for dir.
    "Assumptions are the mother of all **** ups!"

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I am pretty sure TerminateProcess() kills with extreme prejudice, no shutdown time for dir.
    Yep, but I think that was the point. He wanted to be able to terminate a process while it was in the middle of doing something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Terminate Process by Specific FullFilePath
    By MattKamil in forum Windows Programming
    Replies: 1
    Last Post: 07-05-2006, 04:15 PM