已知此打印机的驱动程序libbz2.dll、oem.gpd、Oemsetup.inf、OEMUNI.dll、
oemuni.ini、stdnames.gpd,手工添加打印机好使。源码如下,但不好用,我怀疑PRINTER_INFO_2中DriverName属性指定不对
请指教Option Explicit
Option Base 0Public 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 TypePublic 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 DeletePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As LongPublic Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As LongPublic 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
' [email protected]
' 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")
MsgBox "Printer Creation: " & CreatePrinter(vbNullString, "V2 Bitmap Print Driver for 2000", "LPT1:", "E:\v2_LwClient\new_copy\oem.gpd", "pcb3-2000")
'*********************************************************************
' Test Call
' Parameter is the name of the printer to delete
'
' For printers on another server use the UNC path ie \\Server\Printer
'********************************************************************* MsgBox "Printer Deletion: " & RemovePrinter("New Printer")End SubFunction 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'*********************************************************************
' Initialise our buffer
'********************************************************************* For i = 0 To UBound(bBuffer)
bBuffer(i) = 0
Next'*********************************************************************
' Set the pointers to the string values in bBuffer
'********************************************************************* pi2.pPrinterName = AddString(strPrinter, bBuffer)
pi2.pPortName = AddString(strPort, bBuffer)
pi2.pDriverName = AddString(strDriver, bBuffer)
pi2.pPrintProcessor = AddString(strPrintProcessor, bBuffer)
'*********************************************************************
' Default all other values to 0 (NULL)
'********************************************************************* 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 printer
' If success then close the returned handle
'********************************************************************* hPrinter = AddPrinter(strServer, 2, pi2)
If hPrinter <> 0 Then
ClosePrinter (hPrinter)
CreatePrinter = True
Else
CreatePrinter = False
End If
End FunctionPrivate Function AddString(strString As String, ByRef bBuffer() As Byte) As Long
'*********************************************************************
' AddString copies a string into a Byte array and returns a long
' pointer to that string
'********************************************************************* 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 FunctionFunction RemovePrinter(strPrinter As String)
Dim hPrinter As Long
Dim pd As PRINTER_DEFAULTS'*********************************************************************
' Create a PRINTER_DEFAULTS structure with the required access rights
'********************************************************************* pd.pDatatype = 0
pd.pDevMode = 0
pd.DesiredAccess = PRINTER_ALL_ACCESS
'*********************************************************************
' Open the printer
'********************************************************************* If OpenPrinter(strPrinter, hPrinter, pd) = 0 Then
RemovePrinter = False
Exit Function
End If
'*********************************************************************
' Remove it
'********************************************************************* If DeletePrinter(hPrinter) = 0 Then
RemovePrinter = False
Exit Function
End If
'*********************************************************************
' Release the handle
'********************************************************************* ClosePrinter (hPrinter)
RemovePrinter = True
End Function
oemuni.ini、stdnames.gpd,手工添加打印机好使。源码如下,但不好用,我怀疑PRINTER_INFO_2中DriverName属性指定不对
请指教Option Explicit
Option Base 0Public 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 TypePublic 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 DeletePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As LongPublic Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Long, ByVal lpString2 As String) As LongPublic 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
' [email protected]
' 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")
MsgBox "Printer Creation: " & CreatePrinter(vbNullString, "V2 Bitmap Print Driver for 2000", "LPT1:", "E:\v2_LwClient\new_copy\oem.gpd", "pcb3-2000")
'*********************************************************************
' Test Call
' Parameter is the name of the printer to delete
'
' For printers on another server use the UNC path ie \\Server\Printer
'********************************************************************* MsgBox "Printer Deletion: " & RemovePrinter("New Printer")End SubFunction 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'*********************************************************************
' Initialise our buffer
'********************************************************************* For i = 0 To UBound(bBuffer)
bBuffer(i) = 0
Next'*********************************************************************
' Set the pointers to the string values in bBuffer
'********************************************************************* pi2.pPrinterName = AddString(strPrinter, bBuffer)
pi2.pPortName = AddString(strPort, bBuffer)
pi2.pDriverName = AddString(strDriver, bBuffer)
pi2.pPrintProcessor = AddString(strPrintProcessor, bBuffer)
'*********************************************************************
' Default all other values to 0 (NULL)
'********************************************************************* 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 printer
' If success then close the returned handle
'********************************************************************* hPrinter = AddPrinter(strServer, 2, pi2)
If hPrinter <> 0 Then
ClosePrinter (hPrinter)
CreatePrinter = True
Else
CreatePrinter = False
End If
End FunctionPrivate Function AddString(strString As String, ByRef bBuffer() As Byte) As Long
'*********************************************************************
' AddString copies a string into a Byte array and returns a long
' pointer to that string
'********************************************************************* 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 FunctionFunction RemovePrinter(strPrinter As String)
Dim hPrinter As Long
Dim pd As PRINTER_DEFAULTS'*********************************************************************
' Create a PRINTER_DEFAULTS structure with the required access rights
'********************************************************************* pd.pDatatype = 0
pd.pDevMode = 0
pd.DesiredAccess = PRINTER_ALL_ACCESS
'*********************************************************************
' Open the printer
'********************************************************************* If OpenPrinter(strPrinter, hPrinter, pd) = 0 Then
RemovePrinter = False
Exit Function
End If
'*********************************************************************
' Remove it
'********************************************************************* If DeletePrinter(hPrinter) = 0 Then
RemovePrinter = False
Exit Function
End If
'*********************************************************************
' Release the handle
'********************************************************************* ClosePrinter (hPrinter)
RemovePrinter = True
End Function
获取指针?小朋友,虽然说字符是用首字节地址表示的,但是也不用在vb里弄的那么复杂吧!你随便找个带字符输入的api函数的例子看看就知道了
再说了,vb用的string是unicode,要传给c style api的时候要转换的.你也没有转啊!不要那么麻烦,该传string的时候你就声明为 as string
比如下面的
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 DEVMODE
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As 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然后直接把vb的字符串用上去,什么都不用调整
vb runtime会在调用的时候自己把该转换的转了