-1

There are three applications main.exe (C++), one.exe (C++) and two.exe (.net).

I want to terminate one.exe and two.exe from main.exe if they are running.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
JAI SINGH
  • 27
  • 6
  • 5
    Possible duplicate of [How to effectively kill a process in C++ (Win32)?](http://stackoverflow.com/questions/1916574/how-to-effectively-kill-a-process-in-c-win32) – Pavel Pája Halbich Jul 26 '16 at 08:02
  • 2
    Tell us more about these programs. The answers below while correct are not necessarily appropriate. `WM_CLOSE` works only for GUI programs and even then sending `WM_CLOSE` may not actually terminate the program but just trigger an "Are you sure?" dialog. `TerminateProcess` is rather crude and may cause all kinds of problems, imagine your program is in the middle of updating a database and it gets suddenly killed; this may leave your database in an inteterminate state or even corrupt it. Do you have the source code of `one.exe`and `two.exe` ? – Jabberwocky Jul 26 '16 at 08:26

3 Answers3

0

Normal GUI applications? Just send them WM_CLOSE.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

If you have Process ID for one.exe and two.exe you can get process handle and use TerminateProcess function. Here is the code sample:

HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
if (hProcess == NULL)
{
  return;
}

UINT uExitCode;
BOOL result = TerminateProcess(hProcess, uExitCode);
CloseHandle(hProcess);
Nikita
  • 6,270
  • 2
  • 24
  • 37
0

If it's a GUI application, you may send a WM_CLOSE message to the other application.

Or if you want to kill the other process, then use TerminateProcess() function.

ninja
  • 809
  • 5
  • 9