AddPrinter in Windows 98

From: Bert (test_at_test.com)
Date: 02/02/05


Date: Wed, 2 Feb 2005 08:28:59 +0100

Hi,

I found the code below to add or delete a printer (which drivers already are
installed) on a winNT/Win2k computer. Now I'm trying to get it working under
Win98, but it doesn't seem to work.
On msdn I found that in the API-call the 'pName' should be Null, so I passed
vbNullString. But I retrieve error 50 - The request is not supported.

Any idea?

Thanks, Bert

'*********************************************************************
Option Explicit

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 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")
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

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

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

    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

    hPrinter = AddPrinter(vbNullString, 2, pi2) '***
First paramter should be 'NULL' , but how to implement? ***
    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
    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



Relevant Pages

  • Re: Cant add a usb Cannon i960 printer
    ... the printer gets defined with a device string like the ... it is hardly interesting to repeat it unless the usblp kernel driver ... >and then the kernel discovers its devcies as it enumerates and probes buses. ...
    (comp.os.linux.setup)
  • Access web cam with multiple web cams
    ... tuner seem to use the same driver ... Private Const WS_VISIBLE As Integer = &H10000000 ... (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As ... Dim DriverVersion As String = Space ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Is C# support load device driver?
    ... how to manage driver loading/unloading using both PInvoke interop and WMI. ... string driverName; ... IntPtr fileHandle; ... databaseName, uint dwDesiredAccess); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is C# support load device driver?
    ... I can see my driver is loaded. ... > string driverName; ... > internal static extern IntPtr OpenSCManager(string machineName, ... > uint dwDesiredAccess, uint serviceType, uint startType, uint ...
    (microsoft.public.dotnet.languages.csharp)
  • Help! Question with marshalling
    ... Public Type CRNO ... No1 As String * 3 ... Public Type DLG_IOSTR ... Public Structure DLG_IOSTR ...
    (microsoft.public.dotnet.framework.interop)