internal static void WriteDtoToFile(NauDto dto, string fileName) { using (var fileStream = new FileStream(fileName, FileMode.Create)) { new BinaryFormatter().Serialize(fileStream, dto); fileStream.Flush(); } }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; using (var clientPipeHandle = CreateNamedPipe( GetPipeName(syncProcessName), WRITE_ONLY | FILE_FLAG_OVERLAPPED, 0, 1, // 1 max instance (only the updater utility is expected to connect) BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero)) { //failed to create named pipe if (clientPipeHandle.IsInvalid) return null; try { p = Process.Start(processStartInfo); } catch (Win32Exception) { // Person denied UAC escallation return null; } while (true) { var success = 0; try { success = ConnectNamedPipe( clientPipeHandle, IntPtr.Zero); } catch { } //failed to connect client pipe if (success == 0) // TODO Log error break; //client connection successfull using (var fStream = new FileStream(clientPipeHandle, FileAccess.Write, (int)BUFFER_SIZE, true)) { new BinaryFormatter().Serialize(fStream, dto); fStream.Flush(); fStream.Close(); } } } return p; }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; State state = new State(); using (state.clientPipeHandle = CreateNamedPipe( GetPipeName(syncProcessName), WRITE_ONLY | FILE_FLAG_OVERLAPPED, 0, 1, // 1 max instance (only the updater utility is expected to connect) BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero)) { //failed to create named pipe if (state.clientPipeHandle.IsInvalid) { return(null); } try { p = Process.Start(processStartInfo); } catch (Win32Exception) { // Person denied UAC escallation return(null); } ThreadPool.QueueUserWorkItem(ConnectPipe, state); //A rather arbitary five seconds, perhaps better to be user configurable at some point? state.eventWaitHandle.WaitOne(10000); //failed to connect client pipe if (state.result == 0) { return(null); } //client connection successfull using (var fStream = new FileStream(state.clientPipeHandle, FileAccess.Write, (int)BUFFER_SIZE, true)) { new BinaryFormatter().Serialize(fStream, dto); fStream.Flush(); fStream.Close(); } } return(p); }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; var paramsFileName = GetAdditionalParamsFileName(syncProcessName); try { WriteDtoToFile(dto, paramsFileName); } catch (Exception e) { Console.WriteLine("Writing params to temporary path failed: '{0}'", e); if (File.Exists(paramsFileName)) { File.Delete(paramsFileName); } return(null); } if (!File.Exists(paramsFileName)) { Console.WriteLine("Writing params to temporary path failed"); return(null); } try { p = ExtendendStartProcess.Start(processStartInfo); } catch (Win32Exception e) { // Person denied UAC escallation Console.WriteLine("User denied UAC escallation? Error while launching process: {0}.", e); return(null); } for (int i = 0; i < 15; i++) { if (!File.Exists(paramsFileName)) { return(p); } Thread.Sleep(1000); } File.Delete(paramsFileName); return(null); }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; State state = new State(); using (state.clientPipeHandle = CreateNamedPipe( GetPipeName(syncProcessName), WRITE_ONLY | FILE_FLAG_OVERLAPPED, 0, 1, // 1 max instance (only the updater utility is expected to connect) BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero)) { //failed to create named pipe if (state.clientPipeHandle.IsInvalid) return null; try { p = Process.Start(processStartInfo); } catch (Win32Exception) { // Person denied UAC escallation return null; } ThreadPool.QueueUserWorkItem(ConnectPipe, state); //A rather arbitary five seconds, perhaps better to be user configurable at some point? int millisecondsTimeout = 120 * 1000; // was 10000. Jared state.eventWaitHandle.WaitOne(millisecondsTimeout); //failed to connect client pipe if (state.result == 0) return null; //client connection successfull using (var fStream = new FileStream(state.clientPipeHandle, FileAccess.Write, (int)BUFFER_SIZE, true)) { new BinaryFormatter().Serialize(fStream, dto); fStream.Flush(); fStream.Close(); } } return p; }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; State state = new State(); using (state.clientPipeHandle = CreateNamedPipe( GetPipeName(syncProcessName), WRITE_ONLY | FILE_FLAG_OVERLAPPED, 0, 1, // 1 max instance (only the updater utility is expected to connect) BUFFER_SIZE, BUFFER_SIZE, 0, IntPtr.Zero)) { //failed to create named pipe if (state.clientPipeHandle.IsInvalid) { throw new Exception("Launch process client: Failed to create named pipe, handle is invalid."); } // This will throw Win32Exception if the user denies UAC p = Process.Start(processStartInfo); ThreadPool.QueueUserWorkItem(ConnectPipe, state); //A rather arbitary five seconds, perhaps better to be user configurable at some point? state.eventWaitHandle.WaitOne(10000); //failed to connect client pipe if (state.result == 0) { throw new Exception("Launch process client: Failed to connect to named pipe"); } //client connection successfull using (var fStream = new FileStream(state.clientPipeHandle, FileAccess.Write, (int)BUFFER_SIZE, true)) { new BinaryFormatter().Serialize(fStream, dto); fStream.Flush(); fStream.Close(); } } return p; }
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; var paramsFileName = GetAdditionalParamsFileName(syncProcessName); try { WriteDtoToFile(dto, paramsFileName); } catch (Exception e) { Console.WriteLine("Writing params to temporary path failed: '{0}'", e); if (File.Exists(paramsFileName)) File.Delete(paramsFileName); return null; } if (!File.Exists(paramsFileName)) { Console.WriteLine("Writing params to temporary path failed"); return null; } try { p = ExtendendStartProcess.Start(processStartInfo); } catch (Win32Exception e) { // Person denied UAC escallation Console.WriteLine("User denied UAC escallation? Error while launching process: {0}.", e); return null; } for (int i = 0; i < 15; i++) { if (!File.Exists(paramsFileName)) return p; Thread.Sleep(1000); } File.Delete(paramsFileName); return null; }
/// <summary> /// Launches the specifies process and sends the dto object to it using a named pipe /// </summary> /// <param name="dto">Dto object to send</param> /// <param name="processStartInfo">Process info for the process to start</param> /// <param name="syncProcessName">Name of the pipe to write to</param> /// <returns>The started process</returns> public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; using (var pipe = new NamedPipeServerStream(syncProcessName, PipeDirection.Out, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous)) { p = Process.Start(processStartInfo); if (p == null) { throw new ProcessStartFailedException("The process failed to start"); } var asyncResult = pipe.BeginWaitForConnection(null, null); if (asyncResult.AsyncWaitHandle.WaitOne(PIPE_TIMEOUT)) { pipe.EndWaitForConnection(asyncResult); var formatter = new BinaryFormatter(); formatter.Serialize(pipe, dto); } else if (p.HasExited) { var exceptionType = Marshal.GetExceptionForHR(p.ExitCode).GetType(); throw new TimeoutException(string.Format( "The NamedPipeServerStream timed out waiting for a named pipe connection, " + "but the process has exited with exit code: {0} ({1})", p.ExitCode, exceptionType.FullName)); } else { //throw new TimeoutException("The NamedPipeServerStream timed out waiting for a named pipe connection."); } } return(p); }
/// <summary> /// Launches the specifies process and sends the dto object to it using a named pipe /// </summary> /// <param name="dto">Dto object to send</param> /// <param name="processStartInfo">Process info for the process to start</param> /// <param name="syncProcessName">Name of the pipe to write to</param> /// <returns>The started process</returns> public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName) { Process p; using (NamedPipeServerStream pipe = new NamedPipeServerStream(syncProcessName, PipeDirection.Out, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous)) { p = Process.Start(processStartInfo); if (p == null) { throw new ProcessStartFailedException("The process failed to start"); } var asyncResult = pipe.BeginWaitForConnection(null, null); if (asyncResult.AsyncWaitHandle.WaitOne(PIPE_TIMEOUT)) { pipe.EndWaitForConnection(asyncResult); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(pipe, dto); } else if (p.HasExited) { Type exceptionType = Marshal.GetExceptionForHR(p.ExitCode).GetType(); throw new TimeoutException(string.Format("The NamedPipeServerStream timed out waiting for a named pipe connection, " + "but the process has exited with exit code: {0} ({1})", p.ExitCode, exceptionType.FullName)); } else { throw new TimeoutException("The NamedPipeServerStream timed out waiting for a named pipe connection."); } } return p; }
internal static void WriteDtoToFile(NauDto dto, string fileName) { using (var fileStream = new FileStream(fileName, FileMode.Create)) { new BinaryFormatter().Serialize(fileStream, dto); fileStream.Flush(); } }