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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetPrinter function fails to retrieves the printer information. [modified]

GetPrinter function fails to retrieves the printer information. [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 3 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.
  • G Offline
    G Offline
    Gokul_md
    wrote on last edited by
    #1

    Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul

    modified on Tuesday, October 14, 2008 4:08 AM

    CPalliniC D 2 Replies Last reply
    0
    • G Gokul_md

      Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul

      modified on Tuesday, October 14, 2008 4:08 AM

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Why do you check dwNeeded instead of GetPrinter return value? What is GetLastError return value when GetPrinter fails? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      G 1 Reply Last reply
      0
      • CPalliniC CPallini

        Why do you check dwNeeded instead of GetPrinter return value? What is GetLastError return value when GetPrinter fails? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        G Offline
        G Offline
        Gokul_md
        wrote on last edited by
        #3

        The Get Printer function will return required buffer size in dwNeeded. I hope this should always be greater than zero. So I checked with the buffer size. When I am trying to print the last error code it is printed nothing (empty).

        CPalliniC 1 Reply Last reply
        0
        • G Gokul_md

          The Get Printer function will return required buffer size in dwNeeded. I hope this should always be greater than zero. So I checked with the buffer size. When I am trying to print the last error code it is printed nothing (empty).

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          I would check the GetPrinter return value, and call GetLastError only when it fails. Are you aware that (MSDN [^]): Remarks Security Alert The pDevMode member in the PRINTER_INFO_2, PRINTER_INFO_8, and PRINTER_INFO_9 structures can be NULL. When this happens, the printer is unusable until the driver is reinstalled successfully. ? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • G Gokul_md

            Hi Please help me to find out the problem in GetPrinter function. First I am trying to get the printer handle for the specified printer using OpenPrinter function. . HANDLE hPrinter = 0; OpenPrinter ((LPTSTR)(LPCTSTR)strPrinterAddress, &hPrinter, NULL); This function gets succeed. Using this printer handle I am trying to access the specified printer information’s using the GetPrinter function. GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); } Even using GetLastError() function I am not able retrieve error message. This function is not failing all the times. When I am trying to print out of 10 times, 6 times the printing is succeed. Only 4 times it’s getting failed. Please help me shoot out this problem. Thanks Gokul

            modified on Tuesday, October 14, 2008 4:08 AM

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Gokul_md wrote:

            GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); }

            This should be:

            if (! GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded))
            {
            DWORD dwError = GetLastError();
            FormatMessage(..., dwError, ...);
            }

            ClosePrinter(hPrinter);

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            G 1 Reply Last reply
            0
            • D David Crow

              Gokul_md wrote:

              GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded); if(!dwNeeded) { DWORD dwError = ::GetLastError(); ClosePrinter( hPrinter ); }

              This should be:

              if (! GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded))
              {
              DWORD dwError = GetLastError();
              FormatMessage(..., dwError, ...);
              }

              ClosePrinter(hPrinter);

              "Love people and use things, not love things and use people." - Unknown

              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

              G Offline
              G Offline
              Gokul_md
              wrote on last edited by
              #6

              Yes i have formated the dword error code to string in the above mentioned way only. but it return the empty string.

              D 1 Reply Last reply
              0
              • G Gokul_md

                Yes i have formated the dword error code to string in the above mentioned way only. but it return the empty string.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Gokul_md wrote:

                Yes i have formated the dword error code...

                What was its value?

                Gokul_md wrote:

                ...but it return the empty string.

                How are you verifying this?

                "Love people and use things, not love things and use people." - Unknown

                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                G 1 Reply Last reply
                0
                • D David Crow

                  Gokul_md wrote:

                  Yes i have formated the dword error code...

                  What was its value?

                  Gokul_md wrote:

                  ...but it return the empty string.

                  How are you verifying this?

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  G Offline
                  G Offline
                  Gokul_md
                  wrote on last edited by
                  #8

                  i have formated the message using the below method. LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | RMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, MAKELANGID(LANG_NEUTRAL, LANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,NULL ); CString strMessage = (LPTSTR)lpMsgBuf; The error message is printed in the log file. FILE *fp = fopen("C:\\log.txt","a+"); fprintf(fp,"\n Error Occured - %s ", strMessage ); fclose(fp); in that log file it printer as Error Occured -

                  D 1 Reply Last reply
                  0
                  • G Gokul_md

                    i have formated the message using the below method. LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | RMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, MAKELANGID(LANG_NEUTRAL, LANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,NULL ); CString strMessage = (LPTSTR)lpMsgBuf; The error message is printed in the log file. FILE *fp = fopen("C:\\log.txt","a+"); fprintf(fp,"\n Error Occured - %s ", strMessage ); fclose(fp); in that log file it printer as Error Occured -

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    What is the value of dwError?

                    "Love people and use things, not love things and use people." - Unknown

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    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