windows - 在外部exe程序上跳过 'press any key to continue'

标签 windows batch-file cmd exe

我为我提供了一个cmd行exe,我无法对其进行更改,我需要编写一个脚本,但是它内置了Pause功能,并且看不到任何方法可以跳过此暂停,因此其余脚本可以继续。

我尝试了各种方法,包括

  • @echo | call program.exe
  • program.exe <否
  • cmd /c echo y &echo.| program.exe
  • 杰伊的答案here
  • 这些
  • 的变体和组合
  • 已检查程序/?查看是否有跳过暂停切换,但没有

  • 感谢任何建议

    最佳答案

    您可以使用互操作将数据发送到进程。这称为挂接进程,上面有一些资源。我喜欢这个answer

    This is a little code that allows you to send message to a backgrounded application. To send the "A" char for example, simply call sendKeystroke(Keys.A), and don't forget to use namespace System.windows.forms to be able to use the Keys object.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace keybound
    {
    class WindowHook
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
        public static void sendKeystroke(ushort k)
        {
            const uint WM_KEYDOWN = 0x100;
            const uint WM_SYSCOMMAND = 0x018;
            const uint SC_CLOSE = 0x053;
    
            IntPtr WindowToFind = FindWindow(null, "Untitled1 - Notepad++");
    
            IntPtr result3 = SendMessage(WindowToFind, WM_KEYDOWN, ((IntPtr)k), (IntPtr)0);
            //IntPtr result3 = SendMessage(WindowToFind, WM_KEYUP, ((IntPtr)c), (IntPtr)0);
        }
    }
    }
    

    您可能会比他们获得应用程序而不是搜索过程要容易得多,因为您可以从应用程序中启动它:
    Process proc = new Process();
    proc.StartInfo.FileName = executablePath;
    proc.Start();
    proc.WaitForInputIdle();
    

    然后proc.Id将成为PID。

    作为替代方案,我遇到了VB类型的example,使用Shell函数似乎更简单,但是我以前从未使用过。您需要在应用程序中添加一个暂停以等待提示,但是,与Interop相比,它看起来更干净:
    Dim ProcID As Integer 
    ' Start the Calculator application, and store the process id.
    ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
    ' Activate the Calculator application.
    AppActivate(ProcID)
    ' Send the keystrokes to the Calculator application.
    My.Computer.Keyboard.SendKeys("22", True)
    My.Computer.Keyboard.SendKeys("*", True)
    My.Computer.Keyboard.SendKeys("44", True)
    My.Computer.Keyboard.SendKeys("=", True)
    ' The result is 22 * 44 = 968.
    

    如果最后使用System.ArgumentException,则可能是因为Shell函数没有获取进程ID。这是因为它需要完全信任。如果以管理员身份运行,该应用程序将正常运行。如果您无法做到这一点,我认为您不会找到一种简便的方法,因为使应用程序彼此运行是一个安全问题,但是我可能是错的。

    关于windows - 在外部exe程序上跳过 'press any key to continue',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31139989/

    相关文章:

    java - 使用 .bat 文件执行 jar 文件

    php - 使用批处理文件中的参数运行exe文件

    windows - 如何将具有不同扩展名的文件作为 BAT 文件运行

    windows - 通过 CMD 的 FTP 文件以及创建日期

    windows - kdc-options 中的 Canonicalize 在 Windows 中自动设置为 true

    batch-file - 批处理文件 : Variable within a variable

    windows - 将远程 FTP 主机上的文件重命名为命令的输出 (Windows)

    找不到 Python 3.4 命令

    windows - 设置透明背景 Win32

    c++ - 询问哪个进程锁定了 Windows C++ 中的文件