小编ee4*_*582的帖子

将 createprocessasuser 的 stdout 重定向到管道在读取时挂起

我正在使用 CreateProcessAsUser 从服务运行进程,并将标准输出重定向到命名管道。但是当我尝试从命名管道中读取时,它会挂在读取上并且永远不会返回。我在一个单独的任务中尝试了这个(目前在下面实现)并且只是在主进程中读取它。我还尝试使用 C# AnonymousPipeServerStream 类(请参阅下面注释掉的代码),但还是挂起。

任何想法我缺少什么?谢谢!

            Native.SECURITY_ATTRIBUTES sa = new Native.SECURITY_ATTRIBUTES();
            processAttributes.nLength = Marshal.SizeOf(sa);
            processAttributes.lpSecurityDescriptor = IntPtr.Zero;
            processAttributes.bInheritHandle = 0;

            //AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
            IntPtr readPipe = IntPtr.Zero;
            IntPtr subProcessPipe = IntPtr.Zero;
            if(!Native.CreatePipe(out readPipe, out subProcessPipe,ref sa, 0x0001))
            {
                throw new Exception("createpipe gave error: " + Marshal.GetLastWin32Error());
            }

            log.InfoFormat("pipe created");

            Task readPipeTask = Task.Factory.StartNew(() =>
            {
                log.InfoFormat("startingtask");
                var readBuff = new byte[BufferSize];
                uint bytesRead = 0;
                bool retval;
                while (true)
                {
                    retval = Native.ReadFile(readPipe, readBuff, BufferSize, out …
Run Code Online (Sandbox Code Playgroud)

c# pipe createprocessasuser

5
推荐指数
1
解决办法
534
查看次数

标签 统计

c# ×1

createprocessasuser ×1

pipe ×1