如何从中引发事件getche()过程。波本()不监视stdin

2024-09-22 16:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在windows中启动二进制文件,使用:

process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)

ch.input comes from:

ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()

这是从stdout读取的一种很酷的非阻塞方法。这是这个问题的公认答案: How can I read all availably data from subprocess.Popen.stdout (non blocking)?

这是我试图启动/监视进程的大部分脚本:

^{pr2}$

不管怎样,我可以用这个方法从过程中读出来。当我试图使用以下命令写入进程时:

i=标准工艺 i、 写入(“s\n”) 或 i、 写入(“s”)

输出在控制台窗口中打印,但不被二进制文件捕获。我查看了idapro中的二进制文件,发现它在运行时使用了一种非标准的方法来捕获用户输入。在

该进程是一个交互式的cli工具,它等待S、R、p或Q(Status、Resume、Pause、Quit)***并捕获输入,用户点击ente***R。这是使用getche()完成的。我把二进制文件扔给了IDA Pro以确认:

v7 = getche();
if ( (unsigned int)dword_41F060 > 1 )
  sub_4030A0(&unk_41C111, v5);
WaitForSingleObject((HANDLE)dword_41F040, 0xFFFFFFFFu);
if ( v7 == 113 )
{
  if ( dword_41F060 != 1 )
  {
    if ( dword_41F060 )
      dword_41F060 = 6;
  }
}
else
{
  if ( v7 <= 113 )
  {
    if ( v7 == 112 )
    {
      if ( dword_41F060 == 2 )
        QueryPerformanceCounter((LARGE_INTEGER *)&qword_41F128);
      dword_41F060 = 3;
      sub_4030A0("Paused", v5);
    }
  }
  else
  {
    if ( v7 == 114 )
    {
      if ( dword_41F060 == 3 )
      {
        QueryPerformanceFrequency((LARGE_INTEGER *)&v9);
        QueryPerformanceCounter((LARGE_INTEGER *)&v8);
        v3 = (double)(v8 - qword_41F128);
        LODWORD(v4) = sub_413FF0(v9, v10, 1000, 0);
        dbl_41F138 = v3 / (double)v4 + dbl_41F138;
      }
      dword_41F060 = 2;
      sub_4030A0("Resumed", v5);
    }
    else
    {
      if ( v7 == 115 )
        sub_40AFC0();
    }
  }
}

有人知道怎么触发这个事件吗?这看起来很有希望:http://docs.python.org/library/msvcrt.html我尝试使用:

在msvcrt.ungetch公司(“s”) 使字符字符“推后”到控制台缓冲区;它将是getch()或getche()读取的下一个字符

它确实将字母“s”推送到控制台,但没有触发getche()上的断点。手动敲击字母S是有效的,并且确实会导致IDA pro命中断点

哈普?:)

编辑:

我创建了两个小的windows控制台应用程序来演示什么是有效的,什么是不起作用的,并确保我的python是正常的。在

第一个我不能通过写stdin来识别输入,第二个我可以。在

#include "stdafx.h"
#include <conio.h>


int _tmain(int argc, _TCHAR* argv[])
{
    char response;
    printf("Enter \"s\":\n");
    response = _getch();
    printf("You entered %c\n", response);
    return 0;
}

可以写信给这个人:

#include "stdafx.h"
#include <conio.h>


int _tmain(int argc, _TCHAR* argv[])
{
    char response [2];
    printf("Enter \"s\":\n");
    gets(response);
    printf("You entered %s", response);
    return 0;
}

编辑2 我也试过: 导入子流程 导入时间 从e进口* 导入msvcrt

ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()


cmd = ["test-getch.exe"]
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
i = process.stdin
answers = []
msvcrt.putch('s')
ch.flush
waitforresults(ch, answers, expect = 1)
for answer in answers:
    print answer.getvalue()
i.close()
time.sleep(3)
#process.terminate()
ch.stop()
del process, ch
print "DONE"

Tags: 文件ifresponsestdinstdout二进制chprocess
1条回答
网友
1楼 · 发布于 2024-09-22 16:38:19

MSDN摘录:

These routines read and write on your console. The console I/O routines are not compatible with stream I/O or low-level I/O library routines. In the Windows operating systems, the output from these functions is always directed to the console and cannot be redirected.

因此,我看到了两种可能的选择,但都没有希望:

  1. 创建一个伪Windows控制台并从中运行程序。然后你会得到你的输出。(UNIX上的pty模拟)
  2. 以某种方式从现有的Windows控制台获取数据。控制台2http://sourceforge.net/projects/console/正在做类似的事情。在

我不知道怎么做这些事情,也不知道是否有可能。在

无论如何,我看不出任何切实可行的解决办法,基本上就是说你运气不好。在

相关问题 更多 >