在cmd中调用Ctrl + c停止运行exe [英] Invoking Ctrl+c in cmd to stop an exe running

查看:609
本文介绍了在cmd中调用Ctrl + c停止运行exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图停止使用Ctrl+C通过Process.Start()调用的exe.
我找到了使用PInvoke的方法.但这不起作用.
该代码在下面和其中使用

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

bool result = GenerateConsoleCtrlEvent(CommandCtrlC, Convert.ToUInt32(command.Id));


GenerateConsoleCtrlEvent()方法我也得到了true结果...

有人可以指出我在哪里错吗?


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

Can anybody point out where am I wrong?

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);

    }
}

推荐答案

使用键盘挂钩,捕获Ctrl + C.

使用< code>获取流程的句柄. System.Diagnostics.Process.handle</code>物业

然后使用 System.Diagnostics.Process
Close功能
使用Windows api方法杀死打开的应用程序.

跟随这些链接

使用Win32 API杀死应用程序 [使用Win API查找并关闭窗口 [
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[^]


我不太明白为什么Kill不管用.您始终可以使用System.Diagnostics.Process.Kill杀死任何进程(不仅是.NET应用程序).

但是,这可能不是一个有效的解决方案,因为您的进程在Ctrl + C上执行了重要的致命操作(据我所知,您无权访问源代码,否则无法将代码嵌入到程序集中一个问题).您可以使用P/Invoke for Windows API SendInput
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


这篇关于在cmd中调用Ctrl + c停止运行exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆