c ++ GDI打印导致系统冻结
c++ GDI printing causes system to freeze
我目前正在维护一个为 SD 卡创建标签的旧版 c++ 软件。该软件使用 GDI 创建打印作业,通常没有问题。但是,打印一定数量后,系统永远死机,连鼠标都没有反应了。我无法判断应用程序崩溃的位置,因为它发生在不同的点,而且完全无法预测。我什至不确定我需要在这里显示什么代码。
我已经设置了很多断点并逐步执行了代码的不同部分,但由于问题只是随机出现,所以我无法得出任何结论。我也尝试 运行 在不同的环境中运行该应用程序,例如虚拟机,但无论我使用什么机器,问题都会再次出现。
HDC printerDC = CreateDCW(L"WINSPOOL", printers[SELECTED_PRINTERS[printerIndex]].c_str(), NULL, NULL);
DODEBUG(L"CreateDCW returned: " << printerDC << L" (should be something else than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"CreateDCW");
SetGraphicsMode(printerDC, GM_ADVANCED);
int result4 = StartDocW(printerDC, &di);
DODEBUG(L"StartDocW returned: " << result4 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"StartDocW");
if(result4 > 0)
{
int result5 = StartPage(printerDC);
DODEBUG(L"StartPage returned: " << result5 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"StartPage");
SetBkMode(printerDC, TRANSPARENT);
//1 dpi = 0.03937 pixel/mm; 1 pixel/mm = 25.4 dpi; 1 dpi = 0.003937 pixel/papersize; 0.1 pixel/papersize = 25.4 dpi;
//1 px / 1 mm = 25.4 dpi => 1 px = 25.4 dpi * 1 mm => 1/(25.4) px = 25.4 dpi * 1 mm
//1 dpi = 0.03937 pixel/mm => 1dpi * 1mm = 0.03937 pixel
BITMAPINFOHEADER bi = { 0 };
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biHeight = (printerPaperYResolution * printLength) / 254;
bi.biWidth = (printerPaperXResolution * printWidth) / 254;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
//Fix divisible-by-4-bug:
LONG byteWidth = bi.biWidth * 3;
LONG xDiv = byteWidth % 12;
if(xDiv > 0)
{
bi.biWidth += (12 - xDiv) / 3;
byteWidth = bi.biWidth * 3;
}
LONG yDiv = (bi.biHeight * 3) % 12;
if(yDiv > 0)
{
bi.biHeight += (12 - yDiv) / 3;
}
LONG byteCount = 3 * bi.biHeight*bi.biWidth;
byte* pBits = new byte[byteCount];
memset(pBits, 255, byteCount);
switch(printerIndex)
{
case PRINTER_ZERTIFIKAT:
PrintZertifikat(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINTER_LABEL_PACKUNG:
PrintLabelPackung(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINTER_LABEL_SDKARTE:
PrintLabelSDKarte(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINT_ONLY_SDLABEL:
PrintOnlySDLabel(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
}
int result6 = EndPage(printerDC);
DODEBUG(L"EndPage returned: " << result6 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"EndPage");
int result7 = EndDoc(printerDC);
}
你用DeleteDC()正确释放你的HDC了吗?
我目前正在维护一个为 SD 卡创建标签的旧版 c++ 软件。该软件使用 GDI 创建打印作业,通常没有问题。但是,打印一定数量后,系统永远死机,连鼠标都没有反应了。我无法判断应用程序崩溃的位置,因为它发生在不同的点,而且完全无法预测。我什至不确定我需要在这里显示什么代码。
我已经设置了很多断点并逐步执行了代码的不同部分,但由于问题只是随机出现,所以我无法得出任何结论。我也尝试 运行 在不同的环境中运行该应用程序,例如虚拟机,但无论我使用什么机器,问题都会再次出现。
HDC printerDC = CreateDCW(L"WINSPOOL", printers[SELECTED_PRINTERS[printerIndex]].c_str(), NULL, NULL);
DODEBUG(L"CreateDCW returned: " << printerDC << L" (should be something else than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"CreateDCW");
SetGraphicsMode(printerDC, GM_ADVANCED);
int result4 = StartDocW(printerDC, &di);
DODEBUG(L"StartDocW returned: " << result4 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"StartDocW");
if(result4 > 0)
{
int result5 = StartPage(printerDC);
DODEBUG(L"StartPage returned: " << result5 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"StartPage");
SetBkMode(printerDC, TRANSPARENT);
//1 dpi = 0.03937 pixel/mm; 1 pixel/mm = 25.4 dpi; 1 dpi = 0.003937 pixel/papersize; 0.1 pixel/papersize = 25.4 dpi;
//1 px / 1 mm = 25.4 dpi => 1 px = 25.4 dpi * 1 mm => 1/(25.4) px = 25.4 dpi * 1 mm
//1 dpi = 0.03937 pixel/mm => 1dpi * 1mm = 0.03937 pixel
BITMAPINFOHEADER bi = { 0 };
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biHeight = (printerPaperYResolution * printLength) / 254;
bi.biWidth = (printerPaperXResolution * printWidth) / 254;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
//Fix divisible-by-4-bug:
LONG byteWidth = bi.biWidth * 3;
LONG xDiv = byteWidth % 12;
if(xDiv > 0)
{
bi.biWidth += (12 - xDiv) / 3;
byteWidth = bi.biWidth * 3;
}
LONG yDiv = (bi.biHeight * 3) % 12;
if(yDiv > 0)
{
bi.biHeight += (12 - yDiv) / 3;
}
LONG byteCount = 3 * bi.biHeight*bi.biWidth;
byte* pBits = new byte[byteCount];
memset(pBits, 255, byteCount);
switch(printerIndex)
{
case PRINTER_ZERTIFIKAT:
PrintZertifikat(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINTER_LABEL_PACKUNG:
PrintLabelPackung(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINTER_LABEL_SDKARTE:
PrintLabelSDKarte(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
case PRINT_ONLY_SDLABEL:
PrintOnlySDLabel(sdCardData, printerDC, pBits, byteCount, byteWidth, &bi);
break;
}
int result6 = EndPage(printerDC);
DODEBUG(L"EndPage returned: " << result6 << L" (should be greater than 0), GetLastError: " << GetLastError() << L" (should be 0)", L"EndPage");
int result7 = EndDoc(printerDC);
}
你用DeleteDC()正确释放你的HDC了吗?