How I monitor the sensors of a C#printer

Asked

Viewed 19 times

0

I have a hybrid printer authentication and printing quite old by the way, what happens is that in the authentication part has a sensor that the document is present, what I would like to know is how do I receive the status of this sensor for my application. I’ve searched a lot on the Internet and I haven’t found anything related that would return a value or anything like.

I have a code already ready that performs some functions like restart and print.

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public partial class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
    }

    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, ref IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, int level, [In()][MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, ref int dwWritten);

    [DllImport("winspool.Drv", EntryPoint = "AddMonitor", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool AddMonitor(IntPtr hPrinter);

    //[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    //public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, ref int dwWritten);

    // Check for possible trouble states of a printer using the flags of the QueueStatus property



    private IntPtr hPrinter = new IntPtr(0);
    private DOCINFOA di = new DOCINFOA();
    private bool PrinterOpen = false;

    public bool PrinterIsOpen
    {
        get
        {
            bool PrinterIsOpenRet = default;
            PrinterIsOpenRet = PrinterOpen;
            return PrinterIsOpenRet;
        }
    }

    public bool OpenPrint(string szPrinterName)
    {
        bool OpenPrintRet = default;
        if (PrinterOpen == false)
        {
            di.pDocName = ".NET RAW Document";
            di.pDataType = "RAW";
            if (OpenPrinter(szPrinterName.Normalize(), ref hPrinter, IntPtr.Zero))
            {
                // Start a document.
                if (StartDocPrinter(hPrinter, 1, di))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        PrinterOpen = true;
                    }
                }
            }
        }

        OpenPrintRet = PrinterOpen;
        return OpenPrintRet;
    }

    public void ClosePrint()
    {
        if (PrinterOpen)
        {
            EndPagePrinter(hPrinter);
            EndDocPrinter(hPrinter);
            ClosePrinter(hPrinter);
            PrinterOpen = false;
        }
    }

    public bool SendStringToPrinter(string szPrinterName, string szString)
    {
        bool SendStringToPrinterRet = default;
        if (PrinterOpen)
        {
            IntPtr pBytes;
            int dwCount;
            int dwWritten = 0;
            dwCount = szString.Length;
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            SendStringToPrinterRet = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
            if (AddMonitor(hPrinter))
                Marshal.FreeCoTaskMem(pBytes);
        }
        else
        {
            SendStringToPrinterRet = false;
        }
        return SendStringToPrinterRet;
    }
}

I tried to implement a function based on my internet searches that would be Addmonitor but without success.

The chunk that sends the data to the printer is this

            SendStringToPrinterRet = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.