Delphi - Capture stdout and stderr output from statically linked MSVC++ compiled DLL

落爺英雄遲暮 提交于 2019-12-07 05:18:57

问题


I have been trying to capture stdout and stderr output from a DLL compiled in MSVC++ that my Delphi app statically links to, but so far have been unsuccessful.

procedure Test;
var
  fs: TFileStream;

begin
  fs := TFileStream.Create('C:\temp\output.log', fmCreate or fmShareDenyWrite);
  SetStdHandle(STD_OUTPUT_HANDLE, fs.Handle);
  SetStdHandle(STD_ERROR_HANDLE, fs.Handle);

  dllFunc(0); // Writes to stdout in MSVC++ console app, but not here
  // fs.Length is always zero

  fs.Free;
end;

Thought I was on the right track, but it does not work.

  1. Is SetStdHandle() enough?
  2. Is TFileStream the right thing to use here?
  3. Am I using TFileStream properly for SetStdHandle()?
  4. Is it possible that the DLL sets its stdout/stderr handles when the app loads? If so, where is the best place to use SetStdHandle() or equivalent?

Any help would be appreciated.


回答1:


If the DLL grabs the stdout handles when it is loaded, then you will need to dynamically load the DLL after you have changed the stdout handles in your code.




回答2:


If your app is a console app, you could just run the thing and capture everything to stdout with redirection. i.e.

C:\MyAppWhichCallsDll.exe > c:\temp\output.log


来源:https://stackoverflow.com/questions/2639144/delphi-capture-stdout-and-stderr-output-from-statically-linked-msvc-compiled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!