Results 1 to 5 of 5

Thread: copy memory problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    copy memory problem

    i have posted on this issue some time ago, but need to revisit now (new printers)

    using api printdialog i have an issue, getting the returned printer devicename if it is longer than 30 characters

    as far as i can see the problem is the structure passed to copy memory

    vb Code:
    1. CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
    2.  
    3.  Type DEVMODE_TYPE
    4.     dmDeviceName As String * CCHDEVICENAME ' =32
    5.     dmSpecVersion As Integer
    6.     dmDriverVersion As Integer
    7.     dmSize As Integer
    8.     dmDriverExtra As Integer
    9.     dmFields As Long
    10.     dmOrientation As Integer
    11.     dmPaperSize As Integer
    12.     dmPaperLength As Integer
    13.     dmPaperWidth As Integer
    14.     dmScale As Integer
    15.     dmCopies As Integer
    16.     dmDefaultSource As Integer
    17.     dmPrintQuality As Integer
    18.     dmColor As Integer
    19.     dmDuplex As Integer
    20.     dmYResolution As Integer
    21.     dmTTOption As Integer
    22.     dmCollate As Integer
    23.     dmFormName As String * CCHFORMNAME
    24.     dmUnusedPadding As Integer
    25.     dmBitsPerPel As Integer
    26.     dmPelsWidth As Long
    27.     dmPelsHeight As Long
    28.     dmDisplayFlags As Long
    29.     dmDisplayFrequency As Long
    30. End Type
    i have tried changing the length of the dmdevicename but no improvement, when i call the procedure the printer name is truncated to 32 characters, but the returned name is always 30 charcters

    is there any way to fix this?

    here is the full code to the function

    Code:
     Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
        '-> Code by Donald Grover
        Dim PrintDlg As PRINTDLG_TYPE
        Dim DevMode As DEVMODE_TYPE
        Dim DevName As DEVNAMES_TYPE
    
        Dim lpDevMode As Long, lpDevName As Long
        Dim bReturn As Integer
        Dim objPrinter As Printer, NewPrinterName As String
    
        ' Use PrintDialog to get the handle to a memory
        ' block with a DevMode and DevName structures
    
        PrintDlg.lStructSize = Len(PrintDlg)
        PrintDlg.hWndOwner = frmOwner.hWnd
    
        PrintDlg.flags = PrintFlags
        On Error Resume Next
        'Set the current orientation and duplex setting
        DevMode.dmDeviceName = Printer.DeviceName
        DevMode.dmSize = Len(DevMode)
        DevMode.dmFields = DM_ORIENTATION Or DM_DUPLEX
        DevMode.dmPaperWidth = Printer.Width
        DevMode.dmOrientation = Printer.Orientation
        DevMode.dmPaperSize = Printer.PaperSize
        DevMode.dmDuplex = Printer.Duplex
        On Error GoTo 0
    
        'Allocate memory for the initialization hDevMode structure
        'and copy the settings gathered above into this memory
        PrintDlg.hDevMode = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevMode))
        lpDevMode = GlobalLock(PrintDlg.hDevMode)
        If lpDevMode > 0 Then
            CopyMemory ByVal lpDevMode, DevMode, Len(DevMode)
            bReturn = GlobalUnlock(PrintDlg.hDevMode)
        End If
    
        'Set the current driver, device, and port name strings
        With DevName
            .wDriverOffset = 8
            .wDeviceOffset = .wDriverOffset + 1 + Len(Printer.DriverName)
            .wOutputOffset = .wDeviceOffset + 1 + Len(Printer.Port)
            .wDefault = 0
        End With
    
        With Printer
            DevName.extra = .DriverName & Chr(0) & .DeviceName & Chr(0) & .Port & Chr(0)
        End With
    
        'Allocate memory for the initial hDevName structure
        'and copy the settings gathered above into this memory
        PrintDlg.hDevNames = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(DevName))
        lpDevName = GlobalLock(PrintDlg.hDevNames)
        If lpDevName > 0 Then
            CopyMemory ByVal lpDevName, DevName, Len(DevName)
            bReturn = GlobalUnlock(lpDevName)
        End If
    
        'Call the print dialog up and let the user make changes
        If PrintDialog(PrintDlg) <> 0 Then
            'First get the DevName structure.
            lpDevName = GlobalLock(PrintDlg.hDevNames)
            CopyMemory DevName, ByVal lpDevName, Len(DevName)
            bReturn = GlobalUnlock(lpDevName)
            GlobalFree PrintDlg.hDevNames
    
            'Next get the DevMode structure and set the printer
            'properties appropriately
            lpDevMode = GlobalLock(PrintDlg.hDevMode)
            CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
            bReturn = GlobalUnlock(PrintDlg.hDevMode)
            GlobalFree PrintDlg.hDevMode
            NewPrinterName = Mid(Split(DevName.extra, vbNullChar)(1), 2)
    '        NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
            If Printer.DeviceName <> NewPrinterName Then
                For Each objPrinter In Printers
                    If objPrinter.DeviceName = NewPrinterName Then
                        Set Printer = objPrinter
                        'set printer toolbar name at this point
                    End If
                Next
            End If
    
            On Error Resume Next
            'Set printer object properties according to selections made
            'by user
            
            Printer.Copies = DevMode.dmCopies
            Printer.Duplex = DevMode.dmDuplex
            Printer.Orientation = DevMode.dmOrientation
            Printer.PaperSize = DevMode.dmPaperSize
            Printer.PrintQuality = DevMode.dmPrintQuality
            Printer.ColorMode = DevMode.dmColor
            Printer.PaperBin = DevMode.dmDefaultSource
            On Error GoTo 0
        End If
    End Sub
    i think the original code was from allapi
    Last edited by westconn1; Jan 17th, 2010 at 05:20 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: copy memory problem

    There is nothing you can do. You have to use other means to get the full device name, maybe even VB's own native Printers collection can help with that?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: copy memory problem

    thanks merri i did not find that reference
    the point is to match the return from the api to the vb printers collection devicenames
    some of the printers now install with default names up to 40 characters and if with copy 2 or other extentions even more, with only the last part being different
    one of my printer names is
    HP Color LaserJet 2605dn_2605dtn PCL 6
    and i have copy2 for black only printing
    either of these is truncated to HP Color LaserJet 2605dn_2605d, so not possible to tell which is returned from the dialog

    seems i can get the name from the devname structure, i was playing with this, but was unable to get reliable result, but i think i have got it now
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: copy memory problem

    This MAY be helpful:
    VB Print Dialog

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: copy memory problem

    This MAY be helpful:
    thank you, i do know about this dialog, have posted links to it in the past, i have wrapped some apidialogs, and could not understand why the printer dialog should not return complete results
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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