The printer prints a test label printout but does not print a printout in the custom application

C Cenk Yenikoylu 1 year 6 months ago
153 3 0

Hello everyone,

A WinForm application I developed does not print labels on the client computer. I tested it on different computers in my own environment. I also tried it on the virtual Windows 10 operating system, where I did a clean install. I also tried it on my different physical laptop with Windows 11 and a second Windows 10 operating system. In my environment the application works.

The test print runs successfully in the Zebra Printer application on the customer computer. There is no NLOG error log through my application. In NLOG, my TRACE process steps appear as successful. My source code for printing is as follows.

- Driver is installed.
- Zebra Setup Utilities is installed.

No prints appear in the Windows print queue. When the test page is printed in the Zebra application, the process appears in the Windows printer queue.

I don't know what else I can test. I would be glad if you help.

The operating system of the customer computer is Windows 11.

The video recording of the transaction on the customer's computer is attached.

My development printer device:
ZD22042-T0EG00EZ

Client printer device:
Zebra ZD421, connected via USB-cable (Zebra part# ZD4A043-30EM00EZ)

Please Register or Login to post a reply

3 Replies

C Cenk Yenikoylu

print click code

private void bsiLablePrinter_ItemDoubleClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (IsPrinterDriverInstalled)
                {
                    if (XtraMessageBox.Show("A test label will now be printed from the sample template?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        PrintDialog pd = new PrintDialog();
                        pd.PrinterSettings = new PrinterSettings();

                        string filename = "LabelTest.txt";
                        String text = "";
                        using (StreamReader sr = new StreamReader(filename))
                        {
                            text = sr.ReadToEnd();
                            RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, text);
                        }
                    }
                }
                else
                    XtraMessageBox.Show("The label printer is not available.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

RawHelper function .cs file for print.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BridgeSystemsLogisticApp
{
    public class RawPrinterHelper
    {
        // Structure and API declarions:
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public 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, out 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, Int32 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, Int32 dwCount, out Int32 dwWritten);

        // SendBytesToPrinter()
        // When the function is given a printer name and an unmanaged array
        // of bytes, the function sends those bytes to the print queue.
        // Returns true on success, false on failure.
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
          
            Int32 dwError = 0, dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);
            DOCINFOA di = new DOCINFOA();
            bool bSuccess = false; // Assume failure unless you specifically succeed.

            di.pDocName = "LabelTemplate.txt";// "My C#.NET RAW Document";
            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))
                    {
                        // Write your bytes.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
         
          
        }
        public static bool SendFileToPrinter(string szPrinterName, string szFileName)
        {
          
                // Open the file.
                FileStream fs = new FileStream(szFileName, FileMode.Open);
                // Create a BinaryReader on the file.
                BinaryReader br = new BinaryReader(fs);
                // Dim an array of bytes big enough to hold the file's contents.
                Byte[] bytes = new Byte[fs.Length];
                bool bSuccess = false;
                // Your unmanaged pointer.
                IntPtr pUnmanagedBytes = new IntPtr(0);
                int nLength;

                nLength = Convert.ToInt32(fs.Length);
                // Read the contents of the file into the array.
                bytes = br.ReadBytes(nLength);
                // Allocate some unmanaged memory for those bytes.
                pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
                // Copy the managed byte array into the unmanaged array.
                Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
                // Send the unmanaged bytes to the printer.
                bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
                // Free the unmanaged memory that you allocated earlier.
                Marshal.FreeCoTaskMem(pUnmanagedBytes);
                return bSuccess;
         
        }
        public static bool SendStringToPrinter(string szPrinterName, string szString)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?
            dwCount = szString.Length;
            // Assume that the printer is expecting ANSI text, and then convert
            // the string to ANSI text.
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }
        public static bool SendTextFileToPrinter(string szFileName, string printerName)
        {
            var sb = new StringBuilder();

            using (var sr = new StreamReader(szFileName, Encoding.Default))
            {
                while (!sr.EndOfStream)
                {
                    sb.AppendLine(sr.ReadLine());
                }
            }

            return RawPrinterHelper.SendStringToPrinter(printerName, sb.ToString());
        }

    }
}

---

C Cenk Yenikoylu

label txt code

---

^XA
~TA000
~JSN
^LT0
^MTT
^PON
^PMN
^LH0,0
^JMA
^PR4,4
~SD15
^JUS
^LRN
^CI27
^PA0,1,1,0
^MMT
^PW799
^LL799
^LS0
^FO10,2
^GFA,4449,17512,88,:Z64:eJztW89r29gWPpIjbGyoOlCTTULCrEIK9WYWQx/YKrS8rR/YdBPTf8GLlNlk6GWyCQ1M/gXRtzEuJNvgActdzD6LmreZguhsRAbSLt6AcAbpnXPulS3HluSmIwbe9HRGvr98/OnTp3PPvVIAvtgX+/tavydy8Wut5eJWt3WRh18N/+Vh5SPo5eF3LR96YXfX3snD7120PPz2er288ObC8Pa3O+U8/OaFt7fbywUv3MsnQGzvbufFbx5uobfTK+XhN6/7bQctD79wF3LBe6dUyiX+GjjD5eE3r/kNUuc3cXTLfwL6fQGyFGudnsLj25nFX16TRW6JKpKk1+HtbNKnrzeo6Be4RTSuqaIoat/a6OtVKjSlE1WRfs9uCRetdxOvQ0fvc+G2W/h9o3MTr+S3EDq3xjugy+M4zmRbv49e+vA1Hi+32a/xGXjbykFTnrZk2/psesPQRq1WJKNhGGDlB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5vEl75GyfoNevYSiuSP8unkshiggPzg7/s31/ZL5OjlIFGEL8W7ABeIdf+ya2qj6R1EbHzBe0u+ziz0SzQbhxZFY3u88034mvFvJm0P487VjUa/rLuK97vl3Cu5gYB5eszJZv/cnDzFg1B/AOdJq1/EL3zZ+OvxV+D7UXtspeJFX5LdL/ALxyzxCxC9o+4Rxo8p4kWvkdx+074D4TfRKeM0TMTz9gfgVAvl97w3NQ++I+oanyK8YDo+P7eGp8Ifmscv8Do5EiHgxOotUfmFjS/u4v14cQXcLr/ReUV1n5tfa2yj+PNpDfvfw8wCKo32MgC+ge1AsJnpFvK/qyJ752h00Dt/b/oPCr5OJ+eqSiSP96peD+tnh+0md+X0/gcJ7v96HAPx6/eRVCr/tAwoRTz9uPtV+v0B+3zw/KDafdyN+MRysP8XDPdbvc6T29+56Gw7wUuwV/2Ml4w0DCmmh1/jwQ+ghv144ND25icD65ddMQo/1K8IhlLlhCH4tqMxenVsw490F4tVaTeOf0LGahoZUGhdVS/JrADZqLdhE4TaR7T2kttUy3uKM2DQuIOXdd71v62IXpzP9BD92yriuwP9lrr5bLuNB6D2gaLyziwMfYsMON2zrNqy0BEm7ulO7t8qgOfPOROaYcs/PHnTDvn+WPUYbZ2QNSyzlycDU9H7mHvaCre+vMurT/5qgvMo+UimXvyf8W5j4qwF8sf9b+x8dcTlJ:38F5^FO500,250
^FO20,210
^A0N50,50
^FDModel: [THIS TEST LABEL]^FS
^FO20,260
^A0N,30,30
^FD(C) 2024 Bridge Systems BV^FS
^FO20,290
^A0N,30,30
^FDAll Rights Reserved^FS
^FO20,340
^A0N,40,40
^FDP/O no.:^FS
^FO240,340
^A0N,40,40
^FDBSME23-020^FS
^FO20,380
^A0N,40,40
^FDBatch no.:^FS
^FO240,380
^A0N,40,40
^FD24004^FS
^FO20,420
^A0N,40,40
^FDProd. date:^FS
^FO240,420
^A0N,40,40
^FDApril 2024^FS
^FO20,460
^A0N,40,40
^FDCarton no.:^FS
^FO240,460
^A0N,40,40
^FD116^FS
^FO20,500
^A0N,40,40
^FDQuantity:^FS
^FO240,500
^A0N,40,40
^FD24 pcs^FS
^FO20,540
^A0N,40,40
^FDG.W.:^FS
^FO240,540
^A0N,40,40
^FD8.2 KG^FS
^FO20,580
^A0N,40,40
^FDN.W.:^FS
^FO240,580
^A0N,40,40
^FD3.1 KG^FS
^FO20,620
^A0N,40,40
^FDDimensions:^FS
^FO240,620
^A0N,40,40
^FD21.2x42x28.4 CM^FS
^FO530,260
^BQ,2,5
^FDQA,219072034886093068488337
205992362813P1
232404556317301
CY7NH4ZT900538
8806094343335
W6450343230717684
219072034886093068488337
205992362813P1
232404556317301
CY7NH4ZT900538
8806094343335
W6450343230717684

Quantity: 24 pcs
Production date: 4-2024
Batch number: 24004
Carton number: 116
^FS
^FO280,670^BY3
^BCN,100,N,N,N
^FD24004-116-24^FS
^XZ

 

C Cenk Yenikoylu

Is there any other method besides RAW? Is there a user manual docs about this?

CONTACT
Can’t find what you’re looking for?