Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,
I am trying to stop an exe which I called through Process.Start() using Ctrl+C.
I found a way using PInvoke. But it's not working.
The code is below and in the Line where I use
C#
bool result = GenerateConsoleCtrlEvent(CommandCtrlC, Convert.ToUInt32(command.Id));

GenerateConsoleCtrlEvent() method I am getting a true result too...

Can anybody point out where am I wrong?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace CommandTests
{
    class Program
    {
        static void Main(string[] args)
        {           
            Process command = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "cmd.exe";
            info.WorkingDirectory = @"c:\sox-14-3-2";
            info.UseShellExecute = false;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            command.StartInfo = info;
            command.Start();
            command.StandardInput.WriteLine("set AUDIODEV=Line 1 (Virtual Audio Cable)");
            command.StandardInput.WriteLine("rec.exe -d test.wav trim 0 00:10");
            Thread.Sleep(4000);
            UInt32 CommandCtrlC = 0;
            bool result=GenerateConsoleCtrlEvent(CommandCtrlC, Convert.ToUInt32(command.Id));
            Console.ReadLine();
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent,
            uint dwProcessGroupId);

    }
}
Posted
Updated 9-Mar-11 9:49am
v2

Use a keyboard hook, capture the Ctrl+C.

Get the handle of the process using <code> System.Diagnostics.Process.handle</code> property

Then use the Close function of the System.Diagnostics.Process

Use windows api methods to kill the opened applications.

Follow these links

Kill Application Using Win32 APIs[^]

Find and Close the Window using Win API[^]
 
Share this answer
 
v2
Comments
KingsKnight 9-Mar-11 5:11am    
Thanks Abel,

But the rec.exe I am using only accepts a CTRL+C to abort operation. So once the rec.exe started I can't kill the process(I mean even though I called a command.Kill() it won't stop.).

For command.close I tried to hook it with Console.Cancelkeypressed event. But the event is not raising at
bool result=GenerateConsoleCtrlEvent(CommandCtrlC, Convert.ToUInt32(command.Id));
TimGameDev 9-Mar-11 16:33pm    
I agree with SA, System.Diagnostics.Process.Kill should work fine for any process on the local machine.
I don't really understand why Kill is not working. You can always use System.Diagnostics.Process.Kill to kill any process (not only .NET application).

However, this might not be a valid solution, because your process does important post-mortal actions on Ctrl+C (as I understand, you don't have access to source code, otherwise embedding of the code in your assembly would not be a problem). You can make 100% perfect imitation of Ctrl+C by using P/Invoke for Windows API SendInput, http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^]. I tested it, works in all cases, because this is a very low-level even simulation, almost like coming out of hardware.

—SA
 
Share this answer
 
Comments
TimGameDev 9-Mar-11 16:43pm    
Actually I think that Process.Kill should work fine but you are right Kill method is used for force an application to quit. My 5!
Sergey Alexandrovich Kryukov 9-Mar-11 18:40pm    
Thank you, Timur,
--SA
Albin Abel 9-Mar-11 21:47pm    
My 5 too
Sergey Alexandrovich Kryukov 9-Mar-11 22:23pm    
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900