|
-
Feb 15th, 2001, 05:53 AM
#1
Thread Starter
Member
addprinter
Hi all,
I got this code from forum.
could anyone help me out...
I need to add one network printer(POSTSCRIPT).
---------------
'# comment by milind
'Public Function AddPrinter1(ByVal sName$) As Boolean
'
' Dim lRes As Long
'
' 'lRes = AddPrinterConnection(UCase(sName))
' lRes = AddPrinterConnection(sName)
'
'End Function
'
'Private Sub Command1_Click()
'MsgBox ("now add")
'AddPrinter1 ("\\MORPHIUS\draft")
'
'End Sub
'=====================
Public Sub Command1_Click()
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
'---------------------
'# IT gives error constant expression required for Max_Path
'------------
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
Addprintererror:
MsgBox ("hi printer error")
End Sub
----------------------------------------
The code in the module goes these way.
-----------------------------------------------------
'Declare Function AddPrinterConnection Lib "winspool.drv" Alias "AddPrinterConnectionA" (ByVal pName As String) As Long
'Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterA" (ByVal pName As String, ByVal Level As Long, pPrinter As Any) As Long
'=============
Declare Function GetPrinterDriverDirectory Lib "winspool.drv" Alias "GetPrinterDriverDirectoryA" (ByVal pName As String, ByVal pEnvironment As String, ByVal Level As Long, pDriverDirectory As Byte, ByVal cdBuf As Long, pcbNeeded As Long) As Long
Declare Function AddPrinterDriver Lib "winspool.drv" Alias "AddPrinterDriverA" (ByVal pName As String, ByVal Level As Long, pDriverInfo As Any) As Long
Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterA" (ByVal pName As String, ByVal Level As Long, pPrinter As Any) As Long
Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
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
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
Public Function errorhandler(ByVal msgval As String, ByVal errdesc As String)
MsgBox (msgval)
MsgBox (errdesc)
End Function
-----------------------
ANY GURUS...
Thanks,
Milind.
Last edited by Milind; Feb 15th, 2001 at 05:57 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|