Results 1 to 4 of 4

Thread: Printer APIs

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    With the help of OpenPrinter...
    You also need the PRINTER_DEFAULT, the DEVMODE structures and also the CCHDEVICENAME and the CCHFORMNAME constants plus an access constant like PRINTER_ACCESS_USE or PINTER_ACCESS_ADMINISTER.
    You'll find all of the above in the API Viewer...

    Look the rest up in the MSDN library...

    Best regards

  2. #2
    nullus
    Guest
    Thanks, I think I've got an idea about how to do this now...

  3. #3
    nullus
    Guest
    Hmm... this is what I have now (with a little help from MS), but it still doesn't work.

    Code:
    Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
    
    Private Const CCHDEVICENAME = 32
    Private Const CCHFORMNAME = 32
    Private Const NULLPTR = 0&
    Private Const DMORIENT_PORTRAIT = 1
    Private Const DMORIENT_LANDSCAPE = 2
    
    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 Long
            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
    
    Public Sub SetPrinterSettings(pPrinterName As String, phdc As Long)
        Dim pDevMode As DEVMODE
        Dim lngPrinter As Long
    
        If (OpenPrinter(pPrinterName, lngPrinter, NULLPTR) <> 0) Then
            If (pDevMode.dmOrientation = DMORIENT_PORTRAIT) Or (pDevMode.dmOrientation = 0) Then
                pDevMode.dmOrientation = DMORIENT_LANDSCAPE
            Else
                pDevMode.dmOrientation = DMORIENT_PORTRAIT
            End If
        End If
    End Sub
    If I run it like this, it'll bring up a ByRef error where I pass NULLPTR into OpenPrinter. I commented the first If statement out, but it still doesn't alter the printer's orientation.

    I know nothing about how to use these APIs, what am I doing wrong?

  4. #4

    Thread Starter
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Try this code:
    Code:
    Private Const PRINTER_ACCESS_USE = &H8
    
    Public Sub SetPrinterSettings(pPrinterName As String, phdc As Long)
        Dim pDevMode As DEVMODE
        Dim lngPrinter As Long
        Dim udtPDef As PRINTER_DEFAULTS
        Dim hPrinter As Long
        
        With pDevMode
            .dmCopies = 1
            .dmOrientation = DMORIENT_LANDSCAPE
        End With
        With udtPDef
            .pDevMode = pDevMode
            .DesiredAccess = PRINTER_ACCESS_USE
        End With
        
        hPrinter = OpenPrinter(pPrinterName, lngPrinter, udtPDef)
        If hPrinter <> 0 Then
            'do the print
        End If
    End Sub
    BTW if you want to pass a null pointer to a Long then you have to pass it ByVal:
    Code:
    If (OpenPrinter(pPrinterName, lngPrinter, ByVal NULLPTR) <> 0) Then
    Best regards

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