1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
Option Explicit
Option Base 0
''*******************************************************
'*
'* WINDOWS API
'*
'*******************************************************
'-------------------------------------------------------
' Printer API
'-------------------------------------------------------
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
'-------------------------------------------------------
' DRIVER API
'-------------------------------------------------------
Public Declare Function AddPrinterDriver Lib "winspool.drv" Alias "AddPrinterDriverA" (ByVal pName As String, ByVal Level As Long, pDriverInfo As Any) As Long
Public 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
'-------------------------------------------------------
' Others API
'-------------------------------------------------------
Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As Long
Public Declare Function lstrcp2 Lib "kernel32" Alias "lstrcpyA" (lpString1 As String, lpString2 As Byte) As Long
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
'-------------------------------------------------------
' Constants
'-------------------------------------------------------
Public Declare Function GetLastError Lib "Kernel32.dll" () 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)
'-------------------------------------------------------
' Set Default Printer Class
'-------------------------------------------------------
Private cSetPrinter As New cSetDfltPrinter
'-------------------------------------------------------
' Global Variables
'-------------------------------------------------------
Global FeedBack As Boolean
Global hPrinter As Long
Global PrtName As String
Global cEnvi As String
Global cVers As Integer
Global SetAsDflt As Boolean
Global PrinterDefs As PRINTER_DEFAULTS
Global NumFichDispo As Integer
Sub Delay(HowLong As Date)
Dim TempTime As Date
TempTime = DateAdd("s", HowLong, Now)
While TempTime > Now
DoEvents 'Allows windows to handle other stuff
Wend
End Sub
'*******************************************************
'*
'* M A I N C O D E
'*
'*******************************************************
Sub Main()
'-------------------------------------------------------
' Local VAriables
'-------------------------------------------------------
Dim OriPath, DrvPath, sMsg, PpdFile As String
Dim DrvBool, PrtBool As Boolean
Dim DriverFile(5) As String
Dim i, j, k, l, m As Integer
Dim PaperSize As String
Dim Paper As String
'-------------------------------------------------------
' Analyse & Parse Command Line = [INSTALLDIR],[PRTNAME],[SETASDFLT],[FEEDBACK],[PPDFILE]
'-------------------------------------------------------
PrtName = "PRISMAsatellite"
OriPath = "C:\Temp\"
SetAsDflt = True
'FeedBack = True
PpdFile = "smart.ppd"
''MsgBox ("Command = " + Command)
If (Command <> "") Then
i = InStr(Command, ",")
If (i > 0) Then
OriPath = Left(Command, i - 1)
j = InStr(i + 1, Command, ",")
If (j > 0) Then
PrtName = Mid(Command, i + 1, j - i - 1)
k = InStr(j + 1, Command, ",")
If (k > 0) Then
SetAsDflt = CBool(Val(Mid(Command, j + 1, k - j - 1)))
FeedBack = CBool(Val(Right(Command, 1)))
l = InStr(k + 1, Command, ",")
If (l > 0) Then
m = InStr(l + 1, Command, ",")
''MsgBox ("k = " + Str$(k))
''MsgBox ("l = " + Str$(l))
''MsgBox ("m = " + Str$(m))
If (m > 0) Then
PaperSize = Mid(Command, l + 1, m - l - 1)
''MsgBox ("PpdFile = " + PpdFile)
If Not (PaperSize = "Letter") Then
PaperSize = "A4"
End If
End If
End If
End If
End If
End If
End If
''MsgBox ("PpdFile = " + PpdFile)
''MsgBox ("OriPath = " + OriPath)
FeedBack = True
PrtName = "PRISMAsatellite"
PaperSize = "A4"
SetAsDflt = True
If Not (PaperSize = "Letter") Then
PaperSize = "A4"
End If
Paper = PaperSize
If (FeedBack) Then
NumFichDispo = FreeFile
Open "c:\trace.log" For Output As NumFichDispo
'MsgBox "PrinterName=" + PrtName + "; Set as Dflt=" + CStr(SetAsDflt)
Print #NumFichDispo, "Command = " + Command
End If
'-------------------------------------------------------
' Check If another Printer with same name exists
'-------------------------------------------------------
Call CheckPrinter
'-------------------------------------------------------
' Init Driver's Files
'-------------------------------------------------------
Call getVersion
If (cEnvi <> "Windows NT 4.0") Then
DriverFile(1) = "PSCRIPT5.DLL"
DriverFile(2) = PpdFile
DriverFile(3) = "PS5UI.DLL"
DriverFile(4) = "PSCRIPT.HLP"
DriverFile(5) = "PSCRIPT.NTF"
If (FeedBack) Then
Print #NumFichDispo, "it is not Windows NT 4.0"
End If
Else
DriverFile(1) = "PSCRIPT.DLL"
DriverFile(2) = PpdFile
DriverFile(3) = "PSCRPTUI.DLL"
DriverFile(4) = "PSCRIPT.HLP"
DriverFile(5) = "PSCRIPT.NTF"
If (FeedBack) Then
Print #NumFichDispo, "it is Windows NT 4.0"
End If
End If
'-------------------------------------------------------
' GetDriverDirectory
'-------------------------------------------------------
If (Right(OriPath, 1) = "\") Then
OriPath = OriPath + "executables\"
Else
OriPath = OriPath + "\executables\"
End If
''MsgBox "Driver Orig Path " & OriPath
DrvPath = GetDriverDir() & "\"
''MsgBox "After : Driver Orig Path " & OriPath
If (FeedBack) Then
'MsgBox "Driver Orig Path " & OriPath
'MsgBox "Driver Dest Path " & DrvPath
End If
'-------------------------------------------------------
' Copy Files to Driver directory
'-------------------------------------------------------
For i = 1 To 5
''MsgBox "file to delete: " & DrvPath & DriverFile(i)
''MsgBox "file to copy: " & OriPath & DriverFile(i)
On Error Resume Next
Kill (DrvPath & DriverFile(i))
On Error GoTo 0
If (CopyFile(OriPath & DriverFile(i), DrvPath & DriverFile(i), False)) Then
If (FeedBack) Then
'MsgBox "Error. File " & DrvPath & DriverFile(i) & " not copied!"
End If
End If
Next i
'-------------------------------------------------------
' The parameters for CreatePrinter are:
' Driver Filename
'
' Data Filename
' Config Filename
' Help Filename
' Dependent file(s) name(s)
'
'-------------------------------------------------------
'DrvBool = CreateDriver(DrvPath & DriverFile(1), _
' DrvPath & DriverFile(2), _
' DrvPath & DriverFile(3), _
' DrvPath & DriverFile(4), _
' DrvPath & DriverFile(5) & vbNullString & vbNullString)
DrvBool = CreateDriver(OriPath & DriverFile(1), _
OriPath & DriverFile(2), _
OriPath & DriverFile(3), _
OriPath & DriverFile(4), _
OriPath & DriverFile(5) & vbNullString & vbNullString)
If (DrvBool = True) Then
If (FeedBack) Then
MsgBox "Driver Creation: " & DrvBool
End If
Else
If (FeedBack) Then
MsgBox "Driver Creation: " & DrvBool
End If
Exit Sub
End If
'-------------------------------------------------------
' 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
'
'-------------------------------------------------------
PrtBool = CreatePrinter("", _
PrtName, _
"Oce PRISMAsatellite Port", _
"PRISMAsatellite", _
"WinPrint")
If (FeedBack) Then
Print #NumFichDispo, "CreatePrinter : " & PrtName
Print #NumFichDispo, "CreatePrinter return code : " & PrtBool
'MsgBox "Printer Creation: " & PrtBool
End If
'-------------------------------------------------------
' Set the printer as default one
'-------------------------------------------------------
'Removed for ADS deployement
If (SetAsDflt = True) Then
If cSetPrinter.SetPrinterAsDefault(PrtName) Then
sMsg = PrtName & " has successfully been set as the default printer."
Else
sMsg = PrtName & " has failed to be set as the default printer."
End If
If (FeedBack) Then
Print #NumFichDispo, sMsg
'MsgBox sMsg
End If
End If
'PrtBool = SetPaperSize(PrtName, PaperSize)
Dim wrk As Integer
If Paper = "A4" Then
wrk = 9
Else
wrk = 1
End If
If (FeedBack) Then
Print #NumFichDispo, "[Main] : PaperSize = " & Paper
''MsgBox "PaperSize = " & Paper
End If
Delay (6)
PrtBool = cSetPrinter.SetPaperSize(PrtName, wrk)
If (FeedBack) Then
Print #NumFichDispo, "[Main] : End of addprinter.exe."
Print #NumFichDispo, "SetPaperSize return code : " & PrtBool
Close NumFichDispo
End If
'chb 28/06/2007
MsgBox " The operation has been done. The trace is on the root drive c:\."
End Sub
''*******************************************************
'*
'* Create Driver Windows API
'*
'*******************************************************
Function CreateDriver(strDriver As String, strData As String, strConfig As String, strHelp As String, strDependent As String)
'-------------------------------------------------------
' Local Variables
'-------------------------------------------------------
Dim pDriverInfo As DRIVER_INFO_3
Dim Result As Long
Dim bBuffer(1000) As Byte
Dim i
'-------------------------------------------------------
' Initialize the Variables
'-------------------------------------------------------
For i = 0 To UBound(bBuffer)
bBuffer(i) = 0
Next
'-------------------------------------------------------
Call getVersion
If (FeedBack) Then
'MsgBox ("cVersion = " & cVers)
'MsgBox ("cEnviron = " & cEnvi)
End If
'-------------------------------------------------------
pDriverInfo.cVersion = cVers
pDriverInfo.pName = "PRISMASatellite"
If (cEnvi = "Windows NT 4.0") Then
cEnvi = "Windows NT x86"
End If
pDriverInfo.pEnvironment = cEnvi
pDriverInfo.pDriverPath = strDriver
pDriverInfo.pDataFile = strData
pDriverInfo.pConfigFile = strConfig
pDriverInfo.pHelpFile = strHelp
pDriverInfo.pDependentFiles = strDependent
pDriverInfo.pMonitorName = ""
pDriverInfo.pDefaultDataType = "RAW"
'-------------------------------------------------------
' Add the DRiver in Windows / Registry
'-------------------------------------------------------
Result = AddPrinterDriver("", 3, pDriverInfo)
'ERREUR CODE DE RETOUR 126
MsgBox "Information " & Err.LastDllError()
MsgBox Result
Print #NumFichDispo, "[CreatePrinter] : AddrinterDriver : " & Result
If Result <> 0 Then
CreateDriver = True
Else
If (FeedBack) Then
'MsgBox ("Error code is :" & Err.LastDllError)
Print #NumFichDispo, "- Erreur function create driver -"
End If
CreateDriver = False
End If
End Function
'*******************************************************
'*
'* Create Printer Windows API
'*
'*******************************************************
Function CreatePrinter(strServer As String, ByVal strPrinter As String, strPort As String, strDriver As String, strPrintProcessor As String) As Boolean
'-------------------------------------------------------
' Local Variables
'-------------------------------------------------------
Dim hPrinter As Long
Dim pi2 As PRINTER_INFO_2
Dim bBuffer(1000) As Byte
Dim i
'-------------------------------------------------------
' Initialize the Variables
'-------------------------------------------------------
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)
'-------------------------------------------------------
If (FeedBack) Then
Print #NumFichDispo, "[CreatePrinter] : pPrinterName : " & strPrinter
Print #NumFichDispo, "[CreatePrinter] : pPortName : " & strPort
Print #NumFichDispo, "[CreatePrinter] : pDriverName : " & strDriver
Print #NumFichDispo, "[CreatePrinter] : pPrintProcessor : " & strPrintProcessor
End If
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 DRiver in Windows / Registry
'-------------------------------------------------------
If (FeedBack) Then
Print #NumFichDispo, "[CreatePrinter] : before AddPrinter"
End If
hPrinter = AddPrinter(strServer, 2, pi2)
If (FeedBack) Then
Print #NumFichDispo, "[CreatePrinter] : after AddPrinter + return code :" & hPrinter
End If
If hPrinter <> 0 Then
CreatePrinter = True
Else
If (FeedBack) Then
Print #NumFichDispo, "[CreatePrinter] : addprinter returns 0, last error : " & Err.LastDllError
Print #NumFichDispo, "[CreatePrinter] : " & Err.LastDllError
'MsgBox ("Error code is :" & Err.LastDllError)
End If
CreatePrinter = False
End If
If (FeedBack) Then
Print #NumFichDispo, "[CreatePrinter] : End function"
End If
End Function |
Partager