Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DocumentProperties() cause a First-chance exception

DocumentProperties() cause a First-chance exception

Scheduled Pinned Locked Moved C / C++ / MFC
saleshelptutorialannouncement
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drugodrf
    wrote on last edited by
    #1

    In an application i wrote years ago, i need to have a handle to printer propertiers device mode structure; i searched documents and i finded this function (DocumentProperties()); i searched example, i tried to use it, i writed code and the application works yet. Now i have to write similar application and i note when i call this function, it cause a first-chance exception (only when i call it to know the size of buffer); so i try with the old application and also that cause the exception; i try to solve it, but the the example i find and the documentation i have tell me that the use of the function i make it's correct; the funny fact is this method works very well in release mode and with the old application the customer don't have problems. The code is this:

    BOOL PrinterSettings(LPTSTR pszPrinterName, BOOL bSet)
    {
    if (_tcslen(pszPrinterName) <= 0)
    return FALSE;

    if (bSet && theApp.m\_hBarcodePrinterDev == NULL)
    	return FALSE;
    
    PRINTER\_DEFAULTS pd;
    DWORD			 dwNeeded;
    LONG			 lFlag;
    HANDLE			 hPrinter     = NULL;
    DEVMODE FAR\*	 pDevMode     = NULL;
    PRINTER\_INFO\_2\*	 pPrintInfo2  = NULL;
    BOOL			 bReturn      = TRUE;
    
    ZeroMemory(&pd, sizeof(pd));
    pd.DesiredAccess = PRINTER\_ALL\_ACCESS;
    
    bReturn = OpenPrinter(pszPrinterName, &hPrinter, &pd);
    
    if (bReturn)
    {
    	SetLastError(0);
    	bReturn = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
    	
    	if (!bReturn && (GetLastError() == ERROR\_INSUFFICIENT\_BUFFER) && dwNeeded > 0)
    		bReturn = TRUE;
    }
    
    if (bReturn && (dwNeeded <= 0))
    	bReturn = FALSE;
    
    if (bReturn)
    	pPrintInfo2 = (PRINTER\_INFO\_2 \*) GlobalAlloc (GPTR, dwNeeded);
    
    if (bReturn && pPrintInfo2 == NULL)
    	bReturn = FALSE;
    
    if (bReturn)
    	bReturn = GetPrinter(hPrinter, 2, (LPBYTE) pPrintInfo2, dwNeeded, &dwNeeded);
    
    if (bReturn && pPrintInfo2->pDevMode == NULL)
    {
                // Here cause exception
    	dwNeeded = DocumentProperties(NULL, hPrinter, pszPrinterName, NULL, NULL, 0);
    
    	if (dwNeeded <= 0)
    		bReturn = FALSE;
    
    	if (bReturn)
    		pDevMode = (DEVMODE FAR\*) GlobalAlloc(GHND, dwNeeded);
    
    	if (bReturn && pDevMode == NULL)
    		bReturn = FALSE;
    
    	if (bReturn)
    	{
    		lFlag = DocumentProperties(NULL, hPrinter, pszPrinterName, pDevMode, NULL,DM\_OUT\_BUFFER);
    
    		if (lFlag != IDOK || pDevMode == NULL)
    			bReturn = FALSE;
    	}
    
    	if (bReturn)
    		pPrintInfo2->pDevMode = pDevMode;
    }
    
    if (
    
    L 1 Reply Last reply
    0
    • D Drugodrf

      In an application i wrote years ago, i need to have a handle to printer propertiers device mode structure; i searched documents and i finded this function (DocumentProperties()); i searched example, i tried to use it, i writed code and the application works yet. Now i have to write similar application and i note when i call this function, it cause a first-chance exception (only when i call it to know the size of buffer); so i try with the old application and also that cause the exception; i try to solve it, but the the example i find and the documentation i have tell me that the use of the function i make it's correct; the funny fact is this method works very well in release mode and with the old application the customer don't have problems. The code is this:

      BOOL PrinterSettings(LPTSTR pszPrinterName, BOOL bSet)
      {
      if (_tcslen(pszPrinterName) <= 0)
      return FALSE;

      if (bSet && theApp.m\_hBarcodePrinterDev == NULL)
      	return FALSE;
      
      PRINTER\_DEFAULTS pd;
      DWORD			 dwNeeded;
      LONG			 lFlag;
      HANDLE			 hPrinter     = NULL;
      DEVMODE FAR\*	 pDevMode     = NULL;
      PRINTER\_INFO\_2\*	 pPrintInfo2  = NULL;
      BOOL			 bReturn      = TRUE;
      
      ZeroMemory(&pd, sizeof(pd));
      pd.DesiredAccess = PRINTER\_ALL\_ACCESS;
      
      bReturn = OpenPrinter(pszPrinterName, &hPrinter, &pd);
      
      if (bReturn)
      {
      	SetLastError(0);
      	bReturn = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
      	
      	if (!bReturn && (GetLastError() == ERROR\_INSUFFICIENT\_BUFFER) && dwNeeded > 0)
      		bReturn = TRUE;
      }
      
      if (bReturn && (dwNeeded <= 0))
      	bReturn = FALSE;
      
      if (bReturn)
      	pPrintInfo2 = (PRINTER\_INFO\_2 \*) GlobalAlloc (GPTR, dwNeeded);
      
      if (bReturn && pPrintInfo2 == NULL)
      	bReturn = FALSE;
      
      if (bReturn)
      	bReturn = GetPrinter(hPrinter, 2, (LPBYTE) pPrintInfo2, dwNeeded, &dwNeeded);
      
      if (bReturn && pPrintInfo2->pDevMode == NULL)
      {
                  // Here cause exception
      	dwNeeded = DocumentProperties(NULL, hPrinter, pszPrinterName, NULL, NULL, 0);
      
      	if (dwNeeded <= 0)
      		bReturn = FALSE;
      
      	if (bReturn)
      		pDevMode = (DEVMODE FAR\*) GlobalAlloc(GHND, dwNeeded);
      
      	if (bReturn && pDevMode == NULL)
      		bReturn = FALSE;
      
      	if (bReturn)
      	{
      		lFlag = DocumentProperties(NULL, hPrinter, pszPrinterName, pDevMode, NULL,DM\_OUT\_BUFFER);
      
      		if (lFlag != IDOK || pDevMode == NULL)
      			bReturn = FALSE;
      	}
      
      	if (bReturn)
      		pPrintInfo2->pDevMode = pDevMode;
      }
      
      if (
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What is the exception and where does it occur?

      D 1 Reply Last reply
      0
      • L Lost User

        What is the exception and where does it occur?

        D Offline
        D Offline
        Drugodrf
        wrote on last edited by
        #3

        Whene the execution arrives in one of two lines i signed, compiler write: First-chance exception in MyApp.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception. First-chance exception in MyApp.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception. but the function works normally. Thanks Drugo

        L 1 Reply Last reply
        0
        • D Drugodrf

          Whene the execution arrives in one of two lines i signed, compiler write: First-chance exception in MyApp.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception. First-chance exception in MyApp.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception. but the function works normally. Thanks Drugo

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          These are not compiler messages, they look more like the messages you see in the debug log and can be ignored. Unless you get an exception inside your program, which causes the application to fail, you do not need to take any action.

          D 1 Reply Last reply
          0
          • L Lost User

            These are not compiler messages, they look more like the messages you see in the debug log and can be ignored. Unless you get an exception inside your program, which causes the application to fail, you do not need to take any action.

            D Offline
            D Offline
            Drugodrf
            wrote on last edited by
            #5

            ... and what do you think could to be due this message ? Thanks Drugo

            L 1 Reply Last reply
            0
            • D Drugodrf

              ... and what do you think could to be due this message ? Thanks Drugo

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              They are written to the debug log by the debugger; they are not being sent to the application. These exceptions are caught and handled by the system libraries.

              D 1 Reply Last reply
              0
              • L Lost User

                They are written to the debug log by the debugger; they are not being sent to the application. These exceptions are caught and handled by the system libraries.

                D Offline
                D Offline
                Drugodrf
                wrote on last edited by
                #7

                I try to change printer and now i don't have no more exception. Thank you so much for your answer. Bye Drugo

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups