-
Jan 28th, 2010, 04:15 PM
#1
Thread Starter
Junior Member
openprinter api
i am working on some code that used the openprinter api to send print code to a generic text printer. the code works fine in windows xp but when i go to run the same app on windows 7 it fails. is there a different api that needs to be used for windows 7 or is this a windows 7 security thing?
thanks
-
Jan 28th, 2010, 04:24 PM
#2
Re: openprinter api
Source, extracts below
Originally Posted by msdn
If a user does not have permission to open a specified printer or print server with the desired access, the OpenPrinter call will fail with a return value of zero and GetLastError will return the value ERROR_ACCESS_DENIED.
You can call the OpenPrinter function to open a handle to a print server or to determine the access rights that a client has to a print server. To do so, specify the name of the print server in the pPrinterName parameter, set the pDatatype and pDevMode members of the PRINTER_DEFAULTS structure to NULL, and set the DesiredAccess member to specify a server access mask value such as SERVER_ALL_ACCESS. When you are finished with the handle, pass it to the ClosePrinter function to close it.
-
Jan 28th, 2010, 04:33 PM
#3
Thread Starter
Junior Member
Re: openprinter api
the printer is a network printer that is shared off from another pc. on the windows 7 box the printer is set up to allow user 'EVERYONE' full access to the printer so i would not think it is a permissions issue. i could be wrong though.
-
Jan 28th, 2010, 04:37 PM
#4
Re: openprinter api
I assume OpenPrinter is returning zero? If so, what does Err.LastDllError return? Check that immediately afterwards.
Edited: If you are getting error code 5: Access Denied or any error really. Maybe try setting pDesiredAccess to PRINTER_ACCESS_USE (value of 8)
Last edited by LaVolpe; Jan 28th, 2010 at 04:45 PM.
-
Jan 28th, 2010, 04:54 PM
#5
Thread Starter
Junior Member
Re: openprinter api
i will create a test app and check these as what i am running now is compiled code not outputting the error info.
-
Feb 1st, 2010, 10:55 AM
#6
Thread Starter
Junior Member
Re: openprinter api
I got my app working and you are correct i am getting error 5 access denied. i tried to change the access level and am not getting an unhandled exception
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
any thoughts?
also this is a service that is doing the printing, not sure if that would make a difference.
Last edited by dougg; Feb 1st, 2010 at 11:16 AM.
-
Feb 1st, 2010, 11:06 AM
#7
Re: openprinter api
Maybe try setting pDesiredAccess to PRINTER_ACCESS_USE (value of 8)
-
Feb 1st, 2010, 11:40 AM
#8
Thread Starter
Junior Member
Re: openprinter api
i am sorry but i am a little confused. i tried to set the access level
If OpenPrinter(PrinterName, lhPrinter, 8) = 0 Then
and then i get the Attempted to read or write protected memory. This is often an indication that other memory is corrupt unhandled exception.
can you provide me with a code snippet?
thanks
-
Feb 1st, 2010, 11:45 AM
#9
Re: openprinter api
Did you declare the OpenPrinter API's final parameter as ByRef or ByVal? If not ByVal, try:
OpenPrinter(PrinterName, lhPrinter, ByVal 8&)
-
Feb 1st, 2010, 12:00 PM
#10
Thread Starter
Junior Member
Re: openprinter api
this may be an issue with the service somehow. i wrote just a vb forms app with the same basic code that is being used in the service and it prints things fine. the service gives the errors.
-
Feb 1st, 2010, 12:24 PM
#11
Re: openprinter api
Oops, the final parameter in the OpenPrinter API is a structure. The default API declaration appears to be:
Code:
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, ByRef phPrinter As Long, ByRef pDefault As Any) As Long
Private Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Integer
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
Private Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As DEVMODE
DesiredAccess As Long
End Type
Private Const CCHDEVICENAME As Long = 32
Private Const CCHFORMNAME As Long = 32
Private Const PRINTER_ACCESS_USE AS Long = 8
So, using the above info, maybe this will do the trick
Code:
Dim PDF As PRINTER_DEFAULTS
PDF.DesiredAccess = PRINTER_ACCESS_USE
lResult = OpenPrinter(PrinterName, lhPrinter, PDF)
-
Feb 1st, 2010, 02:09 PM
#12
Thread Starter
Junior Member
Re: openprinter api
I modified my test app to use the code you provided(modified to change it to VB.NET) and i get an unhadled exception of value does not fall with the expected range.
i dont think this is an api issue as much as it is an issue with windows 7 security on either the service or the printer.
-
Jan 30th, 2012, 05:08 PM
#13
New Member
Re: openprinter api
dougg, did you ever resolve this? I'm having the same problem. The call worked fine under XP, then the user got a Windows 7 machine last week, and it no longer works. The code is VBA in Access 2003. I'm trying LaVolpe's suggestion, passing a PRINTER_DEFAULTS structure with DesiredAccess of 8 (it was previously just passing a NULL third argument), and it's not crashing, but it also still returns FALSE. Err.LastDllError, however, is returning 3012, unlike your case. Some other Google searching indicates this may mean "Printer Not Found".
I'm thinking definitely security issue, not code issue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|