简体   繁体   English

c ++ GDI打印导致系统死机

[英]c++ GDI printing causes system to freeze

I am curently maintaining an old peace of c++ sofware that creates labels for SD-Cards. 我目前正在维护可为SD卡创建标签的C ++软件的旧版本。 The software uses GDI to create the print jobs and usually there is no problem with that. 该软件使用GDI创建打印作业,通常没有问题。 However, after a certain amount of prints, the system freezes forever, even the mouse does not respond anymore. 但是,经过一定数量的打印后,系统将永久冻结,即使鼠标不再响应。 I can't tell where the app crashes, because it happens at different points and it is totally unpredictabe. 我无法判断应用程序崩溃的位置,因为它发生在不同的时间点,而且完全是无法预测的。 I am not even sure what code I need to show here. 我什至不确定我需要在这里显示什么代码。

I aready set loads of breakpoints and stepped through different parts of the code but as the problem appears only randomly, it was not possible for me to come to any conclusion. 我详细设置了断点,并逐步遍历了代码的不同部分,但是由于问题只是随机出现的,所以我无法得出任何结论。 I also tried to run the app on different environments, such as a VM but regardless what machine I use, the problem reappears. 我还尝试在不同的环境(例如VM)上运行该应用程序,但是无论我使用哪种计算机,问题都会再次出现。

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?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM