c++ - TProcess - 从多个 Process.Input.Write(...) 命令捕获输出

标签 c++ pascal freepascal lazarus chess

我想让国际象棋引擎分析一些 pgn 格式的游戏。

到现在为止。

begin
// ...
P:=TProcess.Create(nil);
stL:=TStringList.create;
P.Commandline:='Houdini'; // <- this is chess engine, Houdini.exe
P.Options:=P.Options+[poUsePipes];
P.ShowWindow:=swoHide;
P.Execute;

st:='uci'+ Lineending;
P.Input.Write(st[1], Length(st));
// st:=P.Output.ReadAnsiString;    <- this is my try
// showmessage(st);
st:='setoption name multipv value 3'+lineending;
P.Input.Write(st[1], Length(st)); // so after second command how to catch different output
st:='isready'+lineending;
P.Input.Write(st[1], Length(st));
st:='ucinewgame'+lineending;
P.Input.Write(st[1], Length(st));
st:='isready'+lineending;
P.Input.Write(st[1], Length(st));

// another part of code should be here ***

st:='quit'+lineending;
P.Input.Write(st[1], Length(st)); // quiting the engine
stL.LoadFromStream(P.Output);
stL.SaveToFile('AjDaVidime.txt');  // nothing stores particular

P.Free;
stL.Free; 
end;

所以问题是如何在每次 P.Input.Write 之后从引擎输出中获取输出...

代码的另一部分如下(这是为了奇怪的 stackoverflow 行为来发布我的问题。 此代码应在 *

中的第一个
// This for loop is main loop for analyzing chess game
for i:=1 to moves do begin
 st:='position fen '+arrayFen[i]+lineending;
 P.Input.Write(st[1], Length(st));
 st:='go movetime 1000'+lineending;  // Fen is position 1000ms is 1sec, so engine Must analyze 1 sec. that position, so go movetime 1000 is command.
 P.Input.Write(st[1], Length(st));
 Sleep(1000); // <- is this neccessary?
end; 

最佳答案

Afaik 不可能以跨平台方式等待 TProcess 的输出。管道上的读取可能会阻塞,直到收到指定的字节。

可能可以通过在流句柄(操作系统句柄)上使用 (baseunix.fp)select 或 (windows.)waitforsingleobject 来解决这个问题。

我不知道执行此操作的任何代码,但我认为 Lazarus 中的调试器代码是使用代码(调用 GDB)的最复杂的 TProcess,因此我建议研究一下。

关于c++ - TProcess - 从多个 Process.Input.Write(...) 命令捕获输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20054546/

相关文章:

c++ - 如何使用 g++ 与 -lrt 链接

C++ 字符串操作反转

c - 为什么 C 使用星号来声明指针,而不是像 Pascal 那样使用插入符号?

freepascal - FPC : RTTI on records

macos - 是否可以在Mac上安装Pascal编译器而不安装XCODE?

c++ - 这个结构试图做什么?

pascal - 什么是好的 Lazarus/FPC 资源?

pascal - 如何解释帕斯卡方程?

interpreter - Free Pascal 有命令行解释器吗?

C++ const_cast 问题