如何将DLL中的stdout重定向到TMemo

 mobiledu2502899727 发布于 2023-01-16 18:14

我有一个第三方dll,我无法更改并以不规则的间隔将其输出发送到stdout.我想捕获标准输出并将其显示在TMemo控件中.之前的答案(Delphi - 从静态链接的MSVC++编译的DLL捕获stdout和stderr输出)显示了如何将这样的输出捕获到文件,我想将其捕获到TMemo.

可能的解决方案:我可以读取文件,因为它填满了stdout的输出并轮询了文件,但是我需要从先前保存的输出中识别新输出.PLus它似乎不是一个真正的解决方案.我已经做了很多互联网搜索,我发现最常见的相关答案是如何从外部应用程序捕获stdout,这不是我想要做的,我想从dll捕获输出.还有这个代码片段,但我真的不明白它在做什么或如何使用它 - http://embarcadero.newsgroups.archived.at/public.delphi.language.delphi.win32/201010/10101510449. HTML但它似乎解决了同样的问题.有没有人在他们的互联网旅行中发现任何与此主题相关的内容?

我有一些基于Pipes工作的东西,并不像我想象的那么困难,但有一件事让我很烦恼.这是代码:

var
  TextBuffer: array[1..32767] of AnsiChar;
  TextString: AnsiString;
  BytesRead, BytesRem: cardinal;
  PipeSize: cardinal;
  Security : TSecurityAttributes;
begin
 Security.nlength := SizeOf(TSecurityAttributes) ;
 Security.binherithandle := true;
 Security.lpsecuritydescriptor := nil;
 if CreatePipe(outputPipeRead, outputPipeWrite, @Security, 0) then
    begin
    SetStdHandle(STD_OUTPUT_HANDLE, outputPipeWrite);
    end
 else
    showmessage ('Error in creating pipe');

 .... Call dll here so that it sends output to stdout

 PipeSize := Sizeof (textbuffer);
 PeekNamedPipe (outputPipeRead, nil, PipeSize, @BytesRead, @PipeSize, @BytesRem);
 if BytesRead > 0 then
    begin
    if ReadFile(outputPipeRead, TextBuffer, PipeSize, BytesRead, nil) then
       begin
       // a requirement for Windows OS system components
       OemToChar(@TextBuffer, @TextBuffer);
       TextString := AnsiString(TextBuffer);
       SetLength(TextString, BytesRead);
       mOutput.Lines.Add (TextString);
       end;
    end
else
    showmessage ('No text');

这确实捕获输出并将其存放在TMemo中,但我不明白为什么(经过多次试验和错误)后,setstdhandle将outputPipeWrite分配给stdout但是通过outputPipeRead读取管道?这段代码的灵感来自 http://www.tek-tips.com/faqs.cfm?fid=7402

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有