ReadPrinter function always return false in c# RRS feed

  • Question

  • Please look into the code and let me know why always the read printer method is returning false ? do i need to call writeprinter first and then readprinter ? i just want to get printer status by sending a ZPL command and then receiving the response. 

    Most likely i may be giving it the wrong parameters but can some tell me how to implement it ? reference to the function im trying to use is as below:

    this is how im doing it (Please correct me if im doing something wrong:

            [DllImport("winspool.Drv", EntryPoint = "ReadPrinter", SetLastError = true, ExactSpelling = true, CallingConvention =                     CallingConvention.StdCall)]
            public static extern Boolean ReadPrinter(IntPtr hPrinter, out StringBuilder data, Int32 cbBuf, out IntPtr pNoBytesRead);

             public static Boolean ReadBytesFromPrinter(String szPrinterName, string command)
                Int32 dwError = 0; IntPtr dwWritten;
                IntPtr hPrinter = new IntPtr(0);
                DOCINFOA di = new DOCINFOA();
                Boolean bSuccess = false; // Assume failure unless you specifically succeed.
                di.pDocName = "SendBytesToPrinter";
                di.pDataType = "RAW";

                // Open the printer.
                if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
                    // Start a document.
                    if (StartDocPrinter(hPrinter, 1, di))
                        // Start a page.
                        if (StartPagePrinter(hPrinter))
                            Int32 maxRead = 256;
                            StringBuilder sbData = new StringBuilder(maxRead);
                            //Read Data      
                            bSuccess = 
                            bSuccess = ReadPrinter(hPrinter,out  command, maxRead, out dwWritten);
                            data = sbData.ToString();




                // If you did not succeed, GetLastError may give more information
                // about why not.
                if (bSuccess == false)
                    dwError = Marshal.GetLastWin32Error();
                    data = null;

                return bSuccess;

    Friday, August 5, 2016 9:19 AM


  • Hi KhawajaHassan,

    Please try to use the following code and check if it works for you

    [DllImport("winspool.Drv", EntryPoint = "ReadPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
         static extern bool ReadPrinter(IntPtr hPrinter, IntPtr pBuf, int cbBuf, out int pNoBytesRead);

    For more information, please refer to:

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites;Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Cole Wu

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 8, 2016 8:33 AM