Terminate a child process of a parent process created using CreateProcess

I trying to execute a batch script via the CreateProcess method in C++.
This batch script in turn launches a JVM to execute a java class.

When I want to terminate the batch script, it only terminates the cmd and not the java process.

How do we terminate the java process, which is a child process of the batch script process.
closed account (Dy7SLyTq)
my generic blanket answer: windows api. although this time ill do some research http://stackoverflow.com/questions/1916574/how-to-effectively-kill-a-process-in-c-win32
Here is a snip you can use to see how to find and Terminate a process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	HANDLE hProcessSnap;
	HANDLE hProcess;
	PROCESSENTRY32 procEntry32;
	DWORD dwPriorety;

	hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPALL, 0 );
	if (hProcessSnap == INVALID_HANDLE_VALUE )
	{
	   cout << "$ ERROR: CreatToolhelpSnapshot.\nError Code: " << GetLastError() << endl;
	} else {
		cout << "$ Process snapshot. [OK]. " << endl;
		}

	procEntry32.dwSize = sizeof( PROCESSENTRY32 ); 

	if ( !Process32First( hProcessSnap, &procEntry32 ) )
	{
		cout << "Process32FIrst ERROR: " << GetLastError() << endl;
		CloseHandle( hProcessSnap );
		Sleep(10000);
		return 0;
	} else {
		cout << "$ Loading process list... [OK]" << endl;
		}

	do {		
		cout << "$ Process Name: " << procEntry32.szExeFile; 
		if ( "my_process.exe" == procEntry32.szExeFile )
		{
			HANDLE process = OpenProcess( PROCESS_ALL_ACCESS, 
                                                     FALSE, procEntry32.th32ProcessID);
			TerminateProcess(process, 0);
                        CloseHandle( process );
                        break;
		}
	} while ( Process32Next( hProcessSnap, &procEntry32 ) );


Hope it helps.

WetCode
Last edited on
If the process was created via CreateProcess() then you already have the required handle with all access rigths, no need for enumerating all running processes, just call TerminateProcess() on it.
If I did not misunderstand the OP uses CreateProcess to launch a batch script,
Witch in turn runs a 3rd Java process (somthing.jar) witch he don`t have the HANDLE to.

That's way he have to Enumerate the processes.
Last edited on
Then why the hell needs the batch script after all ? Why he can't launch the java process directly ?

Regarding your code, if there are more than one java applications running on that system, the code will pick one of them randomly and close the wrong one.

You probably need to get command line for each java process to see which one contains *.jar file name executed.
Regarding your code, if there are more than one java applications running on that system, the code will pick one of them randomly and close the wrong one.

The OP does state that they want to find the Java process which is the child process of the one which runs the launching batch file.

As the PROCESSENTRY32 struct includes a th32ParentProcessID member, the process ID returned by CreateProcess can be used to filter the results returned by Process32First/Process32Next to identify the correct child process.

Andy

PROCESSENTRY32 structure
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684839%28v=vs.85%29.aspx
Last edited on
Topic archived. No new replies allowed.