CreateDC() 失败并显示 LastError 2
我已经实现了一个获取本地打印机的类,并根据应用程序选项使用可用的本地打印机之一。
首先,该类枚举打印机(EnumPrinters - PRINTER_ENUM_LOCAL)并保存相应的PRINTER_INFO_2。然后,它获取打印机功能(DeviceCapabilites)和DEVMODE。每个类实例将访问一台打印机。
当应用程序选择打印选项时,它会选择将使用哪台打印机及其相应的类实例。然后,该实例创建设备上下文:
m_hdc = CreateDC (m_pi2->pDriverName, m_pi2->pPrinterName, NULL, m_pdm));
并
HDC m_hdc;
PRINTER_INFO_2 * m_pi2;
DEVMODE * m_pdm;
相应地处理所有打印数据。
问题是有时 CreateDC 返回 NULL 而 GetLastError() 返回 ERROR_FILE_NOT_FOUND(2)。
我的意思是“有时”,因为在其他机器上,使用相同的打印机、相同的处理器、相同的 Windows XP SP3 映像和相同的测试数据,CreateDC 可以正确处理。另外,重装系统和应用程序后问题有时也不会消失。
我期待听到任何有助于我找出问题的建议。
先感谢您。
I have implemented a class that gets local printers and, depending on the application option, uses one of the local printer available.
Firstly, the class enumerates the printers (EnumPrinters - PRINTER_ENUM_LOCAL) and saves the corresponding PRINTER_INFO_2. Then, it gets printer capabilities (DeviceCapabilites) and DEVMODE. Each class instance will access one printer.
When application selects the printing option, it selects which printer will be used and its corresponding instance of the class. Then, this instance creates the device context:
m_hdc = CreateDC (m_pi2->pDriverName, m_pi2->pPrinterName, NULL, m_pdm));
where
HDC m_hdc;
PRINTER_INFO_2 * m_pi2;
DEVMODE * m_pdm;
and process all printing data accordingly.
The problem is that sometimes, the CreateDC return NULL and GetLastError() return ERROR_FILE_NOT_FOUND(2).
I mean 'sometimes' because in other machines, with same printer, same processor, same Windows XP SP3 image and same test data, the CreateDC processes correctly. In addition, the reinstalling the system and application the problem disappears sometimes no.
I am looking forward to hearing any suggestion that helps me to find out the issue.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来好像加载打印机驱动程序所需的文件时出现问题。您可以使用 Process Monitor(一个免费的 SysInternals 工具,可从 microsoft.com 下载)来获取有关错误发生时发生的情况的大量信息。我会进行捕获并查看失败的文件和注册表访问。它在一台特定机器上间歇性失败的事实似乎与混乱的驱动程序配置一致。
您可以尝试的另一件事是创建信息上下文而不是设备上下文。您无法使用 IC 进行打印,但您可以查询有关设备的信息,这可能是获取附加信息的一种方式。
It sounds like a problem loading a file required by the printer driver. You could use Process Monitor (a free SysInternals tool downloadable from microsoft.com) to get a bunch of information about what's going on at the time of the error. I'd do a capture and look at failed file and registry accesses. The fact that it fails intermittently on one particular machine seems consistent with a messed up driver configuration.
Another thing you could try is to create an information context rather than a device context. You can't print with an IC, but you can query information about the device, which may be a way to get additional information.