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. Why "OpenPrinter" API fails when open a network printer?

Why "OpenPrinter" API fails when open a network printer?

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadminjsonquestion
7 Posts 4 Posters 5 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.
  • Q Offline
    Q Offline
    qujun2004
    wrote on last edited by
    #1

    Hi all I met a confusing issue, just as the title says. There's a network printer installed in my machine. And in my code when I open this printer with "OpenPrinter" API, it will failed. But, in fact the printer is OK, because I can use it to print in Notepad. My code is: HANDLE hPrinter = NULL; PRINTER_DEFAULTS PDef; PDef.pDatatype = NULL; PDef.pDevMode = NULL; BOOL bRet = OpenPrinter(_T("\\\\172.xx.xxx.xx\\HP LaserJet 1020"), &hPrinter, &PDef); What confuses me most is that, the "OpenPrinter" API doesn't faill in all test machines. Only in some machines it fails. In most test machines, it is OK to open the printer. I checked the printer and the test machines, however nothing wrong was found. And what's more, when OpenPrinter fails, the error code is 5, which means "Access is denied". It seems that the test machines can't access the printer. But it does printing operation OK! What's the problem with my machine? Any response will be appreciated very much! :confused::confused:

    M V 2 Replies Last reply
    0
    • Q qujun2004

      Hi all I met a confusing issue, just as the title says. There's a network printer installed in my machine. And in my code when I open this printer with "OpenPrinter" API, it will failed. But, in fact the printer is OK, because I can use it to print in Notepad. My code is: HANDLE hPrinter = NULL; PRINTER_DEFAULTS PDef; PDef.pDatatype = NULL; PDef.pDevMode = NULL; BOOL bRet = OpenPrinter(_T("\\\\172.xx.xxx.xx\\HP LaserJet 1020"), &hPrinter, &PDef); What confuses me most is that, the "OpenPrinter" API doesn't faill in all test machines. Only in some machines it fails. In most test machines, it is OK to open the printer. I checked the printer and the test machines, however nothing wrong was found. And what's more, when OpenPrinter fails, the error code is 5, which means "Access is denied". It seems that the test machines can't access the printer. But it does printing operation OK! What's the problem with my machine? Any response will be appreciated very much! :confused::confused:

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      Unfortunately their are 2 OpenPrinter APIs, one in Winspool.lib and one in SpoolSS.dll. If you use the wrong one then horrible, and not easily predictable, things occur. I suggest you check this out first, determine which one you're calling, try the other one and if things are no better or worse then feel free to vote this a bad answer. :)

      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

      Q 1 Reply Last reply
      0
      • M Matthew Faithfull

        Unfortunately their are 2 OpenPrinter APIs, one in Winspool.lib and one in SpoolSS.dll. If you use the wrong one then horrible, and not easily predictable, things occur. I suggest you check this out first, determine which one you're calling, try the other one and if things are no better or worse then feel free to vote this a bad answer. :)

        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

        Q Offline
        Q Offline
        qujun2004
        wrote on last edited by
        #3

        Matthew Faithfull wrote:

        Unfortunately their are 2 OpenPrinter APIs, one in Winspool.lib and one in SpoolSS.dll. If you use the wrong one then horrible, and not easily predictable, things occur. I suggest you check this out first, determine which one you're calling, try the other one and if things are no better or worse then feel free to vote this a bad answer.

        Matthew Faithfull, thank you very much. I will check it.

        1 Reply Last reply
        0
        • Q qujun2004

          Hi all I met a confusing issue, just as the title says. There's a network printer installed in my machine. And in my code when I open this printer with "OpenPrinter" API, it will failed. But, in fact the printer is OK, because I can use it to print in Notepad. My code is: HANDLE hPrinter = NULL; PRINTER_DEFAULTS PDef; PDef.pDatatype = NULL; PDef.pDevMode = NULL; BOOL bRet = OpenPrinter(_T("\\\\172.xx.xxx.xx\\HP LaserJet 1020"), &hPrinter, &PDef); What confuses me most is that, the "OpenPrinter" API doesn't faill in all test machines. Only in some machines it fails. In most test machines, it is OK to open the printer. I checked the printer and the test machines, however nothing wrong was found. And what's more, when OpenPrinter fails, the error code is 5, which means "Access is denied". It seems that the test machines can't access the printer. But it does printing operation OK! What's the problem with my machine? Any response will be appreciated very much! :confused::confused:

          V Offline
          V Offline
          vikas amin
          wrote on last edited by
          #4

          Your access to the network printers are different then local ones . Set the pDesiredAccess to PRINTER_ACCESS_USE or try this BOOL bRet = OpenPrinter(_T("\\\\172.xx.xxx.xx\\HP LaserJet 1020"), &hPrinter , NULL ); http://msdn.microsoft.com/en-us/library/cc211063.aspx[^]

          Vikas Amin

          My First Article on CP" Virtual Serial Port "[^]

          modified on Thursday, July 24, 2008 5:33 PM

          Q 1 Reply Last reply
          0
          • V vikas amin

            Your access to the network printers are different then local ones . Set the pDesiredAccess to PRINTER_ACCESS_USE or try this BOOL bRet = OpenPrinter(_T("\\\\172.xx.xxx.xx\\HP LaserJet 1020"), &hPrinter , NULL ); http://msdn.microsoft.com/en-us/library/cc211063.aspx[^]

            Vikas Amin

            My First Article on CP" Virtual Serial Port "[^]

            modified on Thursday, July 24, 2008 5:33 PM

            Q Offline
            Q Offline
            qujun2004
            wrote on last edited by
            #5

            Amin, thank you very much! Actually, I have tested to set the "pDesiredAccess" to "PRINTER_ACCESS_USE", instead of using "PRINTER_ALL_ACCESS". When I doing this, "OpenPrinter" to all printers installed in my machine is successful. However, in fact, there's another network printer which is unaccessible. When I use "PRINTER_ALL_ACCESS" in "OpenPrinter", it can't distinguish this situation. What I mean is that, in my machine there're two network printers. One is OK and another is really unaccessible. When I use "PRINTER_ALL_ACCESS", "OpenPrinter" to both of the two printers is FAILED. On the contray, use "PRINTER_ACCESS_USE", makes "OpenPrinter" to the two printers successful. I thought that there should to be a method for "OpenPrinter" to distinguish these two printer accurately. But my test shows that "OpenPrinter" is somewhat awkward. Maybe there's something wrong with the setting of my computer.

            V S 2 Replies Last reply
            0
            • Q qujun2004

              Amin, thank you very much! Actually, I have tested to set the "pDesiredAccess" to "PRINTER_ACCESS_USE", instead of using "PRINTER_ALL_ACCESS". When I doing this, "OpenPrinter" to all printers installed in my machine is successful. However, in fact, there's another network printer which is unaccessible. When I use "PRINTER_ALL_ACCESS" in "OpenPrinter", it can't distinguish this situation. What I mean is that, in my machine there're two network printers. One is OK and another is really unaccessible. When I use "PRINTER_ALL_ACCESS", "OpenPrinter" to both of the two printers is FAILED. On the contray, use "PRINTER_ACCESS_USE", makes "OpenPrinter" to the two printers successful. I thought that there should to be a method for "OpenPrinter" to distinguish these two printer accurately. But my test shows that "OpenPrinter" is somewhat awkward. Maybe there's something wrong with the setting of my computer.

              V Offline
              V Offline
              vikas amin
              wrote on last edited by
              #6

              I am not pretty sure why this happens with the other printer but you can test by runing your application form some other PC and check if you can acess both the n/w printers.

              Vikas Amin

              My First Article on CP" Virtual Serial Port "[^]

              modified on Thursday, July 24, 2008 5:33 PM

              1 Reply Last reply
              0
              • Q qujun2004

                Amin, thank you very much! Actually, I have tested to set the "pDesiredAccess" to "PRINTER_ACCESS_USE", instead of using "PRINTER_ALL_ACCESS". When I doing this, "OpenPrinter" to all printers installed in my machine is successful. However, in fact, there's another network printer which is unaccessible. When I use "PRINTER_ALL_ACCESS" in "OpenPrinter", it can't distinguish this situation. What I mean is that, in my machine there're two network printers. One is OK and another is really unaccessible. When I use "PRINTER_ALL_ACCESS", "OpenPrinter" to both of the two printers is FAILED. On the contray, use "PRINTER_ACCESS_USE", makes "OpenPrinter" to the two printers successful. I thought that there should to be a method for "OpenPrinter" to distinguish these two printer accurately. But my test shows that "OpenPrinter" is somewhat awkward. Maybe there's something wrong with the setting of my computer.

                S Offline
                S Offline
                Selcuk GURAL
                wrote on last edited by
                #7

                Hi, Then i use

                STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_USE | PRINTER_ACCESS_ADMINISTER

                everything perfect for network printers.

                http://selcukgural.com

                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