阅读:2935回复:2
求教VB下的AddPrinterDriver及AddPrinter
各位大侠,小弟最近想用VB调用API安装一个系统自带的"Generic / Text Only"打印机,查找INF文件夹下的ntprint.inf文件,得到
[Generic] "Generic / Text Only" = TTY.GPD ,GenericGeneric_/_Tex8040,Generic_/_Text_Only [TTY.GPD] CopyFiles=@TTYRES.DLL,@TTY.INI,@TTY.DLL,@TTYUI.DLL,@TTY.GPD,@TTYUI.HLP,UNIDRV DataSection=UNIDRV_DATA 但不知用AddPrinterDriver怎样安装此驱动? 我用向导手动安装了。再用AddPrinter能成功安装打印机。下面是我的代码 Option Explicit 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 Long pSepFile As String pPrintProcessor As String pDatatype As String pParameters As String pSecurityDescriptor As Long Attributes As Long Priority As Long DefaultPriority As Long StartTime As Long UntilTime As Long Status As Long JobsCount As Long AveragePPM As Long End Type Private Type DRIVER_INFO_3 cVersion As Long pName As String pEnvironment As String pDriverPath As String pDataFile As String pConfigFile As String pHelpFile As String pDependentFiles As String pMonitorName As String pDefaultDataType As String End Type Private Declare Function AddPrinterDriver Lib "winspool.drv" Alias "AddPrinterDriverA" (ByVal pName As String, ByVal Level As Long, pDriverInfo As Any) As Long Private Declare Function AddPrinter Lib "winspool.drv" Alias "AddPrinterA" (ByVal pName As String, ByVal Level As Long, ByRef pPrinter As Any) As Long Private Const PRINTER_ATTRIBUTE_DEFAULT = &H4 Private Const PRINTER_ATTRIBUTE_DIRECT = &H2 Private Const PRINTER_ATTRIBUTE_ENABLE_BIDI = &H800 Private Const PRINTER_ATTRIBUTE_HIDDEN = &H20 Private Const PRINTER_ATTRIBUTE_LOCAL = &H40 Private Const PRINTER_ATTRIBUTE_NETWORK = &H10 Private Const PRINTER_ATTRIBUTE_QUEUED = &H1 Private Const PRINTER_ATTRIBUTE_SHARED = &H8 Private Const PRINTER_ATTRIBUTE_WORK_OFFLINE = &H400 Private Sub cmdAddDriver_Click() Dim driverInfo As DRIVER_INFO_3 With driverInfo .cVersion = 3 .pName = "Generic / Text Only" .pEnvironment = "Windows NT x86" .pDriverPath = "UNIDRV.DLL" .pDataFile = "TTY.GPD" .pConfigFile = "UNIDRVUI.DLL" .pHelpFile = "TTYUI.HLP" .pDefaultDataType = "RAW" End With Dim stat As Long stat = AddPrinterDriver("", 3&, driverInfo) MsgBox stat ‘添加不成功,老是0,不知哪里错了。 End Sub Private Sub cmdAddPrinter_Click() '添加打印机,假如驱动已经安装了,此过程可以成功添加打印机。 Dim printInfo As PRINTER_INFO_2 printInfo.pPrinterName = "myPrinter" '打印机名 printInfo.pPortName = "USB001" '端口名称 printInfo.pShareName = "mySharePrinter" '共享名 printInfo.pDriverName = "Generic / Text Only" '驱动类型 printInfo.pPrintProcessor = "WinPrint" '驱动处理 printInfo.pDatatype = "RAW" '数据类型 printInfo.pLocation = "at yidie computer" '位置说明 printInfo.pComment = "a generic / text only printer" '简要说明 printInfo.Attributes = PRINTER_ATTRIBUTE_LOCAL Or PRINTER_ATTRIBUTE_SHARED 'AddPrinter第一个参数是主机名,本机用"" If AddPrinter("", 2&, printInfo) <> 0 Then MsgBox "打印机" & printInfo.pPrinterName & "已添加" Else MsgBox "添加打印机失败" End If End Sub |
|
|
沙发#
发布于:2007-07-11 17:05
改成下面的,仍然返回 0 不行,调用GetLastError,返回 0 ,ERRORAPI
哪位大侠帮帮忙啊。没有VB的,给个C的也行,只要能添加成功。 Public Function AddDriver() As Boolean Dim driverInfo As DRIVER_INFO_3 Dim driverPath$, pdepend$ driverPath="C:\WINDOWS\system32\spool\drivers\w32x86\3\" pdepend = driverPath & "ttyres.dll" & vbNullChar & _ driverPath & "tty.ini" & vbNullChar & _ driverPath & "tty.dll" & vbNullChar & _ driverPath & "ttyui.dll" & vbNullChar & _ driverPath & "unires.dll" & vbNullChar & _ driverPath & "ttyui.hlp" & vbNullChar & _ driverPath & "stdnames.gpd" & vbNullChar & vbNullChar With driverInfo .cVersion = 3 .pname = "yidiePrinter" .pDriverPath = driverPath & "unidrv.dll" .pConfigFile = driverPath & "unidrvui.dll" .pDataFile = driverPath & "tty.gpd" .pEnvironment = "windows nt x86" .pDependentFiles = pdepend End With Dim lp As Long lp = AddPrinterDriver("", 3&, driverInfo) Debug.Print lp '仍然返回 0 AddDriver=(lp>0) End Function |
|
|
板凳#
发布于:2007-07-27 11:41
自已解决了
先把驱动所需的文件先拷贝到GetPrinterDriverDirectory得到的目录下 ADD_DRIVER_INFO_3 结构变量的文件赋值都不带路径就行了 PUBLIC Type ADD_DRIVER_INFO_3 cVersion As Long pName As String pEnvironment As String pDriverPath As String pDataFile As String pConfigFile As String pHelpFile As String pDependentFiles As Long pMonitorName As String pDefaultDataType As String End Type Public Function AddDriver() As Boolean Dim driverInfo As ADD_DRIVER_INFO_3 Dim sDependFiles As String Dim buff() As Byte sDependFiles = "ttyres.dll" & vbNullChar & _ "tty.ini" & vbNullChar & _ "tty.dll" & vbNullChar & _ "ttyui.dll" & vbNullChar & _ "unires.dll" & vbNullChar & _ "ttyui.hlp" & vbNullChar & _ "stdnames.gpd" & vbNullChar & vbNullChar buff = StrConv(sDependFiles, vbFromUnicode) With driverInfo .cVersion = 3 .pName = pName .pEnvironment = "windows nt x86" .pDriverPath = "unidrv.dll" .pDataFile = "tty.gpd" .pConfigFile = "unidrvui.dll" .pHelpFile = "unidrv.hlp" .pDependentFiles = VarPtr(buff(0)) End With Dim lp As Long lp = AddPrinterDriver("", 3&, driverInfo) AddDriver=(lp>0) End Function |
|
|