Link to home
Start Free TrialLog in
Avatar of Thomas Stockbruegger
Thomas StockbrueggerFlag for Germany

asked on

Need some help with fonts and printing

Hello...I need your help, again.
my printing will crash...I have enclosed the complete call stack.
The user clicks on this button
OnBnClickedButton_Drucken()
and I am calling Drucken_weiss() to start printing.

Can you help me to find out what the error means.
500 points.
Best regards,
Thomas

still learing this difficult printing thing






>my call stack	

mfc71d.dll!CGdiObject::Attach(void * hObject=0x770a068f)  Zeile 1102 + 0x3	C++
 	mfc71d.dll!CFont::CreateFontA(int nHeight=-40, int nWidth=0, int nEscapement=0, int nOrientation=0, int nWeight=600, unsigned char bItalic=0, unsigned char bUnderline=0, unsigned char cStrikeOut=0, unsigned char nCharSet=' ', unsigned char nOutPrecision=' ', unsigned char nClipPrecision=' ', unsigned char nQuality=0, unsigned char nPitchAndFamily=0, const char * lpszFacename=0x0066a080)  Zeile 166 + 0x4f	C++
 	Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::OnBeginPrinting(CDC * pDC=0x0012e914, CPrintInfo * pInfo=0x0012e898)  Zeile 2052	C++
 	Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss()  Zeile 1978	C++
 	Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::OnBnClickedButton_Drucken()  Zeile 1647	C++
 	mfc71d.dll!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x00fb9b90, unsigned int nID=1029, int nCode=0, void (void)* pfn=0x004ccea0, void * pExtra=0x00000000, unsigned int nSig=53, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 89	C++
 	mfc71d.dll!CCmdTarget::OnCmdMsg(unsigned int nID=1029, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 396 + 0x27	C++
 	mfc71d.dll!CDialog::OnCmdMsg(unsigned int nID=1029, int nCode=0, void * pExtra=0x00000000, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 88 + 0x18	C++
 	mfc71d.dll!CWnd::OnCommand(unsigned int wParam=1029, long lParam=1246560)  Zeile 2550	C++
 	mfc71d.dll!CWnd::OnWndMsg(unsigned int message=273, unsigned int wParam=1029, long lParam=1246560, long * pResult=0x0012ed74)  Zeile 1759 + 0x1c	C++
 	mfc71d.dll!CWnd::WindowProc(unsigned int message=273, unsigned int wParam=1029, long lParam=1246560)  Zeile 1745 + 0x1e	C++
 	mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb9b90, HWND__ * hWnd=0x00190582, unsigned int nMsg=273, unsigned int wParam=1029, long lParam=1246560)  Zeile 241 + 0x1a	C++
 	mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x00190582, unsigned int nMsg=273, unsigned int wParam=1029, long lParam=1246560)  Zeile 389	C++
 	mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x00190582, unsigned int nMsg=273, unsigned int wParam=1029, long lParam=1246560)  Zeile 209 + 0x15	C++
 	USER32.DLL!77e2a454() 	
 	USER32.DLL!77e06382() 	
 	USER32.DLL!77e07361() 	
 	USER32.DLL!77e209ef() 	
 	USER32.DLL!77e1c1de() 	
 	USER32.DLL!77e186f0() 	
 	mfc71d.dll!CWnd::OnWndMsg(unsigned int message=514, unsigned int wParam=0, long lParam=655378, long * pResult=0x00fbabe0)  Zeile 1880	C++
 	mfc71d.dll!CWnd::WindowProc(unsigned int message=514, unsigned int wParam=0, long lParam=655378)  Zeile 1746 + 0x1a	C++
 	mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fbabe0, HWND__ * hWnd=0x00130560, unsigned int nMsg=514, unsigned int wParam=0, long lParam=655378)  Zeile 241 + 0x1a	C++
 	mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x00130560, unsigned int nMsg=514, unsigned int wParam=0, long lParam=655378)  Zeile 389	C++
 	mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x00130560, unsigned int nMsg=514, unsigned int wParam=0, long lParam=655378)  Zeile 209 + 0x15	C++
 	USER32.DLL!77e2a454() 	
 	USER32.DLL!77e04605() 	
 	NTDLL.DLL!77891baf() 	
 	USER32.DLL!77e0a7f2() 	
 	USER32.DLL!77e1ecf3() 	
 	USER32.DLL!77e1f46b() 	
 	mfc71d.dll!CWnd::IsDialogMessageA(tagMSG * lpMsg=0x00136a48)  Zeile 200	C++
 	mfc71d.dll!CWnd::PreTranslateInput(tagMSG * lpMsg=0x00136a48)  Zeile 4512	C++
 	mfc71d.dll!CDialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 83	C++
 	mfc71d.dll!CWnd::WalkPreTranslateTree(HWND__ * hWndStop=0x007404ba, tagMSG * pMsg=0x00136a48)  Zeile 3129 + 0x12	C++
 	mfc71d.dll!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 238 + 0x12	C++
 	mfc71d.dll!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 795 + 0x9	C++
 	mfc71d.dll!AfxPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 257 + 0xf	C++
 	mfc71d.dll!AfxInternalPumpMessage()  Zeile 183 + 0x18	C++
 	mfc71d.dll!CWinThread::PumpMessage()  Zeile 916	C++
 	mfc71d.dll!AfxPumpMessage()  Zeile 195 + 0xb	C++
 	mfc71d.dll!CWnd::RunModalLoop(unsigned long dwFlags=4)  Zeile 4566 + 0x5	C++
 	mfc71d.dll!CDialog::DoModal()  Zeile 527 + 0xc	C++
 	Stockbruegger.exe!CLieferscheine_Dialog::OnNMClickList1(tagNMHDR * pNMHDR=0x0012f85c, long * pResult=0x0012f63c)  Zeile 3658 + 0x1a	C++
 	mfc71d.dll!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x00fb8c38, unsigned int nID=1000, int nCode=65534, void (void)* pfn=0x004cd6bb, void * pExtra=0x0012f540, unsigned int nSig=57, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 119	C++
 	mfc71d.dll!CCmdTarget::OnCmdMsg(unsigned int nID=1000, int nCode=65534, void * pExtra=0x0012f540, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 396 + 0x27	C++
 	mfc71d.dll!CDialog::OnCmdMsg(unsigned int nID=1000, int nCode=5177342, void * pExtra=0x0012f540, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 88 + 0x18	C++
 	mfc71d.dll!CWnd::OnNotify(unsigned int __formal=1000, long lParam=1243228, long * pResult=0x0012f63c)  Zeile 2576	C++
 	mfc71d.dll!CWnd::OnWndMsg(unsigned int message=78, unsigned int wParam=1000, long lParam=1243228, long * pResult=0x0012f670)  Zeile 1771 + 0x28	C++
 	mfc71d.dll!CWnd::WindowProc(unsigned int message=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 1745 + 0x1e	C++
 	mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb8c38, HWND__ * hWnd=0x0010051a, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 241 + 0x1a	C++
 	mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x0010051a, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 389	C++
 	mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x0010051a, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 209 + 0x15	C++
 	USER32.DLL!77e2a454() 	
 	USER32.DLL!77e06382() 	
 	USER32.DLL!77e07361() 	
 	comctl32.dll!71715c1d() 	
 	USER32.DLL!77e04750() 	
 	comctl32.dll!71738e84() 	
 	comctl32.dll!71737085() 	
 	comctl32.dll!717388c5() 	
 	comctl32.dll!71738b08() 	
 	comctl32.dll!717171c3() 	
 	mfc71d.dll!AfxUnlockGlobals(int nLockType=7)  Zeile 112	C++
 	mfc71d.dll!CWnd::OnWndMsg(unsigned int message=2082666204, unsigned int wParam=513, long lParam=1, long * pResult=0x007301ce)  Zeile 1880	C++
 	0012faec()	
 	mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb8cac, HWND__ * hWnd=0x0019054c, unsigned int nMsg=513, unsigned int wParam=1, long lParam=7537102)  Zeile 241 + 0x1a	C++
 	mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x0019054c, unsigned int nMsg=513, unsigned int wParam=1, long lParam=7537102)  Zeile 389	C++
 	mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x0019054c, unsigned int nMsg=513, unsigned int wParam=1, long lParam=7537102)  Zeile 209 + 0x15	C++
 	USER32.DLL!77e2a454() 	
 	USER32.DLL!77e04605() 	
 	USER32.DLL!77e19fd5() 	
 	USER32.DLL!77e1e0a6() 	
 	USER32.DLL!77e0a7f2() 	
 	USER32.DLL!77e1ecf3() 	
 	USER32.DLL!77e1f46b() 	
 	mfc71d.dll!CWnd::IsDialogMessageA(tagMSG * lpMsg=0x00136a48)  Zeile 200	C++
 	mfc71d.dll!CWnd::PreTranslateInput(tagMSG * lpMsg=0x00136a48)  Zeile 4512	C++
 	mfc71d.dll!CDialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 83	C++
 	Stockbruegger.exe!CLieferscheine_Dialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 3592	C++
 	mfc71d.dll!CWnd::WalkPreTranslateTree(HWND__ * hWndStop=0x007404ba, tagMSG * pMsg=0x00136a48)  Zeile 3129 + 0x12	C++
 	mfc71d.dll!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 238 + 0x12	C++
 	mfc71d.dll!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 795 + 0x9	C++
 	mfc71d.dll!AfxPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 257 + 0xf	C++
 	mfc71d.dll!AfxInternalPumpMessage()  Zeile 183 + 0x18	C++
 	mfc71d.dll!CWinThread::PumpMessage()  Zeile 916	C++
 	mfc71d.dll!CWinThread::Run()  Zeile 637 + 0xb	C++
 	mfc71d.dll!CWinApp::Run()  Zeile 701	C++
 	mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00133952, int nCmdShow=1)  Zeile 49 + 0xb	C++
 	Stockbruegger.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00133952, int nCmdShow=1)  Zeile 25	C++
 	Stockbruegger.exe!WinMainCRTStartup()  Zeile 390 + 0x39	C
 	KERNEL32.DLL!77e989d5() 	

Open in new window






Stop here:
BOOL CGdiObject::Attach(HGDIOBJ hObject)
{
 -----> ASSERT(m_hObject == NULL);      // only attach once, detach on destroy      if (hObject == NULL)
            return FALSE;
      CHandleMap* pMap = afxMapHGDIOBJ(TRUE); // create map if not exist
      ASSERT(pMap != NULL);
      pMap->SetPermanent(m_hObject = hObject, this);
      return TRUE;
}





Constructor...Destructor

IMPLEMENT_DYNAMIC(CAuftragsHistory_Lieferschein_Dialog, CDialog)
CAuftragsHistory_Lieferschein_Dialog::CAuftragsHistory_Lieferschein_Dialog(CWnd* pParent /*=NULL*/)
      : CDialog(CAuftragsHistory_Lieferschein_Dialog::IDD, pParent)
      , m_strLieferscheinNr(_T(""))
      , m_piAdoRecordBinding(NULL)
      , m_strConnection(_T(""))
      , m_strCmdText(_T(""))
      , mb_Kennzahl_first_show_up(false)
{
    m_fontUeberschrift=NULL;
    m_fontBold =NULL;
    m_fontSmall=NULL;
    m_hDevMode=NULL;
    m_hDevNames=NULL;
}



CAuftragsHistory_Lieferschein_Dialog::~CAuftragsHistory_Lieferschein_Dialog()
{
  delete m_fontUeberschrift;
  delete m_fontBold;
  delete m_fontSmall;
}











//#######################################################################################

void CAuftragsHistory_Lieferschein_Dialog::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
      
    pInfo->SetMaxPage(1);
    //--------------------------------------

      //--------------- Neue Schrift erstellen ------------------
       if(m_fontUeberschrift==NULL)
{
    m_fontUeberschrift->CreateFont(-40,
                                         0,
                                       0,
                                         0,
                                         600,
                                         0,
                                            0,
                                         0,
                                         DEFAULT_CHARSET,
                                         OUT_CHARACTER_PRECIS,
                                         CLIP_CHARACTER_PRECIS,
                                         DEFAULT_QUALITY,
                                         DEFAULT_PITCH|FF_DONTCARE,
                                         "Arial");
}
    //----------------------------------------------------------
 
}

//#######################################################################################
void CAuftragsHistory_Lieferschein_Dialog::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
      //Druckbereich in 0,1mm
                      pDC->SetMapMode(MM_LOMETRIC);
                              //obwohl dies schon in OnBeginPrinting steht
                              //ist es immer eine gute Idee es nochmal zuschreiben

                    //str_Datum_druck = aktuelle Datum
 
                           
                           CFont* pOldFont = pDC->SelectObject(m_fontUeberschrift);
                                                     
                           //--------------- Tag - Datum - KW -------------------
                           pDC->TextOut(100,-50,"Versuch");
                           pDC->TextOut(350,-50,"Versuch");
                           pDC->TextOut(600,-50,"Wilhelm Stockbrügger OHG");
                   //-------------------- Abmessung ---------------------
                           
                           pDC->SelectObject(pOldFont);
 
   
                   
                   
                        here comes later the delivery notice "Lieferschein"
                 
               
                 
     

                           //------------- Letzte Seite ermitteln ------------
                           UINT nEndPage = pInfo->m_nCurPage;
                           if(nEndPage==pInfo->GetMaxPage())
                           {
                               // pDC->SelectObject(pOldFont);
                                // pDC->TextOut(100,-600,"Letzte Seite");
                           }
                   //-------------------------------------------------
                 
                           //------------- Erste Seite ermitteln ------------
                           UINT nStartPage = pInfo->m_nCurPage;
                           if(nStartPage==pInfo->GetMinPage())
                           {
                                // pDC->SelectObject(pOldFont);
                               //  pDC->TextOut(100,-600,"Erste Seite");
                           }
                   //-------------------------------------------------

}

//#######################################################################################
void CAuftragsHistory_Lieferschein_Dialog::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift;

}
//#######################################################################################
bool CAuftragsHistory_Lieferschein_Dialog::GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{
      // if NULL is passed, then assume we are setting app object's
    // devmode and devnames
    if (phDevMode == NULL || phDevNames == NULL)
      {
            MessageBox("EPSON LQ-680Pro Port 2 Wiegekarte-1error");
        return FALSE;
      }


    // Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
      {
            MessageBox("EPSON LQ-680Pro Port 2 Wiegekarte-2error");
        return FALSE;
      }


    // obtain PRINTER_INFO_2 structure and close printer
    DWORD dwBytesReturned, dwBytesNeeded;
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,
        dwBytesNeeded);
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,&dwBytesReturned) == 0)
      {
       GlobalFree(p2);
       ClosePrinter(hPrinter);
       MessageBox("EPSON LQ-680Pro Port 2 Wiegekarte-3error");
       return FALSE;
    }
     ClosePrinter(hPrinter);

    // Allocate a global handle for DEVMODE
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    ASSERT(hDevMode);
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
    ASSERT(pDevMode);

    // copy DEVMODE data from PRINTER_INFO_2::pDevMode
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    GlobalUnlock(hDevMode);

    // Compute size of DEVNAMES structure from PRINTER_INFO_2's data
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name

    // Allocate a global handle big enough to hold DEVNAMES.
    HGLOBAL hDevNames = GlobalAlloc(GHND, sizeof(DEVNAMES) +(drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));
    ASSERT(hDevNames);
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
    ASSERT(pDevNames);

    // Copy the DEVNAMES information from PRINTER_INFO_2
    // tcOffset = TCHAR Offset into structure
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
    ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR));

    pDevNames->wDriverOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,drvNameLen*sizeof(TCHAR));
    tcOffset += drvNameLen;

    pDevNames->wDeviceOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,ptrNameLen*sizeof(TCHAR));
    tcOffset += ptrNameLen;

    pDevNames->wOutputOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,porNameLen*sizeof(TCHAR));
    pDevNames->wDefault = 0;

    GlobalUnlock(hDevNames);
    GlobalFree(p2);   // free PRINTER_INFO_2

    // set the new hDevMode and hDevNames
    *phDevMode = hDevMode;
    *phDevNames = hDevNames;
    return TRUE;
}

//#######################################################################################
HANDLE CAuftragsHistory_Lieferschein_Dialog::CopyHandle(HANDLE h)
{
      // Return a handle to a copy of the data
    // that the passed handle was for.
    if (!h) return NULL;

    BYTE*   lpCopy;
    BYTE*   lp;
    HANDLE   hCopy;
    DWORD   dwLen = GlobalSize(h);
    if (hCopy = GlobalAlloc(GHND, dwLen))
    {
        lpCopy = (BYTE*)GlobalLock(hCopy);
        lp     = (BYTE*)GlobalLock(h);

        CopyMemory(lpCopy,lp,dwLen);

        GlobalUnlock(hCopy);
        GlobalUnlock(h);
    }

    return hCopy;

}

//#######################################################################################


//#######################################################################################
void CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss(void)
{
      // if (GetPrinterDevice(_T("EPSON LQ-680Pro Port 2 Wiegekarte"), &hDevNames, &hDevMode))
      // HANDLE hDevMode;          m_hDevMode;
    // HANDLE hDevNames;         m_hDevNames;

     m_hDevMode=m_hDevNames=NULL;

       if (GetPrinterDevice(_T("\\\\DSERVER\\Brother HL-7050 series"), &m_hDevNames, &m_hDevMode)) // See KB article Q166129
      //       if (GetPrinterDevice(_T("PowerPDF 2005"), &hDevNames, &hDevMode)) // See KB article Q166129
     {
          AfxGetApp()->SelectPrinter(m_hDevNames, m_hDevMode);
          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
 
               //--------------------------- Papier Auswahl DEVMODE ----------------------------------
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {               
                    LPDEVMODE dev=dlgPrint.GetDevMode();
                 GlobalUnlock(dev);
                    // hier kann man alle Atribute von DEVMODE angeben (siehe Microsoft DEVMODE Blätter)
           
                   // dev->dmOrientation=DMORIENT_LANDSCAPE; //Querformat
              //dev->dmOrientation=DMORIENT_PORTRAIT;  //Hochformat
                   // dev->dmPaperSize=DMPAPER_A4;
                   // dev->dmCopies=1;
                   // dev->dmDefaultSource=0;
                   // dev->dmPaperSize=DMPAPER_A5;
               // dev->dmPaperLength=1020;
               // dev->dmPaperWidth=1480;
              //  dev->dmDefaultSource=DMBIN_TRACTOR;

                     dev->dmOrientation=DMORIENT_PORTRAIT;
               dev->dmPaperSize=DMPAPER_A4;
               dev->dmCopies=1;
               dev->dmDefaultSource=0;
                      
                //Wichtig !!!dmFields setzen
              dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                    
              }
 
              dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                   //---------------------------------------------------------------------------------
                // Force the CPrintDialog to use our device mode & name.
             dlgPrint.m_pd.hDevMode  = CopyHandle(m_hDevMode);
             dlgPrint.m_pd.hDevNames = CopyHandle(m_hDevNames);
           //===============================================================================================
             // if(dlgPrint.DoModal()==IDOK) //Ohne CPrintDialogBox
      //   {
            
               GlobalFree(m_hDevMode);
               GlobalFree(m_hDevNames);
               m_hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
               m_hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.

                     CDC dc;
                     HDC hDCPrint = dlgPrint.CreatePrinterDC();
                     //------------------
                     dc.Attach(hDCPrint);
                     //------------------
                     //dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;
                                  
                    //------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
                                      
                     BOOL bPrintingOK = dc.StartDoc(&Print);
                 CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 
               //~~~~~~~~~~~~~~~~~~~~~~~~~
                 OnBeginPrinting(&dc,&pInfo);  // call prepare
               //~~~~~~~~~~~~~~~~~~~~~~~~~


                     //==============================================================================
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     OnPrint(&dc,&pInfo); // call Print your Page
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //==============================================================================

                     //~~~~~~~~~~~~~~~~~~~~~~~~~
                   OnEndPrinting(&dc,&pInfo); // call your clean up
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
                     
                           dc.DeleteDC();
//
 // }// Ohne CPrintDialogBox

              GlobalFree(dlgPrint.m_pd.hDevMode);
              GlobalFree(dlgPrint.m_pd.hDevNames);
       }
                  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


 
        
}


 

//#######################################################################################

void CAuftragsHistory_Lieferschein_Dialog::OnBnClickedButton_Drucken()
{    

    int iLieferscheinNr=atoi(m_strLieferscheinNr);//MemberVariable
 
 
      Sleep(1000);
    //===============
    Drucken_weiss();
       Sleep(1000);
      if(iLieferscheinNr<200000) MessageBox(" Drucke Lieferschein " +m_strLieferscheinNr);
      if(iLieferscheinNr>200000) MessageBox(" Drucke Warenbegleitschein " +m_strLieferscheinNr);
 //#######################################################################################


 
 


Avatar of Zoppo
Zoppo
Flag of Germany image

Hi tsp2002,

you simply forgot to create the CFont instance - add the bold line from below:

void CAuftragsHistory_Lieferschein_Dialog::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
    //Druckbereich in 0,1mm
    pDC->SetMapMode(MM_LOMETRIC);
    //--------------------------------------
     
    pInfo->SetMaxPage(1);
    //--------------------------------------

    //--------------- Neue Schrift erstellen ------------------
    if(m_fontUeberschrift==NULL)
    {
    m_fontUeberschrift = new CFont;
    m_fontUeberschrift->CreateFont(-40,
                                         0,
                                       0,
                                         0,
                                         600,
                                         0,
                                            0,
                                         0,
                                         DEFAULT_CHARSET,
                                         OUT_CHARACTER_PRECIS,
                                         CLIP_CHARACTER_PRECIS,
                                         DEFAULT_QUALITY,
                                         DEFAULT_PITCH|FF_DONTCARE,
                                         "Arial");
    }
    //----------------------------------------------------------
 
}

Hope that helps,

ZOPPO
Avatar of Thomas Stockbruegger

ASKER

Hi Zoppo, your are right.
I changed that.


But I still got a crash...not allways but very often.
I even erased all font and all printing in OnPrint(CDC* pDC, CPrintInfo* pInfo)
so I only get a white paper....now it stop again here:

Call Stack:
KERNEL32DLL!77eaf870()

I don´t know why...please help, thanks Thomas
sorry...I was wrong....if I clean out everything for a white page and
clean out OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
so no new font....it runs until now wiht no crash....so I gues like Sara it comes from the font
please help, thanks....I would like to get this to work
Hm - hard to say, that's a lot of code. Could you tell where (in which function/line) in your code the crash happens?

BTW: I think the code as it is above (including the modification from my previous comment) will crash when the printing functionality is called more than once within the lifetime of the dialog since with 'OnEndPrinting' the font is deleted but not set to NULL so the 'OnBeginPrinting' won't create a new CFont instance, instead it uses the now invalid pointer. Change 'OnEndPrinting' to look like this:

void CAuftragsHistory_Lieferschein_Dialog::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift;
      m_fontUeberschrift = NULL;
}

ZOPPO
void CAuftragsHistory_Lieferschein_Dialog::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift;
      m_fontUeberschrift = NULL;
}
sorry I forgot...I did this allready
ok - then if it still crashs please tell where exactly in your code it crashs ...
Hello Zoppo,
there is no stop at my code.
I try to load some hardcopy that I made...but I thing the internet site will not work....loading a image....
from loading:An unexpected error occurred. Please try again later.


So I got the following window:
Unbehandelte Ausnahme bei 0x77eae566 in Stockbruegger.exe....
Zugriffsverletzung-Leseposition 0x0017fb54

and a second
für die aktuelle Position ist kein Quellcode verfügbar

and in dissasembly anzeigen

77EAE566   mov    al,byte ptr [ebx]
 
But is there maybe any function (from your code) call shown in the call stack below the current position?

One more problem I found is in this code:
  LPDEVMODE dev=dlgPrint.GetDevMode();
  GlobalUnlock(dev);

  ...

  dev->dmOrientation=DMORIENT_PORTRAIT;
  dev->dmPaperSize=DMPAPER_A4;
  dev->dmCopies=1;
  dev->dmDefaultSource=0;
                      
  //Wichtig !!!dmFields setzen
  dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;

Open in new window

The 'GlobalUnlock' is called to early - you must not access any data via 'dev' after you unlocked it.

Try this instead:
  LPDEVMODE dev=dlgPrint.GetDevMode();

  ...

  dev->dmOrientation=DMORIENT_PORTRAIT;
  dev->dmPaperSize=DMPAPER_A4;
  dev->dmCopies=1;
  dev->dmDefaultSource=0;
                      
  //Wichtig !!!dmFields setzen
  dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;

  GlobalUnlock(dev);

Open in new window


ZOPPO


you should differ between a crash like the one you had when using the pointer m_fontUeberschrift without doing a 'new CFont' before, and an ASSERT in one of windows functions.

the assertion in Attach function only happens when you create the same font a multiple time. in my opinion you should it create only once and never delete the pointer and never delete the object (resource) before destructing the view.

Sara
the exception you had is a crash and most probably a pointer error. if using member pointers you always need to initialize them with NULL in the constructor. also any local pointer should be initialized. and whenever you make a delete, initialize the pointer to NULL again. that is a must for member pointers.

Sara
I changed that....same result
But is there maybe any function (from your code) call shown in the call stack below the current position?



 Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::OnBeginPrinting(CDC * pDC=0x0012e914, CPrintInfo * pInfo=0x0012e898)  Zeile 2052      C++
 Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss()  Zeile 1978      C++
 StockbruStockbruegger.exe!CLieferscheine_Dialog::OnNMClickList1(tagNMHDR * pNMHDR=0x0012f85c, long * pResult=0x0012f63c)  Zeile 3658 + 0x1a      C++
egger.exe!CAuftragsHistory_Lieferschein_Dialog::OnBnClickedButton_Drucken()  Zeile 1647      C++
Stockbruegger.exe!CLieferscheine_Dialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 3592      C++
Stockbruegger.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00133952, int nCmdShow=1)  Zeile 25      C++
       Stockbruegger.exe!WinMainCRTStartup()  Zeile 390 + 0x39      C


OnBnClickedButton_Drucken()  will call Drucken_weis();

did you mean that?
ok - please when crash happened double-click the first line in the call stack where your code is mentioned, so here:
Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::OnBeginPrinting

Then the debugger should jump to the place in your code which leads to the crash ... please post this function and add a hint where exactly the debugger jumped to.
sorry Zoppo....that was the call stack before the font error.
now I have no line to click = only Call Stack:
KERNEL32DLL!77eaf870()
Hm - that's not good - are you sure the debugger hasn't accidentally switched to another thread? Open threads-window (Debug->Windows->Threads) and check that the 'Main Thread' is the current thread.

Without knowing which function really leads to the stack it's quite difficult - IMO you first should try to step through your code with the debugger to find if your code is crashing anywhere or if the crash happens after your code has run completeley. In the latter case it may be that something in your code somehow disturb something which later on leads to a crash.
Dear Zoppo,
I will try this tomorrow...Schönen Abend. Best regards Thomas
Hi Zoppo,
here are the hardcopys from yesterday...now I can load them up.

1.BMP
2.BMP
3.BMP
4.BMP
Could you post the complete stack too?
     KERNEL32.DLL!77e9bcb1()       
       KERNEL32.DLL!77e9bcb1()       
       oledb32.dll!1f891ff6()       
       oledb32.dll!1f895892()       
       oledb32.dll!1f895869()       
       oledb32.dll!1f89554a()       
       msvcr71d.dll!_CxxThrowException(void * pExceptionObject=0x0012dd94, const _s__ThrowInfo * pThrowInfo=0x006b023c)  + 0x39      C++
       Stockbruegger.exe!_com_raise_error(HRESULT hr=-2146825267, IErrorInfo * perrinfo=0x02ff8ff0)  Zeile 19      C++
       Stockbruegger.exe!_com_issue_errorex(HRESULT hr=-2146825267, IUnknown * punk=0x01d20f60, const _GUID & riid={...})  Zeile 67      C++
>      Stockbruegger.exe!ADOCG::Recordset15::MoveFirst()  Zeile 1531      C++       Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::Rechnung(ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > str_RechnungsNR={...}, ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > str_Typ={...}, ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<char> > > str_Guete={...}, int Zeile=1, double AuftragNr=0.00000000000000000)  Zeile 1451      C++
       Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::Lieferschein_Lesen()  Zeile 923      C++
       Stockbruegger.exe!CAuftragsHistory_Lieferschein_Dialog::OnTimer(unsigned int nIDEvent=1)  Zeile 1589      C++
       mfc71d.dll!CWnd::OnWndMsg(unsigned int message=275, unsigned int wParam=1, long lParam=0, long * pResult=0x0012f0ec)  Zeile 2027      C++
       mfc71d.dll!CWnd::WindowProc(unsigned int message=275, unsigned int wParam=1, long lParam=0)  Zeile 1745 + 0x1e      C++
       mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb9b40, HWND__ * hWnd=0x0006046e, unsigned int nMsg=275, unsigned int wParam=1, long lParam=0)  Zeile 241 + 0x1a      C++
       mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x0006046e, unsigned int nMsg=275, unsigned int wParam=1, long lParam=0)  Zeile 389      C++
       mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x0006046e, unsigned int nMsg=275, unsigned int wParam=1, long lParam=0)  Zeile 209 + 0x15      C++
       USER32.DLL!77e2a454()       
       USER32.DLL!77e04605()       
       mfc71d.dll!CWnd::AttachControlSite(CHandleMap * pMap=0x00fb6088)  Zeile 445 + 0x16      C++
       mfc71d.dll!CWnd::FromHandle(HWND__ * hWnd=0x000704f2)  Zeile 311      C++
       mfc71d.dll!CWnd::GetTopLevelParent()  Zeile 2623      C++
       00000001()      
       USER32.DLL!77e05b77()       
       mfc71d.dll!AfxInternalPumpMessage()  Zeile 188      C++
       mfc71d.dll!CWinThread::PumpMessage()  Zeile 916      C++
       mfc71d.dll!AfxPumpMessage()  Zeile 195 + 0xb      C++
       mfc71d.dll!CWnd::RunModalLoop(unsigned long dwFlags=4)  Zeile 4566 + 0x5      C++
       mfc71d.dll!CDialog::DoModal()  Zeile 527 + 0xc      C++
       Stockbruegger.exe!CLieferscheine_Dialog::OnNMClickList1(tagNMHDR * pNMHDR=0x0012f85c, long * pResult=0x0012f63c)  Zeile 3658 + 0x1a      C++
       mfc71d.dll!_AfxDispatchCmdMsg(CCmdTarget * pTarget=0x00fb8be8, unsigned int nID=1000, int nCode=65534, void (void)* pfn=0x004cd6bb, void * pExtra=0x0012f540, unsigned int nSig=57, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 119      C++
       mfc71d.dll!CCmdTarget::OnCmdMsg(unsigned int nID=1000, int nCode=65534, void * pExtra=0x0012f540, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 396 + 0x27      C++
       mfc71d.dll!CDialog::OnCmdMsg(unsigned int nID=1000, int nCode=5177342, void * pExtra=0x0012f540, AFX_CMDHANDLERINFO * pHandlerInfo=0x00000000)  Zeile 88 + 0x18      C++
       mfc71d.dll!CWnd::OnNotify(unsigned int __formal=1000, long lParam=1243228, long * pResult=0x0012f63c)  Zeile 2576      C++
       mfc71d.dll!CWnd::OnWndMsg(unsigned int message=78, unsigned int wParam=1000, long lParam=1243228, long * pResult=0x0012f670)  Zeile 1771 + 0x28      C++
       mfc71d.dll!CWnd::WindowProc(unsigned int message=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 1745 + 0x1e      C++
       mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb8be8, HWND__ * hWnd=0x000504e6, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 241 + 0x1a      C++
       mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x000504e6, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 389      C++
       mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x000504e6, unsigned int nMsg=78, unsigned int wParam=1000, long lParam=1243228)  Zeile 209 + 0x15      C++
       USER32.DLL!77e2a454()       
       USER32.DLL!77e06382()       
       USER32.DLL!77e07361()       
       comctl32.dll!71715c1d()       
       NTDLL.DLL!77891baf()       
       comctl32.dll!71738e84()       
       comctl32.dll!71737085()       
       comctl32.dll!717388c5()       
       comctl32.dll!71738b08()       
       comctl32.dll!717171c3()       
       mfc71d.dll!AfxUnlockGlobals(int nLockType=7)  Zeile 112      C++
       mfc71d.dll!CWnd::OnWndMsg(unsigned int message=2082666204, unsigned int wParam=513, long lParam=1, long * pResult=0x003f01ca)  Zeile 1816      C++
       0012faec()      
       mfc71d.dll!AfxCallWndProc(CWnd * pWnd=0x00fb8c5c, HWND__ * hWnd=0x000404e4, unsigned int nMsg=513, unsigned int wParam=1, long lParam=4129226)  Zeile 241 + 0x1a      C++
       mfc71d.dll!AfxWndProc(HWND__ * hWnd=0x000404e4, unsigned int nMsg=513, unsigned int wParam=1, long lParam=4129226)  Zeile 389      C++
       mfc71d.dll!AfxWndProcBase(HWND__ * hWnd=0x000404e4, unsigned int nMsg=513, unsigned int wParam=1, long lParam=4129226)  Zeile 209 + 0x15      C++
       USER32.DLL!77e2a454()       
       USER32.DLL!77e04605()       
       USER32.DLL!77e07361()       
       USER32.DLL!77e0a7f2()       
       USER32.DLL!77e1ecf3()       
       USER32.DLL!77e1f46b()       
       mfc71d.dll!CWnd::IsDialogMessageA(tagMSG * lpMsg=0x00136a48)  Zeile 200      C++
       mfc71d.dll!CWnd::PreTranslateInput(tagMSG * lpMsg=0x00136a48)  Zeile 4512      C++
       mfc71d.dll!CDialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 83      C++
       Stockbruegger.exe!CLieferscheine_Dialog::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 3592      C++
       mfc71d.dll!CWnd::WalkPreTranslateTree(HWND__ * hWndStop=0x000704f2, tagMSG * pMsg=0x00136a48)  Zeile 3129 + 0x12      C++
       mfc71d.dll!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 238 + 0x12      C++
       mfc71d.dll!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 795 + 0x9      C++
       mfc71d.dll!AfxPreTranslateMessage(tagMSG * pMsg=0x00136a48)  Zeile 257 + 0xf      C++
       mfc71d.dll!AfxInternalPumpMessage()  Zeile 183 + 0x18      C++
       mfc71d.dll!CWinThread::PumpMessage()  Zeile 916      C++
       mfc71d.dll!CWinThread::Run()  Zeile 637 + 0xb      C++
       mfc71d.dll!CWinApp::Run()  Zeile 701      C++
       mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00133952, int nCmdShow=1)  Zeile 49 + 0xb      C++
       Stockbruegger.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00133952, int nCmdShow=1)  Zeile 25      C++
       Stockbruegger.exe!WinMainCRTStartup()  Zeile 390 + 0x39      C
       KERNEL32.DLL!77e989d5()       




inline HRESULT Recordset15::MoveFirst ( ) {
    HRESULT _hr = raw_MoveFirst();
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _hr; <----------- green arrow here}



Hi Zoppo,
I am sorry...it is not my day.
Now.. I got an error when I call my window (the window where I print with)
I don´t know what is so wrong…can you help, thank you.

I tried again...and now I can print about 20 white pages....I go back between the windows and print print...
Suddenly the error comes with no call stack only=KERNEL32DLL!77eaf870()
 Please be so kind to help, thank you


1.BMP
it is a com error when accessing the database in function:
Stockbruegger.exe!ADOCG::Recordset15::MoveFirst()  Zeile 1531

it looks like as you did get an error when connecting to db or loading a recordset but ignored the error and tried to move within the recordset (== result of your query) nevertheless.

can you post the source where you access the database?

Sara
when you suddenly encounter strange errors which occur only sometimes and at different locations, it mostly is cause you worked on corrupted memory somehow. such corruption often happened when using uninitialized pointers or use pointers after delete or access arrays beyond array bounds.

so it is important to initialize each pointer (array) you were using. always allocate memory to a pointer with new before use but also check that it is NULL before assigning a new pointer value. after delete, set the pointer to NULL such that you directly see when your code was using the pointer again.

Sara
when the exception code is 0xc00000005 access violation, it is for 99 percent of all cases a pointer error, such that using a NULL pointer or pointer after delete.

unfortunately the default of exception handling by the debugger is to first throw the exception and hope it will be caught by a try-catch-block somewhere. if there is no such handling the debugger finally breaks in some kernel function. for debugging purposes it is in my opinion always better when the debugger breaks immediately when an invalid pointer was detected. you can force the debugger to do so by going to menu Debug - Exceptions ... - Win32 Exceptions (your local version of visual studio should have Debug - Ausnahmen ... -  Win32 Exceptions) and check the box right of c000005 - Access violation. that will cause the debugger to break earlier next time when the exception happened such that the call stack might show the code where the error occured.

same you could do for the com error which is in section C++ Exceptions though i think the com error wasn't the reason for the crash but happened only because the memory already was corrupted.

Sara  
Sara,
I think you mean that....strange....I printed 20 white pages...no error
I will try so long as I have a stop....let you know soon
1.BMP
2.BMP
Sara, I had just some breakdowns....but nothing in call stack   only
KERNEL32DLL!77eaf870()
Where come the error from?
The crash only happens after printing....even a white page....

So is the pointer error etc. only from the printing code? Or could that be that only after my printing code
the program crash down even the pointer error comes from any other places in my code?

I did printing before like this (with a Dialogbox) and so far I had no error like this
Could that be that this comes from GetPrinterDevice?
in win32-exceptions you need to select the 0xc000005 access violation before checking the radio box "wenn die Asunahme ausgeloest wird ... In den Debugger springen".

please post your current code regarding printing.

Sara
any hint from this?
1.BMP
2.BMP
3.BMP
This is a printing with Dialogbox from a other dialog with no error....but I would like to print without a printerdialog box and to print 3 copies = 3 tray    white   red   green.....so I guess I have to use GetPrinterDevice






        CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE,this);
      //-------------- Eigenschaftenbutton PrintDialog disable -----------  
        dlgPrint.m_pd.Flags |= PD_ENABLEPRINTHOOK;
      dlgPrint.m_pd.lpfnPrintHook = PrintHookProc3;
      //-----------------------------------------------------------------

      
      if(dlgPrint.DoModal()==IDOK)
      {
            CDC dc;
            dc.Attach(dlgPrint.GetPrinterDC());
            dc.m_bPrinting=TRUE;
      
        //-------------------------- DEVMODE ---------------------------------------
             
         LPDEVMODE dev=dlgPrint.GetDevMode();
            
         GlobalUnlock(dev);
//            if(m_bDruckenListe==false) dev->dmOrientation=DMORIENT_PORTRAIT;  //Hochformat
            //if(m_bDruckenListe==true) dev->dmOrientation=DMORIENT_LANDSCAPE;  //Querformat
             dev->dmOrientation=DMORIENT_LANDSCAPE;  //Querformat

         dev->dmPaperSize=DMPAPER_A4;
     //  dev->dmPaperSize=DMPAPER_A5;
       //  dev->dmPaperLength=1020;
       //  dev->dmPaperWidth=1480;
           dev->dmCopies=dlgPrint.GetCopies();  
           dev->dmDefaultSource=DMBIN_AUTO;
             //Wichtig !!!dmFields setzen
         dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
            // dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE|
                            //   DM_SCALE|DM_COPIES|DM_PRINTQUALITY|DM_POSITION;
             dc.ResetDC(dev);
             //GlobalUnlock(dev);
             dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
 
 
              //-------------------------------------------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
           BOOL bPrintingOK = dc.StartDoc(&Print);
           CPrintInfo pInfo;
           pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
         
         //~~~~~~~~~~~~~~~~~~~~~~~~~
           OnBeginPrinting(&dc,&pInfo);  // call prepare
         //~~~~~~~~~~~~~~~~~~~~~~~~~
               //==============================================================================
             for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
           {
             //begin a new page
                 dc.StartPage();
             pInfo.m_nCurPage=page;
             //~~~~~~~~~~~~~~~~~~~~~~~~~
               OnPrint(&dc,&pInfo); // call Print your Page
             //~~~~~~~~~~~~~~~~~~~~~~~~~
               bPrintingOK=(dc.EndPage()>0);
             }
           //==============================================================================
                //~~~~~~~~~~~~~~~~~~~~~~~~~
            OnEndPrinting(&dc,&pInfo); // call your clean up
            //~~~~~~~~~~~~~~~~~~~~~~~~~
                 if(bPrintingOK)
                {
              dc.EndDoc();
                }
                 else
                {
                 dc.AbortDoc();
                }
                     
                dc.DeleteDC();
      }
      
}
//############################################################################################
in win32-exceptions you need to select the 0xc000005 access violation before checking the radio box "wenn die Asunahme ausgeloest wird ... In den Debugger springen".

I did this...but the only thing I get in call stack is
KERNEL32DLL!77eaf870()
Hm - in the screenshot with the threads you can see the function where it happens (CLieferscheine_Dialog::OnPrint). It's strange that debugger doesn't show this function in the call stack too.

In the last code you posted you again have the 'GlobalUnlock(dev);' in the wrong place before accessing 'dev' members.
I guess this is important!!!!!!!

To all, one thing I found out :
From OnBnClickedButton_Drucken() I will call Drucken_weiss();
To start the printing

When I put a breakbpoint to the Sleep(1000); behind Drucken_weiss();
And the crash happened it breaks somewhere in Drucken_weiss(); because there is not stop at my breakpoint

I put a breakpoint here:  GlobalFree(dlgPrint.m_pd.hDevNames); in Drucken_weiss();
And it stops here after that it breaks and goes not back to OnBnClickedButton_Drucken()----Sleep(1000)

//#######################################################################################

void CAuftragsHistory_Lieferschein_Dialog::OnBnClickedButton_Drucken()
 

    int iLieferscheinNr=atoi(m_strLieferscheinNr);//MemberVariable
 
 
      Sleep(1000);
    //===============
      Drucken_weiss();       
Sleep(1000);  //breakpoint here}

      if(iLieferscheinNr<200000) MessageBox(" Drucke Lieferschein " +m_strLieferscheinNr);
      if(iLieferscheinNr>200000) MessageBox(" Drucke Warenbegleitschein " +m_strLieferscheinNr);
    //===============
     

//#######################################################################################




void CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss(void)
{
      

      m_hDevMode=m_hDevNames=NULL;  //<-----------

       if (GetPrinterDevice(_T("\\\\DSERVER\\Brother HL-7050 series"), &m_hDevNames, &m_hDevMode))  
                 
     {
          AfxGetApp()->SelectPrinter(m_hDevNames, m_hDevMode);
          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
 
               //--------------------------- Papier Auswahl DEVMODE ----------------------------------
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {               
                    LPDEVMODE dev=dlgPrint.GetDevMode();
      

                     dev->dmOrientation=DMORIENT_PORTRAIT;
               dev->dmPaperSize=DMPAPER_A4;
               dev->dmCopies=1;
               dev->dmDefaultSource=0;
                      
                //Wichtig !!!dmFields setzen
              dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                    GlobalUnlock(dev);
              }
 
              dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                   //---------------------------------------------------------------------------------
                // Force the CPrintDialog to use our device mode & name.
             dlgPrint.m_pd.hDevMode  = CopyHandle(m_hDevMode);
             dlgPrint.m_pd.hDevNames = CopyHandle(m_hDevNames);
           //===============================================================================================
             // if(dlgPrint.DoModal()==IDOK) //Ohne CPrintDialogBox
      //   {
            
               GlobalFree(m_hDevMode);
               GlobalFree(m_hDevNames);
             m_hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
             m_hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.

                     CDC dc;
                     HDC hDCPrint = dlgPrint.CreatePrinterDC();
                     //------------------
                     dc.Attach(hDCPrint);
                     //------------------
                     //dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;
                                  
                    //------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
                                      
                     BOOL bPrintingOK = dc.StartDoc(&Print);
                 CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 
               //~~~~~~~~~~~~~~~~~~~~~~~~~
                 OnBeginPrinting(&dc,&pInfo);  // call prepare
               //~~~~~~~~~~~~~~~~~~~~~~~~~


                     //==============================================================================
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     OnPrint(&dc,&pInfo); // call Print your Page
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //==============================================================================

                     //~~~~~~~~~~~~~~~~~~~~~~~~~
                   OnEndPrinting(&dc,&pInfo); // call your clean up
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
                     
                           dc.DeleteDC();
//
 // }// Ohne CPrintDialogBox

              GlobalFree(dlgPrint.m_pd.hDevMode);
              GlobalFree(dlgPrint.m_pd.hDevNames); breakpoint here      
 }
                  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


 
        
}


 

//#######################################################################################
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Is this okay????


protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV-Unterstützung

     DECLARE_MESSAGE_MAP()

    HANDLE m_hDevMode;
     HANDLE m_hDevNames;



IMPLEMENT_DYNAMIC(CAuftragsHistory_Lieferschein_Dialog, CDialog)
CAuftragsHistory_Lieferschein_Dialog::CAuftragsHistory_Lieferschein_Dialog(CWnd* pParent /*=NULL*/)
      : CDialog(CAuftragsHistory_Lieferschein_Dialog::IDD, pParent)
      , m_strLieferscheinNr(_T(""))
      , m_piAdoRecordBinding(NULL)
      , m_strConnection(_T(""))
      , m_strCmdText(_T(""))
      , mb_Kennzahl_first_show_up(false)
{
   
       m_hDevMode=NULL;
       m_hDevNames=NULL;
}





void CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss(void)
{
 

       if (GetPrinterDevice(_T("\\\\DSERVER\\Brother HL-7050 series"), &m_hDevNames, &m_hDevMode)) //  
     {
          AfxGetApp()->SelectPrinter(m_hDevNames, m_hDevMode);
          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
 
               //--------------------------- Papier Auswahl DEVMODE ----------------------------------
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {               
                    LPDEVMODE dev=dlgPrint.GetDevMode();
      

                     dev->dmOrientation=DMORIENT_PORTRAIT;
               dev->dmPaperSize=DMPAPER_A4;
               dev->dmCopies=1;
               dev->dmDefaultSource=0;
                      
                //Wichtig !!!dmFields setzen
              dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                    GlobalUnlock(dev);
              }
 
              dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                   //---------------------------------------------------------------------------------
                // Force the CPrintDialog to use our device mode & name.
             dlgPrint.m_pd.hDevMode  = CopyHandle(m_hDevMode);
             dlgPrint.m_pd.hDevNames = CopyHandle(m_hDevNames);
           //===============================================================================================
             // if(dlgPrint.DoModal()==IDOK) //Ohne CPrintDialogBox
      //   {
            
 
             m_hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
             m_hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.

                     CDC dc;
                     HDC hDCPrint = dlgPrint.CreatePrinterDC();
                     //------------------
                     dc.Attach(hDCPrint);
                     //------------------
                     //dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;
                                  
                    //------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
                                      
                     BOOL bPrintingOK = dc.StartDoc(&Print);
                 CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 
               //~~~~~~~~~~~~~~~~~~~~~~~~~
                 OnBeginPrinting(&dc,&pInfo);  // call prepare
               //~~~~~~~~~~~~~~~~~~~~~~~~~


                     //==============================================================================
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     OnPrint(&dc,&pInfo); // call Print your Page
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //==============================================================================

      
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
                    //~~~~~~~~~~~~~~~~~~~~~~~~~
                           OnEndPrinting(&dc,&pInfo); // call your clean up
                            //~~~~~~~~~~~~~~~~~~~~~~~~~
                           dc.DeleteDC();
//
 // }// Ohne CPrintDialogBox

            
              GlobalFree(dlgPrint.m_pd.hDevMode);
              GlobalFree(dlgPrint.m_pd.hDevNames);

              GlobalFree(m_hDevMode);
          GlobalFree(m_hDevNames);
      }
                  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


 

        
}


 

//#######################################################################################
IMO that looks ok - does it still crash? If so, did you try to watch resources in TaskManage?
IMO that looks ok - does it still crash? If so, did you try to watch resources in TaskManage?

I printed about 20 white pages with nothing on
so far no error....I will try many many more tomorrow...vielen Dank
Schönen Feierabend, Thomas
ok, that's fine - let's hope :o)

i think we are back at the point where you would need to generally overwork the printing concept. your Drucken_weiss function doesn't fit to the mfc way of printing.

as told in a previous question mfc doesn't do the whole printing in one handler function what would freeze the screen while printing cause no more messages could be processed. instead you only give an impact for printing and the mfc framework would call your printing functions like OnPreparePrinting, OnBeginPrinting, OnPrint, OnEndPrinting. for example the framework would call the OnPrint for each page separatly what would give the program a chance to process other messages between the different calls such that the program keeps responsive.

the Drucken_weiss function calls the OnBeginPrinting and the OnPrint itself, what could lead to unexpected behavior and also could be responsible for the crash (though i can't spot the wrong statement which actually causes the problems).

some of your code is indeed strange because of the wrong architecture. for example the members m_hDevMode and m_hDevNames which your current code actually doesn't need.  you need members if you have to save variables beyond on single call of a member function. that would be if mfc calls OnPreparePrinting (where you should instantiate the printer device) and then you would need that device in the other OnPrint... functions.

Or the calls of StartDoc and EndDoc. those were supposed to start and end the printing process controlled by the mfc framework. in your current code those calls only could make things worse cause they would start and end functionality that not really was required as you do control the printing yourself.

Did you try to debug your code as suggested? aside of the architecture thing i am still thinking that your code uses a wrong pointer somehow and debugging is the way to find those errors.

Sara
that would be if mfc calls OnPreparePrinting (where you should instantiate the printer device) and then you would need that device in the other OnPrint... functions.


Or the calls of StartDoc and EndDoc. those were supposed to start and end the printing process controlled by the mfc framework. in your current code those calls only could make things worse cause they would start and end functionality that not really was required as you do control the printing yourself.

Sara...could you help with a code sample...how should I do this...sorry printing is not my thing...I am happy if it works.


i think we are back at the point where you would need to generally overwork the printing concept that would take to long for me and is to difficult....I am happy that I even can print...I hope you understand....I do this coding beside my normal work at our office...and sometimes it is very stressy.
it is not so difficult. you normally would use a sample project from mfc samples and copy paste the functions. then replace the individual code as retrieving the data or print all three copies.

the main problem with your current code is that it calls framework functions whitout the right context. for example your overload of the OnPreparePrinting of mfc should call DoPreparePrinting and return . then the framework would call OnBeginPrinting.

if you want to go on with your current code i would rename all the printing functions such there is no mismatch. for example call the print function Print and not OnPrint. then drop all calls where you don't have own sourcecode, like the calls to StartDoc and EndDoc and probably the OnBeginPrinting. those function calls caan't help you with your current approach but only make things worse. forget all the member variables which only make sense if the printing was done with multiple calls from framework.

Sara
Sara...something like this?

 if you want to go on with your current code i would rename all the printing functions such there is no mismatch.for example call the print function Print and not OnPrint. then drop all calls where you don't have own sourcecode, like the calls to StartDoc and EndDoc and probably the OnBeginPrinting

void CAuftragsHistory_Lieferschein_Dialog::Drucken_weiss(void)
{
 

       if (GetPrinterDevice(_T("\\\\DSERVER\\Brother HL-7050 series"), &m_hDevNames, &m_hDevMode)) //  
     {
          AfxGetApp()->SelectPrinter(m_hDevNames, m_hDevMode);
          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);
 
               //--------------------------- Papier Auswahl DEVMODE ----------------------------------
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {              
                    LPDEVMODE dev=dlgPrint.GetDevMode();
       

                     dev->dmOrientation=DMORIENT_PORTRAIT;
               dev->dmPaperSize=DMPAPER_A4;
               dev->dmCopies=1;
               dev->dmDefaultSource=0;
                       
                //Wichtig !!!dmFields setzen
              dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                    GlobalUnlock(dev);
              }
 
              dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                   //---------------------------------------------------------------------------------
                // Force the CPrintDialog to use our device mode & name.
             dlgPrint.m_pd.hDevMode  = CopyHandle(m_hDevMode);
             dlgPrint.m_pd.hDevNames = CopyHandle(m_hDevNames);
           //============================================================================
             // if(dlgPrint.DoModal()==IDOK) //Ohne CPrintDialogBox
      //   {
           
 
             m_hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
             m_hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.

                     CDC dc;
                     HDC hDCPrint = dlgPrint.CreatePrinterDC();
                     //------------------
                     dc.Attach(hDCPrint);
                     //------------------
                     //dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;
                                   
                    //------------------------------------
               DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
                                 



   
     without this         //Rem          BOOL bPrintingOK = dc.StartDoc(&Print);      




                CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 







               //~~~~~~~~~~~~~~~~~~~~~~~~~
 without this              // Rem   OnBeginPrinting(&dc,&pInfo);    // call prepare
                //~~~~~~~~~~~~~~~~~~~~~~~~~






                     //===========================================================
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     On_MY_Print(&dc,&pInfo); // call Print your Page  
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //=============================================




        without this
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
   











                //~~~~~~~~~~~~~~~~~~~~~~~~~
                           On_MY_EndPrinting(&dc,&pInfo); // call your clean up      
                  //~~~~~~~~~~~~~~~~~~~~~~~~~
                           dc.DeleteDC();//
 // }// Ohne CPrintDialogBox

           
              GlobalFree(dlgPrint.m_pd.hDevMode);
              GlobalFree(dlgPrint.m_pd.hDevNames);

              GlobalFree(m_hDevMode);
             GlobalFree(m_hDevNames);       }
           //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


 

       
}


i also would make the m_hDevMode and m_hDevNames local variables and pass them to On_My_Print as arguments. the code looks better though i would assume you still didn't get an output?

Sara
Goodmorning Zoppo,
I did the test with the TaskManager
The only thing that is rising per one click print is the Handles (2 per click)
I started with

173 Handles<------------------ this will standy rise 2 per printing click....is this good or bad?
3     Threads
130 User Ob
61   Gdi Ob



Please let me know...untill now no crash
Thank you
Best regards,
Thomas
....Sara....you still didn't get an output? yes... will not work

do you write any data in the On_My_Print to the dc (device context) of the printer?

note, the idea behind printing in mfc is to exchange screen dc with printer dc such that anything you see on screen in your view now is put to paper.

is that what you want or do you have a printer output that is different from the view contents?

Sara
is that what you want or do you have a printer output that is different from the view contents?
I don´t understand your question.

I will later print a delivery paper "Lieferschein"....that is displayed on a Listenelement ....I will go through every row and print a delievery paper



What is about the HANDLES  they are increasing 2 per click when I print...is this good or bad?
handles are pointers to windows resources (where there are pretty much cause you need at least one handle for any windows object, such as windows, controls, bitmaps, files, fonts, ..., ).

as far as i know windows tries to create new handle numbers as long as possible before it reuses old freed handles. that way an invalid use of a freed handle can be detected rather than a wrong one be used.

so your observation seems ok for me.

Sara
Thank you all for your great help...untill now I had no crash
I guess the  the 'GlobalFree' calls to the end of the function to ensure they are cleaned up correctly. was the trick

Thank you very much.
Have a great weekend.
Best regards,
Thomas
Fine - I'm glad we could help.

Have a nice weekend too ...