C#中屏蔽第三方DLL中的Console输出的方法

 

//1.引入

private const Int32 STD_OUTPUT_HANDLE = -11;
[DllImport("Kernel32.dll")]
extern static Boolean SetStdHandle(Int32 nStdHandle, SafeHandleZeroOrMinusOneIsInvalid handle);
[DllImport("Kernel32.dll")]
extern static SafePipeHandle GetStdHandle(Int32 nStdHandle);

//2.操作

//屏蔽dll中的输出
//2.1 获取输出句柄
var defaultHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//2.2 设置输出到匿名管道
SetStdHandle(STD_OUTPUT_HANDLE, new AnonymousPipeServerStream(PipeDirection.Out).SafePipeHandle);
//2.3 调用第三方库(内含Console的输出)
PaddleOCRSharp.PaddleOCREngine engine = new PaddleOCRSharp.PaddleOCREngine(config);
//2.4 恢复
SetStdHandle(STD_OUTPUT_HANDLE,defaultHandle);

  

posted @ 2024-03-06 09:43  chyshx  阅读(83)  评论(0编辑  收藏  举报