private void Run32BitFilterProxy(ref PSFilterPdnConfigToken token, IWin32Window window) { // Check that PSFilterShim exists first thing and abort if it does not. string shimPath = Path.Combine(Path.GetDirectoryName(typeof(PSFilterPdnEffect).Assembly.Location), "PSFilterShim.exe"); if (!File.Exists(shimPath)) { ShowErrorMessage(window, Resources.PSFilterShimNotFound); return; } try { using (PSFilterShimDataFolder proxyTempDir = new PSFilterShimDataFolder()) { string srcFileName = proxyTempDir.GetRandomFilePathWithExtension(".psi"); string destFileName = proxyTempDir.GetRandomFilePathWithExtension(".psi"); string parameterDataFileName = proxyTempDir.GetRandomFilePathWithExtension(".dat"); string resourceDataFileName = proxyTempDir.GetRandomFilePathWithExtension(".dat"); string descriptorRegistryFileName = proxyTempDir.GetRandomFilePathWithExtension(".dat"); string regionFileName = string.Empty; Rectangle sourceBounds = EnvironmentParameters.SourceSurface.Bounds; Rectangle selection = EnvironmentParameters.GetSelection(sourceBounds).GetBoundsInt(); if (selection != sourceBounds) { regionFileName = proxyTempDir.GetRandomFilePathWithExtension(".dat"); RegionDataWrapper selectedRegion = new RegionDataWrapper(EnvironmentParameters.GetSelection(sourceBounds).GetRegionData()); DataContractSerializerUtil.Serialize(regionFileName, selectedRegion); } bool proxyResult = true; string proxyErrorMessage = string.Empty; PSFilterShimSettings settings = new PSFilterShimSettings { RepeatEffect = true, ShowAboutDialog = false, SourceImagePath = srcFileName, DestinationImagePath = destFileName, ParentWindowHandle = window.Handle, PrimaryColor = EnvironmentParameters.PrimaryColor.ToColor(), SecondaryColor = EnvironmentParameters.SecondaryColor.ToColor(), RegionDataPath = regionFileName, ParameterDataPath = parameterDataFileName, PseudoResourcePath = resourceDataFileName, DescriptorRegistryPath = descriptorRegistryFileName, PluginUISettings = null }; using (PSFilterShimPipeServer server = new PSFilterShimPipeServer(AbortCallback, token.FilterData, settings, delegate(string data) { proxyResult = false; proxyErrorMessage = data; }, null)) { PSFilterShimImage.Save(srcFileName, EnvironmentParameters.SourceSurface, 96.0f, 96.0f); ParameterData parameterData; if (token.FilterParameters.TryGetValue(token.FilterData, out parameterData)) { DataContractSerializerUtil.Serialize(parameterDataFileName, parameterData); } if (token.PseudoResources.Count > 0) { DataContractSerializerUtil.Serialize(resourceDataFileName, token.PseudoResources); } if (token.DescriptorRegistry != null) { DataContractSerializerUtil.Serialize(descriptorRegistryFileName, token.DescriptorRegistry); } ProcessStartInfo psi = new ProcessStartInfo(shimPath, server.PipeName); using (Process proxy = Process.Start(psi)) { proxy.WaitForExit(); } } if (proxyResult && File.Exists(destFileName)) { token.Dest = PSFilterShimImage.Load(destFileName); } else if (!string.IsNullOrEmpty(proxyErrorMessage)) { ShowErrorMessage(window, proxyErrorMessage); } } } catch (ArgumentException ax) { ShowErrorMessage(window, ax.Message); } catch (IOException ex) { ShowErrorMessage(window, ex.Message); } catch (NotSupportedException ex) { ShowErrorMessage(window, ex.Message); } catch (UnauthorizedAccessException ex) { ShowErrorMessage(window, ex.Message); } catch (Win32Exception wx) { ShowErrorMessage(window, wx.Message); } }
private void WaitForConnectionCallback(IAsyncResult result) { if (server is null) { return; } try { server.EndWaitForConnection(result); } catch (ObjectDisposedException) { return; } server.ProperRead(replySizeBuffer, 0, replySizeBuffer.Length); int messageLength = BitConverter.ToInt32(replySizeBuffer, 0); byte[] messageBytes; if (messageLength <= oneByteParameterMessageBuffer.Length) { messageBytes = oneByteParameterMessageBuffer; } else { messageBytes = new byte[messageLength]; } server.ProperRead(messageBytes, 0, messageLength); Command command = (Command)messageBytes[0]; switch (command) { case Command.AbortCallback: SendReplyToClient((byte)(abortFunc() ? 1 : 0)); break; case Command.ReportProgress: progressCallback(messageBytes[1]); SendEmptyReplyToClient(); break; case Command.GetPluginData: using (MemoryStream stream = new MemoryStream()) { DataContractSerializerUtil.Serialize(stream, pluginData); SendReplyToClient(stream.GetBuffer(), 0, (int)stream.Length); } break; case Command.GetSettings: using (MemoryStream stream = new MemoryStream()) { DataContractSerializerUtil.Serialize(stream, settings); SendReplyToClient(stream.GetBuffer(), 0, (int)stream.Length); } break; case Command.SetErrorMessage: errorCallback(Encoding.UTF8.GetString(messageBytes, 1, messageLength - 1)); SendEmptyReplyToClient(); break; default: throw new InvalidOperationException($"Unknown command value: { command }."); } server.WaitForPipeDrain(); // Start a new server and wait for the next connection. server.Dispose(); server = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); server.BeginWaitForConnection(WaitForConnectionCallback, null); }