1

I'm having an issue with some MFC printing code lately. To describe the issue at a high level, we're getting problems when we try to print the same document twice. (Really, it doesn't have to be the SAME document. It seems that it happens when two documents are printed and the child window isn't destroyed and created between them.) The first time, the document prints fine. The second time, the program crashes with an uncaught exception.

This also only started happening after a recent upgrade. This is code that I've literally never touched. It worked fine in version 5 when we were using VS2005 and MFC8. But then, when we upgraded to VS2008 and MFC9, this started happening.

Here's the problem code:

void CReportWnd::OnPrint() 
{
    CDC dc;
    CPrintDialog dlg (FALSE);
    CPrintDialog defaults(FALSE);
    DEVMODE *ldev_printinfo;
    int li_first = 0;
    int li_last;


    int ret = defaults.GetDefaults();
    ldev_printinfo = defaults.GetDevMode();
    //ldev_printinfo->dmOrientation = DMORIENT_LANDSCAPE;
    dc.Attach (defaults.GetPrinterDC ());
    dc.ResetDC(ldev_printinfo);

    PROPrint(1, NULL, &dc, NULL, &li_last, true);

    dlg.m_pd.hDevMode = ldev_printinfo;
    dlg.m_pd.Flags &= ~PD_NOPAGENUMS;
    dlg.m_pd.nMinPage = 1;
    dlg.m_pd.nFromPage = 1;
    dlg.m_pd.nMaxPage = li_last;
    dlg.m_pd.nToPage = li_last;     

    if (dlg.DoModal () == IDOK) {
        dc.DeleteDC();
        dc.Detach();
        dc.Attach (dlg.GetPrinterDC ());        
    } else {
        return;
    }

    //Set up document info (need to set the name)
    DOCINFO di;
    ::ZeroMemory (&di, sizeof (DOCINFO));
    di.cbSize = sizeof (DOCINFO);
    di.lpszDocName = "Report";

    if(dc.StartDoc(&di) <= 0) {
        return;
    }


    if(dlg.PrintRange()) {
        li_first = dlg.m_pd.nFromPage - 1;
        li_last = dlg.m_pd.nToPage - 1;
    }

    //Now do the actual print job to the printing device
    PROPrint(1, NULL, &dc, &li_first, &li_last, false);
}

On the int ret = ... line near the top is where the exception is thrown. GetDefaults() throws an access violation exception. But again, only the second time that this function is called. It seems to me like it's some sort of resource issue. Like a resource isn't being freed and allocated properly. But I'm so inexperienced with printing that it could be anything.

If anybody could offer any kind of help, I would really appreciate it.

Also, yes, I know that I can just catch the exception. My issue is, how do I handle the exception and still print the document?

EDIT: It seems as if our program is having this issue in multiple places, not just with this particular set of code. It makes me think that the issue might not be in this code specifically. I'm working on it now and I kind of have to get it fixed, so when I figure out the solution, I'll be sure to post back. Until then, I'm always open to suggestions.

4

1 回答 1

0

不确定,但可能是因为代码正在删除与默认打印机关联的默认设备上下文?尝试删除该dc.DeleteDC()行给您:

if (dlg.DoModal() == IDOK) 
{
   dc.Detach();
   dc.Attach(dlg.GetPrinterDC());        
} 
else
   return;
于 2012-09-12T08:16:36.813 回答