我的StartDoc()在Windows 7中失败,在Windows XP中运行良好 < >我的代码在32位Visual C++ 2010中。这是使用默认打印机1进行的常规打印,如下所示这是精确的代码减去错误处理程序: // Get the length of the printer name. GetDefaultPrinter(NULL, &size); lpcPrinterName = new char[size]; // Get the printer name. if(!GetDefaultPrinter(lpcPrinterName, &size)) { // handle error return false; } // Get a device context for the printer. hdcPrint = CreateDC(NULL, lpcPrinterName, NULL, NULL); // get printer parameters iPrinterDPIX = GetDeviceCaps(hdcPrint, LOGPIXELSX); // x dpi iPrinterDPIY = GetDeviceCaps(hdcPrint, LOGPIXELSY); // y dpi iPrinterBPP = GetDeviceCaps(hdcPrint, BITSPIXEL); // bit per pixel iPrinterHRes = GetDeviceCaps(hdcPrint, HORZRES); // x printable area in pixels. 0 maps to 0 here iPrinterVRes = GetDeviceCaps(hdcPrint, VERTRES); // y printable area in pixels. 0 maps to 0 here if (!OpenPrinter(lpcPrinterName, &printerHandle, NULL)) { // handle error return false; } // initialize docInfo ZeroMemory(&docInfo, sizeof(docInfo)); docInfo.cbSize = sizeof(docInfo); docInfo.lpszDocName = lpcstrDocName; // ---> this is where it fails when run standalone on Windows 7, return value == -1 iPrintJobID = StartDoc(hdcPrint, &docInfo); // this starts a print job if (iPrintJobID <= 0) { // handle error return false; } if (StartPage(hdcPrint) <= 0) { // this starts a new page // handle error return false; } { // enclose in an inner scope to get graphics destroyed before deleting dc Gdiplus::Graphics graphics(hdcPrint, printerHandle); if (graphics.DrawImage(&bmp,x,y) != Ok) // handle error return false; } } if (EndPage(hdcPrint) <= 0) { // ends the page (eject paper) // handle error return false; } if (EndDoc(hdcPrint) <= 0) { // end the print job (actually starts printing) // handle error return false; } ClosePrinter(printerHandle); DeleteDC(hdcPrint); delete[] lpcPrinterName;

我的StartDoc()在Windows 7中失败,在Windows XP中运行良好 < >我的代码在32位Visual C++ 2010中。这是使用默认打印机1进行的常规打印,如下所示这是精确的代码减去错误处理程序: // Get the length of the printer name. GetDefaultPrinter(NULL, &size); lpcPrinterName = new char[size]; // Get the printer name. if(!GetDefaultPrinter(lpcPrinterName, &size)) { // handle error return false; } // Get a device context for the printer. hdcPrint = CreateDC(NULL, lpcPrinterName, NULL, NULL); // get printer parameters iPrinterDPIX = GetDeviceCaps(hdcPrint, LOGPIXELSX); // x dpi iPrinterDPIY = GetDeviceCaps(hdcPrint, LOGPIXELSY); // y dpi iPrinterBPP = GetDeviceCaps(hdcPrint, BITSPIXEL); // bit per pixel iPrinterHRes = GetDeviceCaps(hdcPrint, HORZRES); // x printable area in pixels. 0 maps to 0 here iPrinterVRes = GetDeviceCaps(hdcPrint, VERTRES); // y printable area in pixels. 0 maps to 0 here if (!OpenPrinter(lpcPrinterName, &printerHandle, NULL)) { // handle error return false; } // initialize docInfo ZeroMemory(&docInfo, sizeof(docInfo)); docInfo.cbSize = sizeof(docInfo); docInfo.lpszDocName = lpcstrDocName; // ---> this is where it fails when run standalone on Windows 7, return value == -1 iPrintJobID = StartDoc(hdcPrint, &docInfo); // this starts a print job if (iPrintJobID <= 0) { // handle error return false; } if (StartPage(hdcPrint) <= 0) { // this starts a new page // handle error return false; } { // enclose in an inner scope to get graphics destroyed before deleting dc Gdiplus::Graphics graphics(hdcPrint, printerHandle); if (graphics.DrawImage(&bmp,x,y) != Ok) // handle error return false; } } if (EndPage(hdcPrint) <= 0) { // ends the page (eject paper) // handle error return false; } if (EndDoc(hdcPrint) <= 0) { // end the print job (actually starts printing) // handle error return false; } ClosePrinter(printerHandle); DeleteDC(hdcPrint); delete[] lpcPrinterName;,printing,visual-studio-debugging,visual-c++-2010,Printing,Visual Studio Debugging,Visual C++ 2010,删除该代码片段将清除所有问题。几个月前,我们只有一位使用Windows 10的超过15000名客户出现了相同的问题 经过长时间的调查,我们发现问题在于以前使用通用参数获得的打印机HDC。在我们需要呼叫StartDoc之前,它在所有操作中都运行良好 解决方案是使用我们已有的HDC执行当前的StartDoc调用,如果出现错误,则创建一个新的打印机DC,指定具体的pDeviceMode参数: newPrinterDC = CreateDC(NULL, PrinterName, NULL, pDevic

删除该代码片段将清除所有问题。

几个月前,我们只有一位使用Windows 10的超过15000名客户出现了相同的问题

经过长时间的调查,我们发现问题在于以前使用通用参数获得的打印机HDC。在我们需要呼叫StartDoc之前,它在所有操作中都运行良好

解决方案是使用我们已有的HDC执行当前的StartDoc调用,如果出现错误,则创建一个新的打印机DC,指定具体的pDeviceMode参数:

newPrinterDC = CreateDC(NULL, PrinterName, NULL, pDeviceMode); 
之后,我们可以使用newPrinterDC再次执行StartDoc调用

这解决了问题,并在100%的情况下正确运行


我希望它能帮助您。

StartDoc没有设置错误代码,因此您从GetLastError获得的信息毫无意义。该代码段跳过了所需的错误检查,很难猜测docInfo可能是什么样子。测试你的理论的一个显而易见的方法就是从另一个程序打印,比如记事本。如果这也失败了,那么你需要极客团队而不是程序员的帮助。也可以使用和比较。我已经尝试过了,其他程序可以在任何用户下很好地打印。你说得对,这个bug真的很奇怪我很抱歉我遗漏了docInfo初始化。我现在已经包括在内了。至于错误检查,在我的例子中,StartDoc失败时返回-1。为了清晰起见,我删除了错误检查代码。
newPrinterDC = CreateDC(NULL, PrinterName, NULL, pDeviceMode);