Solved

AddPrinter in VB

Posted on 2003-11-25
8
995 Views
Last Modified: 2008-02-01
Finally, after I try many options, I would like to use AddPrinter API to create a printer in vb, but, this function return 0.

When I launch with :

? CreatePrinter ("\\KIWI", "HP Laserjet 4L"), there is nothing. This function return false.

My server name is \\KIWI and my printer is HP Laserjet 4L.

What's wrong ? Is it possible to send a stirng and not pointer ?

Thanks for your help.

The Api is :

Private Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterA" (ByVal pName As String, ByVal Level As Long, pPrinter As PRINTER_INFO_2) As Long

The PRINTER_INFO_2 :

Private Type PRINTER_INFO_2
  pServerName As String
  pPrinterName As String
  pShareName As String
  pPortName As String
  pDriverName As String
  pComment As String
  pLocation As String
  pDevMode As Integer
  pSepFile As String
  pPrintProcessor As String
  pDatatype As String
  pParameters As String
  pSecurityDescriptor As Integer
  Attributes As Long
  Priority As Long
  DefaultPriority As Long
  StartTime As Long
  UntilTime As Long
  Status As Long
  cJobs As Long
  AveragePPM As Long
End Type

The code is :

'Ajoute une imprimante à l'ordinateur
Public Function CreatePrinter(strServer As String, strPrinter As String) As Boolean
Dim hPrinter As Long
Dim pi2 As PRINTER_INFO_2
Dim bBuffer(1000) As Byte
Dim i

Dim strDriver As String

  strDriver = ""

  '*********************************************************************
  ' Initialise our buffer
  '*********************************************************************
  For i = 0 To UBound(bBuffer)
    bBuffer(i) = 0
  Next
 
  With pi2
   
    '*********************************************************************
    ' Set the pointers to the string values in bBuffer
    '*********************************************************************
    .pPrinterName = AddString(strPrinter, bBuffer)
    .pPortName = AddString("LPT1", bBuffer)
    .pDriverName = AddString(strDriver, bBuffer)
    .pPrintProcessor = AddString("winprint", bBuffer)
     
    '*********************************************************************
    ' Default all other values to 0 (NULL)
    '*********************************************************************
    .Attributes = 0
    .AveragePPM = 0
    .cJobs = 0
    .DefaultPriority = 0
    .pComment = 0
    .pDatatype = 0
    .pDevMode = 0
    .pLocation = 0
    .pParameters = 0
    .Priority = 0
    .pSecurityDescriptor = 0
    .pSepFile = 0
    .pServerName = 0
    .pShareName = 0
    .StartTime = 0
    .Status = 0
    .UntilTime = 0
  End With

  '*********************************************************************
  ' Add the printer
  ' If success then close the returned handle
  '*********************************************************************
  hPrinter = AddPrinter(AddString(strServer, bBuffer), 2, pi2)
  If hPrinter <> 0 Then
    ClosePrinter (hPrinter)
    CreatePrinter = True
  Else
    CreatePrinter = False
  End If
End Function
0
Comment
Question by:esohier
8 Comments
 
LVL 14

Expert Comment

by:Matti
ID: 9817262
Just a small info! Put that Api declaration before that type declaration.
0
 
LVL 5

Accepted Solution

by:
mccainz2 earned 250 total points
ID: 9819151
there is an easier way , add a reference to the windows script host object model and ...

Dim objWSH As New WshNetwork
objWSH.AddWindowsPrinterConnection "\\ddc3sa\hp4500"
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9826219
hearing...
0
 
LVL 14

Expert Comment

by:waty
ID: 9830943
' #Mandix Repository#*****************************************************
' * Programmer Name  : Tony Edgecombe
' * Web Site         : http://www.vbdiamond.com
' * E-Mail           : Tony.Edgecombe@frogmorecs.co.uk
' * Date             : 01/23/2001
' **********************************************************************
' * Comments         : Add / Delete Printers (Windows NT/2000)
' *
' * Add or delete a Windows NT / 2000 printer. Illustrates use of
' * PRINTER_INFO_2 structure and printer related API functions such
' * as CreatePrinter. Usage is well-documented.
' *
' **********************************************************************
Option Explicit
Option Base 0

Public Type PRINTER_INFO_2
   pServerName          As Long 'String
   pPrinterName         As Long 'String
   pShareName           As Long 'String
   pPortName            As Long 'String
   pDriverName          As Long 'String
   pComment             As Long 'String
   pLocation            As Long 'String
   pDevMode             As Long ' DEVMODE
   pSepFile             As Long 'String
   pPrintProcessor      As Long 'String
   pDatatype            As Long 'String
   pParameters          As Long 'String
   pSecurityDescriptor  As Long 'SECURITY_DESCRIPTOR
   Attributes           As Long
   Priority             As Long
   DefaultPriority      As Long
   StartTime            As Long
   UntilTime            As Long
   Status               As Long
   cJobs                As Long
   AveragePPM           As Long
End Type

Public Type PRINTER_DEFAULTS
   pDatatype            As Long 'String
   pDevMode             As Long 'DEVMODE
   DesiredAccess        As Long
End Type

Public Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterA" (ByVal pName As String, ByVal Level As Long, pPrinter As PRINTER_INFO_2) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function DeletePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As Long

Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
'
' This code adds a printer to a Windows NT/2000 server/workstation
'
' Written by Tony Edgecombe
' Tony.Edgecombe@frogmorecs.co.uk
' www.vbprint.com
' 22 Jan 2001
'
'

Sub main()

   '*********************************************************************
   ' Test call
   '
   ' The parameters to CreatePrinter are:
   '
   ' Server Name - Blank if local machine
   ' Printer Name - Must be unique
   ' Port Name - Port must be installed already
   ' Driver Name - Driver must be installed already
   ' Print Processor - Must be installed already
   '
   '*********************************************************************

   MsgBox "Printer Creation: " & CreatePrinter("", "New Printer", "LPT1:", "HP LaserJet 1100 (MS)", "WinPrint")

   '*********************************************************************
   ' Test Call
   ' Parameter is the name of the printer to delete
   '
   ' For printers on another server use the UNC path ie \\Server\Printer
   '*********************************************************************

   MsgBox "Printer Deletion: " & RemovePrinter("New Printer")

End Sub

Function CreatePrinter(strServer As String, _
   strPrinter As String, _
   strPort As String, _
   strDriver As String, _
   strPrintProcessor As String) As Boolean

   Dim hPrinter         As Long
   Dim pi2              As PRINTER_INFO_2
   Dim bBuffer(1000)    As Byte
   Dim i

   '*********************************************************************
   ' Initialise our buffer
   '*********************************************************************

   For i = 0 To UBound(bBuffer)
      bBuffer(i) = 0
   Next

   '*********************************************************************
   ' Set the pointers to the string values in bBuffer
   '*********************************************************************

   pi2.pPrinterName = AddString(strPrinter, bBuffer)
   pi2.pPortName = AddString(strPort, bBuffer)
   pi2.pDriverName = AddString(strDriver, bBuffer)
   pi2.pPrintProcessor = AddString(strPrintProcessor, bBuffer)

   '*********************************************************************
   ' Default all other values to 0 (NULL)
   '*********************************************************************

   pi2.Attributes = 0
   pi2.AveragePPM = 0
   pi2.cJobs = 0
   pi2.DefaultPriority = 0
   pi2.pComment = 0
   pi2.pDatatype = 0
   pi2.pDevMode = 0
   pi2.pLocation = 0
   pi2.pParameters = 0
   pi2.Priority = 0
   pi2.pSecurityDescriptor = 0
   pi2.pSepFile = 0
   pi2.pServerName = 0
   pi2.pShareName = 0
   pi2.StartTime = 0
   pi2.Status = 0
   pi2.UntilTime = 0

   '*********************************************************************
   ' Add the printer
   ' If success then close the returned handle
   '*********************************************************************

   hPrinter = AddPrinter(strServer, 2, pi2)
   If hPrinter <> 0 Then
      ClosePrinter (hPrinter)
      CreatePrinter = True
   Else
      CreatePrinter = False
   End If
End Function

Private Function AddString(strString As String, ByRef bBuffer() As Byte) As Long

   '*********************************************************************
   ' AddString copies a string into a Byte array and returns a long
   ' pointer to that string
   '*********************************************************************

   Dim lngEnd           As Long

   lngEnd = UBound(bBuffer) + 1
   Do
      lngEnd = lngEnd - 1
   Loop While (bBuffer(lngEnd) = 0 And lngEnd > 0)
   lngEnd = lngEnd + 2

   lstrcpy VarPtr(bBuffer(0)) + lngEnd, strString

   AddString = VarPtr(bBuffer(0)) + lngEnd
End Function

Function RemovePrinter(strPrinter As String)
   Dim hPrinter         As Long
   Dim pd               As PRINTER_DEFAULTS

   '*********************************************************************
   ' Create a PRINTER_DEFAULTS structure with the required access rights
   '*********************************************************************

   pd.pDatatype = 0
   pd.pDevMode = 0
   pd.DesiredAccess = PRINTER_ALL_ACCESS

   '*********************************************************************
   ' Open the printer
   '*********************************************************************

   If OpenPrinter(strPrinter, hPrinter, pd) = 0 Then
      RemovePrinter = False
      Exit Function
   End If

   '*********************************************************************
   ' Remove it
   '*********************************************************************

   If DeletePrinter(hPrinter) = 0 Then
      RemovePrinter = False
      Exit Function
   End If

   '*********************************************************************
   ' Release the handle
   '*********************************************************************

   ClosePrinter (hPrinter)

   RemovePrinter = True
End Function
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:mccainz2
ID: 9874835
Feedback  ????
Did any of these solutions help you?
DO you need anymore help on this topic?

THanks
0
 

Author Comment

by:esohier
ID: 9875192
Sorry for the delay,

I've finished recently the module and I can now create, remove or change any settings of printers.

I was use the WScript.Network object to manipulate the printer.

Thanks again.
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9876089
Curious as to why I was graded with a 'B' on this?
Seems like a two-line solution that replaces  one-hundred+ lines of code and also provides you the object by which to manipulate your printer would warrant an 'A'.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9881947
It appears that more lines of code means better code/more effort.
Ridiculous!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now