Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

device name for printer hDc

Posted on 1999-07-02
6
Medium Priority
?
1,278 Views
Last Modified: 2010-05-18
I know this should be simple, but I have yet to figure it out.

I am using the Printer Common Dialog box to set the printer and return a device context (hDc) to use in printing with a third party tool.  I don't want to set the printer default to the selected printer, just use the hDc.  I would also like to be able to display the printer device name, orientation, etc., for the user after the hDc has been selected.  The common dialog box has an orientation property I can read, but I have yet to find a way to tell the calling program what printer was selected. The Printer.DeviceName property still shows the default printer settings, as expected, but now that I have an hDc, how do I know what device is associated with it?
0
Comment
Question by:mwiebe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1521614
Once you retrieve the hDC from the Common Dialog, use the following code:
(hDCPrint is the hDC from the Common Dialog)
  Dim prtTarget As Printer
 
  For Each prtTarget In Printers
    With prtTarget
      If hDCPrint = .hDC Then
        sDeviceName = .DeviceName
        sOrientation = .Orientation
        'Etc.
      End If
    End With
  Next

0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1521615
I don't think that works, I'm working on a similar problem and have already tried that without success.  I've almost fixed my problem by storing the current default printer name before calling the commondialog box(printerdefault = true) then setting the default back after the print job finishes.  Just triing to figure out the orientation part. See http://www.experts-exchange.com/Computers/Programming/Windows/Visual_Basic/Q.10176358  amd http://www.experts-exchange.com/Computers/Programming/Windows/Visual_Basic/Q.10177717
0
 

Author Comment

by:mwiebe
ID: 1521616
I've also tried that (see comment from BeedleGuis) without success.
0
Independent Software Vendors: 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!

 
LVL 13

Accepted Solution

by:
Mirkwood earned 400 total points
ID: 1521617

    Dim Handle As Long          'handle to printer
    Dim PrinterName As String
    Dim pd As PRINTER_DEFAULTS
    Dim x As Long
    Dim need As Long            ' bytes needed
    Dim pi2 As PRINTER_INFO_2   ' your PRINTER_INFO structure
    Dim LastError As Long

    ' determine which printer was selected
    PrinterName = List1.List(List1.ListIndex)
    ' none - exit
    If PrinterName = "" Then
        Exit Sub
    End If

    ' set the PRINTER_DEFAULTS members
    pd.pDatatype = 0&
    pd.DesiredAccess = PRINTER_ALL_ACCESS

    ' Get a handle to the printer
    x = OpenPrinter(PrinterName, Handle, pd)
    ' failed the open
    If x = False Then
        'error handler code goes here
        Exit Sub
    End If

    ' Make an initial call to GetPrinter, requesting Level 2
    ' (PRINTER_INFO_2) information, to determine how many bytes
    ' you need
    x = GetPrinter(Handle, 2, ByVal 0&, 0, need)
    ' don't want to check GetLastError here - it's supposed to fail
    ' with a 122 - ERROR_INSUFFICIENT_BUFFER
    ' redim t as large as you need
    ReDim t((need \ 4)) As Long

    ' and call GetPrinter for keepers this time
    x = GetPrinter(Handle, 2, t(0), need, need)
    ' failed the GetPrinter
    If x = False Then
        'error handler code goes here
        Exit Sub
    End If

    ' set the members of the pi5 structure for use with SetPrinter.
    ' PtrCtoVbString copies the memory pointed at by the two string
    ' pointers contained in the t() array into a Visual Basic string.
    ' The other three elements are just DWORDS (long integers) and
    ' don't require any conversion
    pi2.pServerName = PtrCtoVbString(t(0))
    pi2.pPrinterName = PtrCtoVbString(t(1))
    pi2.pShareName = PtrCtoVbString(t(2))
    pi2.pPortName = PtrCtoVbString(t(3))
    pi2.pDriverName = PtrCtoVbString(t(4))
    pi2.pComment = PtrCtoVbString(t(5))
    pi2.pLocation = PtrCtoVbString(t(6))
     
    ' call SetPrinter to set it
    'x = SetPrinter(Handle, 2, pi2, 0)
    ' failed the SetPrinter
    If x = False Then
        MsgBox "SetPrinterFailed. Error code: " & GetLastError()
        Exit Sub
    End If

    ' and close the handle
    ClosePrinter (Handle)

------- 8< ------ END CUT HERE --------- 8< ------

This is the PrtToVBString function:

------- 8< ------ CUT HERE --------- 8< ------

Private Function PtrCtoVbString(Add As Long) As String

    Dim sTemp As String * 512, x As Long

    x = lstrcpy(sTemp, Add)
    If (InStr(1, sTemp, Chr(0)) = 0) Then
         PtrCtoVbString = "" 
    Else
         PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
    End If
End Function
------- 8< ------ END CUT HERE --------- 8< ------

0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1521618

    ' determine which printer was selected
    PrinterName = List1.List(List1.ListIndex)
    ' none - exit
    If PrinterName = "" Then
        Exit Sub
    End If

Are you suggesting he use his own printerlist? I don't see anything returning a printer name from a device context.    
0
 

Author Comment

by:mwiebe
ID: 1521619
See  BeedleGuis comment - How does this get the printer name from the device context returned from the common dialog?  Am I missing something?
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question