|
-
May 15th, 2002, 02:23 PM
#1
Thread Starter
Member
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.
-
May 15th, 2002, 02:33 PM
#2
Frenzied Member
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:
Dim lret As Long
Dim SizeNeeded As Long
Dim buffer() As Long
ReDim Preserve buffer(0 To 1) As Long
lret = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer), SizeNeeded)
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
lret = GetPrinterApi(mhPrinter, Index, buffer(0), UBound(buffer) * 4, SizeNeeded)
HTH,
Duncan
-
Jun 12th, 2002, 07:48 AM
#3
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|