Print Spool Monitoring

0

I'm trying to make an application that monitors my printer and when the user sends an impression, of whatever program, it captures that information, pauses the printing and opens a new window for the user to enter a code. If the code is correct it continues printing. Otherwise, the printout is canceled.

At the moment I'm just trying to get the application to pause printing. I can have it monitor the spool and return the submitted job information. I have tried to use InvokeMethod ("Pause") but it only appears that there is no such option.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace Listar_Servicos
{
    public partial class frmMonitor : Form
    {
        public frmMonitor()
        {
            InitializeComponent();
        }

        private void frmMonitor_Load(object sender, EventArgs e)
        {
            EventWatch("localhost");
        }

        private ManagementEventWatcher manEWatch;

            public void EventWatch(string host)
            {

                ManagementScope oMs = new ManagementScope(@"\" + host + @"\root\cimv2");
                oMs.Connect();
                manEWatch = new ManagementEventWatcher(oMs, new EventQuery("SELECT * FROM    __InstanceCreationEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_PrintJob'"));
                manEWatch.EventArrived += new EventArrivedEventHandler(mewPrintJobs_EventArrived);
                manEWatch.Start();
            }

            static void mewPrintJobs_EventArrived(object sender, EventArrivedEventArgs e)
            {
                foreach (PropertyData prop in e.NewEvent.Properties)
                {
                    string val = prop.Value == null ? "null" : prop.Value.ToString();

                }

                ManagementBaseObject printJob = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
                string v = "";

                foreach (PropertyData propp in printJob.Properties)
                {

                    string name = propp.Name;
                    string val = propp.Value == null ? "null" : propp.Value.ToString();
                    val += "\n";
                    v += name + ":" + val;

                }
                MessageBox.Show(v);
            }

        private void btnNovoMonitor_Click(object sender, EventArgs e)
        {
            frmMonitor2 novoMonitor = new frmMonitor2();
            novoMonitor.Show();
        }
    }
}

Could anyone help me?

    
asked by anonymous 13.06.2018 / 21:16

1 answer

1

Really a solution to your problem would be to use what Marcelo Uchima put in the comments.

However, this form of programming, according to Microsoft itself:

  

To maintain type security and security, C # does not support   pointer arithmetic by default. However, using the keyword   unsafe, you can define an unsafe context in which the pointers   can be used.

Also according to them, these are some of the characteristics of using unsafe code:

  
  • In some cases, unsafe code can increase the performance of an application by removing matrix boundary checks.
  •   
  • Using unsafe code presents security and stability risks.
  •   

Depending on what you're going to do, despite the security risks, if you want a higher performance you can use this method.

If you do not have major performance issues, using even event monitoring is the best option.

In this code, you can not perform Invoke on the method because your foreach is fetching the properties of the query information. Create only one foreach as follows:

foreach (ManagementObject prntJob in prntJobCollection)

And you will be able to use prntJob.InvokeMethod("Pause", null); (syntax similar to continue, changing only from Pause to Resume ).

    
05.07.2018 / 22:25