Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

how to use printerproperties api on winxp/win2k

I'm trying to use printerproperties api function from winspool.drv, but it dosen't work on winxp (it shows the about dialog of the printer driver or a basic property window), on win98, win95 or winME works perfectly. I need it because I want to make a custom printer dialog.

the code goes like this:

Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hwnd As Long, ByVal hPrinter As Long) As Long
Private Sub Form_Load()
    Dim hPrinter As Long
    OpenPrinter Printer.DeviceName, hPrinter, ByVal 0&
    PrinterProperties Me.hwnd, hPrinter
    ClosePrinter hPrinter
End Sub

Is there any work around to this prob?
0
SebaBiglia
Asked:
SebaBiglia
  • 5
  • 5
1 Solution
 
DarkoLordCommented:
What about using commondialog to show printer properties?

Darko
0
 
SebaBigliaAuthor Commented:
I'm already using it, but I want to customize the printer dialog, to reduce the number of step you need to print. now the user needs to set my program's specifics opcions, then select a printer, and then configure the printer options, I want to put the two first dialogs in one.

0
 
DarkoLordCommented:
You can set these options directly with OpenPrinter and GetPrinter APIs:

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SebaBigliaAuthor Commented:
I think that you aren't getting my point, I need to place a combo box in a dialog with the list of installed printers (done) and a button next to it to open the selected printer properties, that is what I want to do. It's is supposed to be done using the printerproperties API, but I don't know why it dosen't work on Win2k/WinXP, I need a workarround to this specific problem.

I think that there my be a way because a lot of commercial applications has a custom printing dialog an has the combo with the "properties..." button and it works OK on any OS.
0
 
DarkoLordCommented:
Well why don't you then just enumerate all printers and put them to combobox?
0
 
SebaBigliaAuthor Commented:
I've already done it. But I wan't to open the printer properties window of the selected printer in the combobox with the "printerpoperties" API and it dosen't work, it just shows you a usless about form. If you try the same code on win98 it works just fine. That is my problem, I need to know if there is any way to make the code works on win2k/winxp.w
0
 
DarkoLordCommented:
Private Const DM_OUT_BUFFER = 2
Private Const DM_IN_PROMPT = 4
Private Const DM_IN_BUFFER = 8
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const PRINTER_ACCESS_ADMINISTER = &H4
Private Const PRINTER_ACCESS_USE = &H8
Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

Private Type PRINTER_DEFAULTS
    pDatatype As String
    pDevMode As Long
    DesiredAccess As Long
End Type

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
Private Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As Long, ByVal hPrinter As Long, ByVal pDeviceName As String, ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, ByVal fMode As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
Private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long

Public Sub DocumentPropertiesDlg(ByVal hwnd As Long, ByVal PrinterName As String)
    Dim r As Long
    Dim hPrinter As Long
    Dim PD As PRINTER_DEFAULTS
    Dim mDM() As Long
    Dim mPI2() As Long
    Dim BytesNeeded As Long
    Dim Drav As Long
    Dim ErrReport As Boolean
   
    ErrReport = True
    PD.DesiredAccess = PRINTER_ALL_ACCESS
    PD.pDatatype = vbNullString
    PD.pDevMode = 0
    r = OpenPrinter(PrinterName, hPrinter, PD)
    If (r = 0) Or (hPrinter = 0) Then
        GoTo Clean
    End If
    r = DocumentProperties(0, hPrinter, PrinterName, 0, 0, 0)
    If r < 0 Then
        GoTo Clean
    End If
    '1 Long = 4 Byte
    ReDim mDM(r / 4)
    r = DocumentProperties(hwnd, hPrinter, PrinterName, VarPtr(mDM(0)), VarPtr(mDM(0)), DM_OUT_BUFFER Or DM_IN_PROMPT)
    If r = IDCANCEL Then
        GoTo Clean
    End If
    GetPrinter hPrinter, 2, 0, 0, BytesNeeded
    If BytesNeeded = 0 Then
        GoTo Clean
    End If
    ReDim mPI2(BytesNeeded / 4)
    r = GetPrinter(hPrinter, 2, mPI2(0), BytesNeeded, Drav)
    If r = 0 Then
        GoTo Clean
    End If
    mPI2(7) = VarPtr(mDM(0))
    r = SetPrinter(hPrinter, 2, mPI2(0), 0)
    If CBool(r) = False Then
        GoTo Clean
    End If
    ErrReport = False
   
Clean:
    If ErrReport And Err.LastDllError <> 0 Then
        MsgBox Err.LastDllError
    End If
    If hPrinter <> 0 Then
        ClosePrinter hPrinter
    End If
End Sub

Private Sub Command1_Click()
   DocumentPropertiesDlg Me.hwnd, Printer.DeviceName
End Sub
0
 
SebaBigliaAuthor Commented:
Now we are talking, that's what I was looking for..

Works like a charm, thank's man.
0
 
DarkoLordCommented:
You're welcome =)

At first I didn't understand what are you trying to do.. :)
0
 
SebaBigliaAuthor Commented:
I'm not a naturally english speaker (or writer), so maybe I didn't explain myself well.

I was looking for this tip for about two months. But everywere I look they talk about printerproperties function but none of them ever mention DocumentProperties.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now