CreateProcessAsUser を使用してサービスからプロセスを実行し、stdout を名前付きパイプにリダイレクトしています。しかし、名前付きパイプから読み取ろうとすると、読み取りでハングし、戻りません。これを別のタスク(現在は以下で実装されている)で試し、メインプロセスで読んだだけです。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 bytesRead, IntPtr.Zero);
if (!retval || bytesRead == 0)
{
log.InfoFormat("finalread {0} {1}", retval, bytesRead);
break;
}
log.InfoFormat("after read {0} {1}", retval, bytesRead);
}
log.InfoFormat("endingtask");
});
var processInfo = new Native.ProcessInformation();
var startInfo = new Native.StartupInfo();
startInfo.Size = Marshal.SizeOf(startInfo);
//startInfo.StdOutput = pipeServer.SafePipeHandle.DangerousGetHandle();
startInfo.StdOutput = subProcessPipe;
log.InfoFormat("calling createprocess");
if (!Native.CreateProcessAsUser(
UserTokenHandle,
null,
command,
IntPtr.Zero,
IntPtr.Zero,
false,
0x00000020 | 0x00000010 | 0x00000400,
IntPtr.Zero,
null,
ref startInfo,
out processInfo))
{
throw new Exception("Create process didn't work : " + Marshal.GetLastWin32Error());
}
log.InfoFormat("process id = {0}", processInfo.ProcessId);
int processId = processInfo.ProcessId;
using (var process = Process.GetProcessById(processId))
{
var pid = process.Id;
log.Info("process started : " + pid);
process.WaitForExit(ProcessTimeoutIteration);
log.Info("process Completed");
readPipeTask.Wait();
log.Info("process Completedafter wait");
}
////log.Info("reading pipe canread = " + pipeServer.CanRead);
////log.Info("reading pipe isconnected = " + pipeServer.IsConnected);
var standardOutput = new StringBuilder(InitialBufferSize);
//////var readBuff = new byte[BufferSize];
//var ac = new AsyncCallback(CallBackMethod);
////var bytesRead = pipeServer.Read(readBuff, 0, BufferSize);
log.Info("before first read");
////while (bytesRead > 0)
////{
//// //fileStream.Write(readBuff, 0, bytesRead);
//// standardOutput.Append(Encoding.Unicode.GetString(readBuff));
//// bytesRead = pipeServer.Read(readBuff, 0, BufferSize);
////}