Results 1 to 13 of 13

Thread: openprinter api

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: openprinter api

    Source, extracts below
    Quote 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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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.

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: openprinter api

    Maybe try setting pDesiredAccess to PRINTER_ACCESS_USE (value of 8)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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&)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Posts
    16

    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.

  13. #13
    New Member
    Join Date
    Jan 2012
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width