/// <summary>
        /// Initializes a new instance of the <see cref="PSFilterShimService"/> class.
        /// </summary>
        /// <param name="abort">The abort callback.</param>
        /// <param name="plugin">The plug-in data.</param>
        /// <param name="settings">The settings for the shim application.</param>
        /// <param name="error">The error callback.</param>
        /// <param name="progress">The progress callback.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="plugin"/> is null.
        /// or
        /// <paramref name="settings"/> is null.
        /// </exception>
        public PSFilterShimPipeServer(Func <bool> abort,
                                      PluginData plugin,
                                      PSFilterShimSettings settings,
                                      Action <string> error,
                                      Action <byte> progress)
        {
            PipeName         = "PSFilterShim_" + Guid.NewGuid().ToString();
            abortFunc        = abort;
            pluginData       = plugin ?? throw new ArgumentNullException(nameof(plugin));
            this.settings    = settings ?? throw new ArgumentNullException(nameof(settings));
            errorCallback    = error;
            progressCallback = progress;
            // One byte for the command index and one byte for the payload.
            oneByteParameterMessageBuffer = new byte[2];
            // 4 bytes for the payload length and one byte for the payload.
            oneByteParameterReplyBuffer = new byte[5];
            Array.Copy(BitConverter.GetBytes(sizeof(byte)), oneByteParameterReplyBuffer, sizeof(int));
            replySizeBuffer = new byte[sizeof(int)];

            server = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
            server.BeginWaitForConnection(WaitForConnectionCallback, null);
        }
        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);
            }
        }