SetPrinter API返回错误1797 - 打印机驱动程序未知 [英] SetPrinter API returning error 1797 - Printer Driver Unknown

查看:592
本文介绍了SetPrinter API返回错误1797 - 打印机驱动程序未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改我创建的虚拟打印机上的本地端口(将内容打印到其他文件)。但是,通过调用GetPrinter预填充的PRINTER_INFO_2结构的SetPrinter API调用始终失败并显示1797错误代码。

I am trying to change the Local Port on a virtual printer I have created (to print stuff to a different file). However, the SetPrinter API call, with a PRINTER_INFO_2 structure pre-filled in by a call to GetPrinter, always fails with a 1797 error code.

我已尝试手动将pDriverName属性设置为已知的打印驱动程序,或将其设置为null,或将其设置为空 - 都具有相同的结果。

I have tried setting the pDriverName property manually to the known print driver, or setting it to null, or setting it to empty - all with the same result.

我在下面包含了我的SetPort代码 - 它可能比它需要的更多到目前为止(比如重新创建DEVMODE),但我一直都在努力。

I've included my code for SetPort below - its probably doing more than it needs to at the moment (like recreateing the DEVMODE), but I've been trying everything.

任何人都有更多的想法?非常感谢。

Anyone have any more ideas? Would be much appreciated.

        private static void SetPort(IntPtr hPrinter, string printerName, string portName) {
            uint dwNeeded = 0;
            if (GetPrinter(hPrinter, 2, IntPtr.Zero, 0, ref dwNeeded))
                throw new Exception("Unexpected Error: Unable to get printer information for " + printerName);
            int nError = Marshal.GetLastWin32Error();
            if (nError != ERROR_INSUFFICIENT_BUFFER)
                throw new Win32Exception(nError);
            if (dwNeeded > 0) {
                IntPtr pInfo = Marshal.AllocHGlobal((int)dwNeeded);
                try {
                    PRINTER_INFO_2 info;
                    if (GetPrinter(hPrinter, 2, pInfo, dwNeeded, ref dwNeeded)) {
                        info = (PRINTER_INFO_2)Marshal.PtrToStructure(pInfo, typeof(PRINTER_INFO_2));
                        if (!info.pPortName.Equals(portName, StringComparison.OrdinalIgnoreCase)) {
                            IntPtr pInfo2 = Marshal.AllocHGlobal((int)dwNeeded);
                            try {
                                PRINTER_INFO_2 info2 = info;
                                info2.pPortName = portName;
                                info2.pSecurityDescriptor = IntPtr.Zero;
                                IntPtr ptrZero = IntPtr.Zero;
                                int devSize = DocumentProperties(IntPtr.Zero, hPrinter, printerName, IntPtr.Zero, ref ptrZero, 0);
                                IntPtr devModeData = Marshal.AllocHGlobal(devSize);
                                try {
                                    DocumentProperties(IntPtr.Zero, hPrinter, printerName, devModeData, ref ptrZero, DM_OUT_BUFFER);
                                    info2.pDevMode = devModeData;
                                    Marshal.StructureToPtr(info2, pInfo2, false);
                                    if (!SetPrinter(hPrinter, 2, pInfo2, 0))
                                        throw new Win32Exception(Marshal.GetLastWin32Error());
                                }
                                finally {
                                    Marshal.FreeHGlobal(devModeData);
                                }
                            }
                            finally {
                                Marshal.FreeHGlobal(pInfo2);
                            }
                        }
                    }
                }
                finally {
                    Marshal.FreeHGlobal(pInfo);
                }
            }




推荐答案

我读了你的代码,但我不知道,我会请花药高级工程师回复你。

I read your codes, but I have no idea, I will involve anther senior engineer to reply to you.

祝福,


这篇关于SetPrinter API返回错误1797 - 打印机驱动程序未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆