Why "OpenPrinter" API fails when open a network printer?
-
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:
-
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:
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)
-
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)
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.
-
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:
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
-
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
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.
-
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.
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
-
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.
Hi, Then i use
STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_USE | PRINTER_ACCESS_ADMINISTER
everything perfect for network printers.