Example #1
0
        private static void MonitorPrinter(string printerName, NotifyOptions printerNotifyOptions, PRINTER_CHANGE change)
        {
            if (printerNotifyOptions == null && change == 0)
            {
                throw new Exception("Either or both of printer changes or fields to monitor must be set.");
            }

            using var printerChangeNotification = ChangeNotification.Create(change,
                                                                            printerName,
                                                                            PRINTER_NOTIFY_CATEGORY.PRINTER_NOTIFY_CATEGORY_ALL, printerNotifyOptions);

            while (true)
            {
                printerChangeNotification.WaitHandle.WaitOne();
                NotifyInfo printerNotifyInfo;
                bool       refresh = false;

                do
                {
                    printerNotifyInfo = printerChangeNotification.FindNextPrinterChangeNotification(refresh);
                    WriteToConsole(printerNotifyInfo);

                    refresh = true; // For next iteration if data overflowed
                } while ((printerNotifyInfo.Flags & NotifyInfo.PRINTER_NOTIFY_INFO_DISCARDED) != 0);
            }
        }
        public void MultipleFields()
        {
            // Use a random comment then we don't have t worry about it being set from a previous run.
            var  randomComment = DateTime.Now.Ticks.ToString();
            uint priority;

            var options = new NotifyOptions
            {
                Types = new List <NotifyOptionsType>
                {
                    new NotifyOptionsType()
                    {
                        Fields = new List <ushort>
                        {
                            (ushort)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_COMMENT,
                            (ushort)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_LOCATION,
                            (ushort)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_PRIORITY,
                        },
                        Type = NOTIFY_TYPE.PRINTER_NOTIFY_TYPE,
                    }
                }
            };

            using var changeNotification = ChangeNotification.Create(0, NameConstants.PrinterName, PRINTER_NOTIFY_CATEGORY.PRINTER_NOTIFY_CATEGORY_ALL, options);

            // If we don't close the printer handle before calling WaitOne then WaitOne can block indefinitely
            using (var printer = new SafePrinter(NameConstants.PrinterName))
            {
                // This should trigger a change
                var pi2 = printer.GetPrinter();
                pi2.pComment  = randomComment + "Comment";
                pi2.pLocation = randomComment + "Location";
                priority      = ++pi2.Priority & 0xFF;
                printer.SetPrinter(pi2, 0);
            }

            changeNotification.WaitHandle.WaitOne();

            var change = changeNotification.FindNextPrinterChangeNotification(true);

            Assert.That(change.Change == 0); // We didn't request change monitoring

            Assert.That(change.Data.Count, Is.EqualTo(3));

            var comment = change.Data.First(d => d.Field == (int)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_COMMENT).Value;

            Assert.That(comment, Is.EqualTo(randomComment + "Comment"));

            var location = change.Data.First(d => d.Field == (int)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_LOCATION).Value;

            Assert.That(location, Is.EqualTo(randomComment + "Location"));

            var newPriority = change.Data.First(d => d.Field == (int)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_PRIORITY).Value;

            Assert.That(newPriority, Is.EqualTo(priority));
        }
Example #3
0
        public void AddJob()
        {
            var options = new NotifyOptions
            {
                Types = new List <NotifyOptionsType>
                {
                    new NotifyOptionsType
                    {
                        Fields = new List <ushort> {
                            (ushort)JOB_NOTIFY_FIELD.JOB_NOTIFY_FIELD_PRINTER_NAME
                        },
                        Type = NOTIFY_TYPE.JOB_NOTIFY_TYPE,
                    }
                }
            };

            using var changeNotification = ChangeNotification.Create(0, NameConstants.PrinterName, PRINTER_NOTIFY_CATEGORY.PRINTER_NOTIFY_CATEGORY_ALL, options);

            using var localPrintServer = new LocalPrintServer();
            var defaultPrintQueue = localPrintServer.GetPrintQueue(NameConstants.PrinterName);

            var myPrintJob = defaultPrintQueue.AddJob();

            // Write a Byte buffer to the JobStream and close the stream
            var myByteBuffer = Encoding.Unicode.GetBytes("This is a test string for the print job stream.");

            using var myStream = myPrintJob.JobStream;
            myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
            myStream.Close();

            using var myStream2 = myPrintJob.JobStream;
            myStream2.Write(myByteBuffer, 0, myByteBuffer.Length);
            myStream2.Close();

            changeNotification.WaitHandle.WaitOne();

            var changes = changeNotification.FindNextPrinterChangeNotification(false);

            Assert.That(changes.Data.Count, Is.EqualTo(1));
            Assert.That(changes.Data.First().Value, Is.EqualTo(NameConstants.PrinterName));
        }
        public void Comment()
        {
            // Use a random comment then we don't have t worry about it being set from a previous run.
            var randomComment = DateTime.Now.Ticks.ToString();

            var options = new NotifyOptions
            {
                Types = new List <NotifyOptionsType>
                {
                    new NotifyOptionsType()
                    {
                        Fields = new List <ushort> {
                            (ushort)PRINTER_NOTIFY_FIELD.PRINTER_NOTIFY_FIELD_COMMENT
                        },
                        Type = NOTIFY_TYPE.PRINTER_NOTIFY_TYPE,
                    }
                }
            };

            using var changeNotification = ChangeNotification.Create(0, NameConstants.PrinterName, PRINTER_NOTIFY_CATEGORY.PRINTER_NOTIFY_CATEGORY_ALL, options);

            // If we don't close the printer handle before calling WaitOne then WaitOne can block indefinitely
            using (var printer = new SafePrinter(NameConstants.PrinterName))
            {
                // This should trigger a change
                var pi2 = printer.GetPrinter();
                pi2.pComment            = randomComment;
                pi2.pSecurityDescriptor = IntPtr.Zero;
                printer.SetPrinter(pi2, 0);
            }

            changeNotification.WaitHandle.WaitOne();

            var change = changeNotification.FindNextPrinterChangeNotification(false);

            Assert.That(change.Change == 0); // We didn't request change monitoring

            Assert.That(change.Data.Count, Is.EqualTo(1));
            Assert.That(change.Data[0].Value, Is.EqualTo(randomComment));
        }
Example #5
0
        public void SetPrinter()
        {
            using var changeNotification = ChangeNotification.Create(PRINTER_CHANGE.PRINTER_CHANGE_PRINTER, NameConstants.PrinterName, PRINTER_NOTIFY_CATEGORY.PRINTER_NOTIFY_CATEGORY_ALL, null);

            // Make sure changeNotification isn't signaled before we start
            Assert.That(changeNotification.WaitHandle.WaitOne(0), Is.False);

            // If we don't close the printer handle before calling WaitOne then WaitOne can block indefinitely
            using (var printer = new SafePrinter(NameConstants.PrinterName))
            {
                // This should trigger a change
                var pi2 = printer.GetPrinter();
                pi2.pComment = "A printer comment";
                printer.SetPrinter(pi2, 0);
            }

            changeNotification.WaitHandle.WaitOne();

            var change = changeNotification.FindNextPrinterChangeNotification(false);

            Assert.That(change.Change == PRINTER_CHANGE.PRINTER_CHANGE_SET_PRINTER);
        }