/// <summary>
        /// This takes the PrintWorkflowForegroundSetupRequestedEventArgs and gets the IPrinterExtensionContextNative from it,
        /// via the PrintWorkflowConfiguration property.
        /// </summary>
        /// <param name="args">IActivatedEventArgs passed to OnActivated()</param>
        /// <returns>Pointer to interface as IntPtr</returns>
        private static IntPtr GetPrintWorkflowConfigurationNativeAsIntPtr(PrintWorkflowForegroundSetupRequestedEventArgs args)
        {
            Object printerExtensionContextNative = args.Configuration;

            IntPtr ptr = IntPtr.Zero;
            Guid   iidIPrinterExtensionContextNative = new Guid("C056BE0A-9EE2-450A-9823-964F0006F2BB");
            int    hr = Marshal.QueryInterface(Marshal.GetIUnknownForObject(printerExtensionContextNative), ref iidIPrinterExtensionContextNative, out ptr);

            if (hr < 0)
            {
                throw new InvalidCastException(iidIPrinterExtensionContextNative.ToString());
            }

            if (ptr != IntPtr.Zero)
            {
                // This is good
            }
            else
            {
                throw new InvalidCastException(iidIPrinterExtensionContextNative.ToString());
            }

            return(ptr);
        }
        /// <summary>
        /// Handle the Print Task Setup Event
        /// </summary>
        /// <param name="sessionManager">Session manager</param>
        /// <param name="printTaskSetupArgs">Has the Configuration and Controller</param>
        internal void OnSetupRequested(PrintWorkflowForegroundSession sessionManager, PrintWorkflowForegroundSetupRequestedEventArgs printTaskSetupArgs)
        {
            // If anything asynchronous is going to be done, you need to take out a deferral here,
            // since otherwise the next callback happens once this one exits, which may be premature
            Deferral setupRequestedDeferral = printTaskSetupArgs.GetDeferral();

            // Get information about the source application, print job title, and session ID
            string sourceApplicationName = printTaskSetupArgs.Configuration.SourceAppDisplayName;
            string jobTitle  = printTaskSetupArgs.Configuration.JobTitle;
            string sessionId = printTaskSetupArgs.Configuration.SessionId;
            string localStorageVariablePrefix = string.Format("{0}::", sessionId);

            localStorage.SetStorageKeyPrefix(localStorageVariablePrefix);

            // Get the PrinterExtensionContextNative from the activation arguments
            IntPtr ptr = GetPrintWorkflowConfigurationNativeAsIntPtr(printTaskSetupArgs);

            // Create the Print Helper
            PrintHelperClass printHelper = new PrintHelperClass(InitializationType.PrinterExtensionContextNativeType, (ulong)ptr);

            printerName          = printHelper.GetPrinterName();
            WorkflowHeadingLabel = string.Format(workflowHeadingFormat, sourceApplicationName, printerName);

            // Add callback handler on main page
            try
            {
                printHelper.OnInkLevelReceived += OnQueryResultReceived;
                // Send the BiDi query
                printHelper.SendInkLevelQuery();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                Debug.WriteLine(errorMessage);
            }
            finally
            {
                // Complete the deferral taken out at the start of OnSetupRequested
                setupRequestedDeferral.Complete();
            }
        }