Results 1 to 4 of 4

Thread: addprinter api

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    53
    how to use addprinter api.

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


    what are the pName ,Level , pPrinter arguments.




  2. #2
    Addicted Member Babbalouie's Avatar
    Join Date
    Jan 2001
    Location
    On the bright, blue sea...
    Posts
    197
    Not sure how it work, but I have some code that does it. This stuff's about as fubbed duck as a football bat:

    Routine Code:
    Code:
    Const ERROR_PRINTER_ALREADY_EXISTS = 1802
    Const PRINTER_NAME = "Generic / Text Only"
    Const PORT_NAME = "FILE:"
    Const DRIVER_NAME = "Generic / Text Only"
    Const PRINT_PROCESSOR = "winprint"
    Const DATA_TYPE = "TEXT"
    Const DATA_FILE = "Txtonly.dll"
    Const DRIVER_FILE = "Rasdd.dll"
    Const CONFIG_FILE = "Rasddui.dll"
    Const HELP_FILE = "Rasddui.hlp"
    Const ENVIRONMENT = "Windows NT x86"
    Dim hpHandle As Long
    Dim lError As Long
    Dim lResult As Long
    Dim sDriverBuf As String * Max_Path
    Dim sDriverPath As String
    Dim lPathSize As Long
    
      On Error GoTo AddPrinterError
      
      ' First we get the folder for the printer driver files
      lResult = GetPrinterDriverDirectory(vbNullString, vbNullString, 1&, _
                                          sDriverBuf, Max_Path, lPathSize)
      If (lResult = 0) Then
        ErrorHandler "Unable to retrieve the printer driver folder.", Err.LastDllError
      End If
      
      lResult = InStr(sDriverBuf, Chr$(0))
      sDriverPath = Left$(sDriverBuf, lResult - 1)
      
      If (Right$(sDriverPath, 1) <> "\") Then sDriverPath = sDriverPath & "\"
      
      ' Next we copy the required printer files to that folder
      If (CopyFile(App.Path & "\" & DATA_FILE, sDriverPath & DATA_FILE, False) = 0) Then
        ErrorHandler "Unable to copy the required printer driver files (Data).", Err.LastDllError
      End If
      If (Dir(sDriverPath & DATA_FILE, vbReadOnly) <> "") Then
        SetAttr sDriverPath & DATA_FILE, vbNormal
      End If
      
      If (CopyFile(App.Path & "\" & DRIVER_FILE, sDriverPath & DRIVER_FILE, False) = 0) Then
        ErrorHandler "Unable to copy the required printer driver files (Driver).", Err.LastDllError
      End If
      If (Dir(sDriverPath & DRIVER_FILE, vbReadOnly) <> "") Then
        SetAttr sDriverPath & DRIVER_FILE, vbNormal
      End If
      
      If (CopyFile(App.Path & "\" & CONFIG_FILE, sDriverPath & CONFIG_FILE, False) = 0) Then
        ErrorHandler "Unable to copy the required printer driver files (Config).", Err.LastDllError
      End If
      If (Dir(sDriverPath & CONFIG_FILE, vbReadOnly) <> "") Then
        SetAttr sDriverPath & CONFIG_FILE, vbNormal
      End If
      
      If (CopyFile(App.Path & "\" & HELP_FILE, sDriverPath & HELP_FILE, False) = 0) Then
        ErrorHandler "Unable to copy the required printer driver files (Help).", Err.LastDllError
      End If
      If (Dir(sDriverPath & HELP_FILE, vbReadOnly) <> "") Then
        SetAttr sDriverPath & HELP_FILE, vbNormal
      End If
      
      'Add the Printer Driver. This will configure the printer driver.
      udtDriverInfo.cVersion = 2&
      udtDriverInfo.pName = PRINTER_NAME
      udtDriverInfo.pEnvironment = ENVIRONMENT
      udtDriverInfo.pDriverPath = sDriverPath & DRIVER_FILE
      udtDriverInfo.pDataFile = sDriverPath & DATA_FILE
      udtDriverInfo.pConfigFile = sDriverPath & CONFIG_FILE
      udtDriverInfo.pHelpFile = sDriverPath & HELP_FILE
      udtDriverInfo.pDependentFiles = DATA_FILE & Chr$(0) & _
                                      DRIVER_FILE & Chr$(0) & _
                                      CONFIG_FILE & Chr$(0) & _
                                      HELP_FILE & Chr$(0) & Chr$(0)
      udtDriverInfo.pMonitorName = vbNullString
      udtDriverInfo.pDefaultDataType = DATA_TYPE
      
      lResult = AddPrinterDriver(vbNullString, 3&, udtDriverInfo)
      If (lResult = 0) Then
        ErrorHandler "Unable to configure Generic printer driver.", Err.LastDllError
      End If
      
      ' Now we can add the printer
      udtPrinterInfo.pServerName = vbNullString
      udtPrinterInfo.pPrinterName = PRINTER_NAME
      udtPrinterInfo.pPortName = PORT_NAME
      udtPrinterInfo.pDriverName = DRIVER_NAME
      udtPrinterInfo.pPrintProcessor = PRINT_PROCESSOR
      udtPrinterInfo.pDatatype = DATA_TYPE
      hpHandle = AddPrinter(vbNullString, 2&, udtPrinterInfo)
      If (hpHandle) Then
        ClosePrinter (hpHandle)
      Else
        lError = Err.LastDllError
        If (lError <> ERROR_PRINTER_ALREADY_EXISTS) Then
          ErrorHandler "Unable to install Generic printer.", lError
        End If
      End If
    Module Code:
    Code:
    Private Type PRINTER_INFO_2
        pServerName As String
        pPrinterName As String
        pShareName As Long
        pPortName As String
        pDriverName As String
        pComment As Long
        pLocation As Long
        pDevMode As Long  ' Pointer to DEVMODE
        pSepFile As Long
        pPrintProcessor As String
        pDatatype As String
        pParameters As Long
        pSecurityDescriptor As Long ' Pointer to 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
    Private udtPrinterInfo As PRINTER_INFO_2
    Maybe one of the gurus can explain how this works...
    Building A Better Body Albeit Left Out Under Intense Extrapolation

  3. #3
    Addicted Member Babbalouie's Avatar
    Join Date
    Jan 2001
    Location
    On the bright, blue sea...
    Posts
    197
    Oh yeah...add this on too!

    Code:
    Private Type DRIVER_INFO_3
        cVersion As Long                   '  2 (Driver for Windows NT 4.0)
        pName As String                    '  QMS 810
        pEnvironment As String             '  Win32 x86
        pDriverPath As String              '  c:\drivers\pscript.dll
        pDataFile As String                '  c:\drivers\QMS810.PPD
        pConfigFile As String              '  c:\drivers\PSCRPTUI.DLL
        pHelpFile As String                '  c:\drivers\PSCRPTUI.HLP
        pDependentFiles As String          '
        pMonitorName As String             '  "PJL monitor"
        pDefaultDataType As String         '  "EMF"
    End Type
    Private udtDriverInfo As DRIVER_INFO_3
    Building A Better Body Albeit Left Out Under Intense Extrapolation

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    53

    addprinter

    Hi Babbalouie,


    Thanks for the code.I am total newbie to VB,i gave a try.

    I copied the module code to module,made declaration for the addprinter, copyfile, closeprinter, getprinterdriverdirectory, addprinterdriver.

    Created one function errorhandler with 2 arguments.created errorhandler addprintererror: to the code.


    when I ruin the prog it gave me the error

    Constant expresiion required on line where the code goes...

    Dim sDriverBuf As String * Max_Path

    also
    There is code statement
    --
    If (Dir(sDriverPath & DRIVER_FILE, vbReadOnly) <> "") Then
    --
    do I need to create function Dir.


    Babbalouie, will these code help me to get printers on the network ,will it help me in some way to add network printer.

    Could U please guide me,how to achieve it.

    Thanks for the help.


    Milind.
    Last edited by Milind; Jan 31st, 2001 at 12:44 AM.

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