selfhost wcf服务网络打印机错误访问被拒绝

时间:2016-03-17 13:44:50

标签: c# wcf networking printing

我有一个自托管服务,可以打印一些文本。当使用安装的本地打印机打印此服务时,它可以正常工作。 但是当这项服务使用网络打印机进行打印时,我的用户可以使用该打印机并将其添加到打印机和设备中,这会给出错误' 5'这是访问被拒绝。 我已经测试打印到网络打印机,它工作得很好,我创建了一个win form应用程序,并使用网络打印机打印在其中,使用与服务中使用的相同的代码。它在那里工作,但我不知道为什么我的服务给了我访问被拒绝错误。我无法找到解决问题的方法。我的代码是:

public class RawPrinterHelper
    {

        public static string errorMSG = "false";

        // 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 = "Document";
            di.pDataType = "RAW";

            // Open the printer.
            if (OpenPrinter(szPrinterName, 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);
                    }
                    else
                    {
                        errorMSG = "Out of Paper";
                    }
                    EndDocPrinter(hPrinter);
                }
                else
                {
                    errorMSG = "Offline Printer";
                }
                ClosePrinter(hPrinter);
            }
            else
            {
                errorMSG = "Printer Not Found";
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {

                dwError = Marshal.GetLastWin32Error();
                File.AppendAllText(@"d:\test.txt", dwError.ToString() + Environment.NewLine);
            }
            return bSuccess;
        }

        public static bool SendStringToPrinter(string szPrinterName, string pDoc)
        {
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pDoc));
            byte[] rawData = new byte[stream.Length];
            stream.Read(rawData, 0, (int)stream.Length);
            GCHandle rawDataHandle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            IntPtr pointer = rawDataHandle.AddrOfPinnedObject();
            errorMSG = "false";
            return SendBytesToPrinter(szPrinterName, pointer, rawData.Length);
        }

        public static bool SendBytesToPrinter(string szPrinterName, byte[] pDoc)
        {
            MemoryStream stream = new MemoryStream(pDoc);
            byte[] rawData = new byte[stream.Length];
            stream.Read(rawData, 0, (int)stream.Length);
            GCHandle rawDataHandle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            IntPtr pointer = rawDataHandle.AddrOfPinnedObject();
            errorMSG = "false";
            return SendBytesToPrinter(szPrinterName, pointer, rawData.Length);
        }
    }

根据我的观察,此功能(StartDocPrinter(hPrinter, 1, di))返回false,错误号为' 5' (访问被拒绝)。

我的服务正在本地服务中运行,并带有我用户的登录凭据。

0 个答案:

没有答案