StartDocPrinter失败(ERROR_INSUFFICIENT_BUFFER)

时间:2017-05-23 17:37:46

标签: c# .net winapi

我试图打开原始打印机连接并且StartDocPrinter调用失败。 Win32Error是122(ERROR_INSUFFICIENT_BUFFER)。

当以普通用户或管理员身份运行时,此可执行文件在同一台计算机上运行正常,但在作为"本地系统"运行时失败。用户。 (虽然我已经将其作为"本地系统"用户运行的其他几台计算机工作。)我真的不想考虑将其作为不同的用户运行,因为它&#39 ; s正在运行服务,并可能涉及系统的其他几个大的更改。

using System;
using System.Runtime.InteropServices;

namespace EPLTest
{
    class Program
    {
        [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);

        static void Main(string[] args)
        {
            IntPtr hPrinter;
            if (!OpenPrinter("ZDesigner LP2824 Raw".Normalize(), out hPrinter, IntPtr.Zero))
            {
                Console.WriteLine("OpenPrinter Error:" + Marshal.GetLastWin32Error());
                return;
            }
            Console.WriteLine("hPrinter: " + hPrinter.ToString("x8"));

            DOCINFOA di = new DOCINFOA { pDocName = "Barcode Test", pDataType = "RAW" };
            if (StartDocPrinter(hPrinter, 1, di))
            {
                Console.WriteLine("StartDocPrinter Success");

                // Printing code omitted for brevity

                EndDocPrinter(hPrinter);
            }
            else
            {
                Console.WriteLine("StartDocPrinter Error:" + Marshal.GetLastWin32Error());
            }
            ClosePrinter(hPrinter);
        }
    }
}

示例输出:

hPrinter: 010b7e3c
StartDocPrinter Error:122

有什么想法吗?

0 个答案:

没有答案