Results 1 to 3 of 3

Thread: Renaming Printer Live Using API

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2001
    Posts
    37

    Renaming Printer Live Using API

    Hey, Im trying to write a program that will change a printer's display name without making the user reboot. The easy way is to do it using the registry, but I am trying to use the getPrinter() and setPrinter() api functions to set the printer info, but I keep getting memory errors and such, and VB randomly closing. Maybe Im trying to do something too far out of my league, or maybe its not possible in vb (but as far im concerned everything is possible). So if someone can give me some pointers (no pun intended)

    Code:
    Public Function RenamePrinter(fromName As String, toName As String)
        On Error GoTo printerr
        Dim pHandle As Long
        Dim pBuffSize, pBuffSize2 As Long
        Dim r1, r2 As Long
        Dim pInfo As PRINTER_INFO_2
        Dim pDef As PRINTER_DEFAULTS
        r1 = OpenPrinter(fromName, pHandle, pDef)
        Call GetPrinter(pHandle, 2&, 0&, 0&, pBuffSize)
        'ReDim bPrint(1 To pBuffSize) As Byte
        Call GetPrinter(pHandle, 2&, pInfo, pBuffSize, pBuffSize2)
        'Call CopyMemory(pInfo, bPrint(1), Len(pInfo))
        MsgBox pInfo.pPrinterName
    
    printerr:
        MsgBox ("Error")
    cleanup:
        Call ClosePrinter(pHandle)
    I've tried to do this a number of ways and thats why there me some unassigned variables and such. Right now, vb doesn't evene rror, it just closes on me and says nothing.
    *urp*

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    See this article...in particular

    However, it is not as simple as just passing this structure to the GetPrinter API call as a printer can return more information than is in that structure and if you do not allocate sufficent buffer space for it to do so your application will crash.
    Fortunately the API call caters for this - if you pass zero in the pbSize parameter then the API call will tell you how big a buffer you will require in the pbSizeNeeded.
    This means that filling the information from the printer driver becomes a two step process:
    VB Code:
    1. Dim lret As Long
    2.   Dim SizeNeeded As Long
    3.  
    4.   Dim buffer() As Long
    5.  
    6.   ReDim Preserve buffer(0 To 1) As Long
    7.   lret = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer), SizeNeeded)
    8.   ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
    9.   lret = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer) * 4, SizeNeeded)
    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3
    New Member
    Join Date
    Feb 2002
    Posts
    12

    changing printer name

    There is a much simpler way to do it

    1) stop spooler service
    2) rename the following keys in the registry

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\printername to

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\new printername

    and

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\new printername\name=newprintername

    3) start spooler service

    All this can be done on the fly. I use this method when I rebuilt the Winnt add printer function to make it simpler for users.
    ken mason

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