|
列出服务器上的打印机 |
来源:转载 人气:1804 录入时间:2007-11-8 |
组件逻辑
这个组件以“硬方式”编写以便说明如何在VB中调用Wivdons API, 如果你使用VB5中的printer 类也可以实现相同的功能。
这个组件调用了由Windows2000使用的EnumPrinters函数。这个函数以五种格式之一返回一个打印机列表,格式是由你确定的“等级”而定。在这个例子里,我们使用第四级,它只是列举了我们NT服务器所连接的打印机名字和位置(直接连接或网络连接)。
注意
在这里显示的组件代码只适用于Windows NT,如果你在Windows95上运行Personal Web Server的话,使用第五级。Windows 95将网络打印机当作本地打印机处理。
第五级的打印结构同第四级的有略微的不同之处,使用VB5 API浏览器将PRINTER_INFO_5结构体复制到你的代码中。
这是一个功能强大的API调用,可带许多不同的参数。Visual C的帮助文件提供如何使用EnumPrinters函数的细节,但将C形式的语法翻译成VB语句可能具有挑战胜。你可以从www.microsoft.com网点中下载Microsoft Knowledge Base article Q166008来获得如何翻译的额外细节信息。
我们将返回信息存在一个长整数组中因为它比其它更低级的储存结构更便于操作和索引。通过PtrToStr和StrLen调用,我们能够将长整数数组转换成字符串,这种结果在VB中最容易使用。
当确定了有多少打印机与系统相连,我们redim(重定义)两个数组(m_aDeviceName和m_ServerName)来存储所发现打印机数目。如果没有发现打印机,这些代码将不会执行,因为错误处理代码将接管程序的执行。
编写源代码
再一次启动一个新的VB6 ActiveX DLL项目,VB显示了一个缺省的代码窗口名叫Class1,按F4,按以下修改属性表:
(Name)WebPrinters
Instancing 5 - MultiUse
选择Projects |Project 1 Properties将项目名改为Web Utils,并选择Unattended Execution。选项最后最后单击对话框的Make表栏并选择Auto Increment,按OK键保存设置。
下面代码包含了打印机组件的源代码,这个组件只是列举了你系统上的打印机——你可以扩展其代码事完成实际的打印任务。不象先前的VB组件,这个例子使用VB的Get 和Let属性。对这些属性的讨论超过了本例的范围,但Microsoft’s Visual Books Online(与VB5捆绑在一起)包含了这两个属性的深层次解释。
Option Explicit
Private Declare Function EnumPrinters Lib "winspool.drv" _
Alias "EnumPrintersA" _
(ByVal flags As Long, ByVal name As String, _
ByVal Level As Long, pPrinterEnum As Long, _
ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) _
As Long
Private Declare Function PtrToStr Lib "Kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long
Private Declare Function StrLen Lib "Kernel32" Alias "lstrlenA" _
(ByVal Ptr As Long) As Long
Private Type PRINTER_INFO_4
pPrinterName As String
pServerName As String
Attributes As Long
End Type
Private Const PRINTER_ENUM_LOCAL = &H2
Private Const PRINTER_ENUM_CONNECTIONS = &H4
Private Const PRINTER_ENUM_NAME = &H8
Private Const PRINTER_ENUM_NETWORK = &H40
Private Const PRINTER_ENUM_REMOTE = &H10
Private Const PRINTER_ENUM_SHARED = &H20
Private m_iPrinterCount As Integer
Private m_aDeviceName() As String
Private m_aServer() As String
Private Property Let PrinterCount(iValue As Integer)
m_iPrinterCount = iValue
End Property
Public Property Get PrinterCount() As Integer
PrinterCount = m_iPrinterCount
End Property
Private Property Let DeviceName(iIndex As Integer, sValue As String)
m_aDeviceName(iIndex) = sValue
End Property
Public Property Get DeviceName(iIndex As Integer) As String
DeviceName = m_aDeviceName(iIndex)
End Property
Private Property Let Server(iIndex As Integer, sValue As String)
m_aServer(iIndex) = sValue
End Property
Public Property Get Server(iIndex As Integer) As String
Server = m_aServer(iIndex)
End Property
Private Sub Class_Initialize()
Dim bReturn As Boolean
Dim lFlags As Long
Dim sName As String
Dim lLevel As Long
Dim lBuffer() As Long
Dim lCdBuf As Long
Dim lPcbNeeded As Long
Dim lEntries As Long
Dim i As Integer
Dim lTemp As Long
Dim sTempString As String
lFlags = PRINTER_ENUM_CONNECTIONS Or PRINTER_ENUM_LOCAL
sName = vbNullString
lLevel = 4
lCdBuf = 3072
ReDim lBuffer((lCdBuf \ 4) - 1) As Long
bReturn = EnumPrinters(lFlags, sName, lLevel, lBuffer(0), lCdBuf, lPcbNeeded, lEntries)
PrinterCount = lEntries ' Number Printers Found
ReDim m_aDeviceName(PrinterCount) As String
ReDim m_aServer(PrinterCount) As String
ReDim m_aPaperBin(PrinterCount) As String
ReDim m_aPaperSize(PrinterCount) As String
For i = 0 To PrinterCount - 1 ' Set the Device Name
sTempString = Space(StrLen(lBuffer(i * 3)))
lTemp = PtrToStr(sTempString, lBuffer(i * 3))
DeviceName(i) = sTempString ' Set the Server Name
sTempString = Space(StrLen(lBuffer(i * 3 + 1)))
lTemp = PtrToStr(sTempString, lBuffer(i * 3 + 1))
Server(i) = sTempString
Next i
End Sub
创建DLL
将你的工作保存到磁盘上,例如:c:\Utils。
选择File|Make WebUtils DLL来创建DLL。这个组件将保存在同你保存源代码相同路径下。
在命令行中通过以下命令注册DLL。
RegSvr32 c:\Utils\WebUtils.dll
在页面中使用组件
下面显示的ASP代码将列出与你系统相连的所有打印机。通过改写这些代码,你可以添加一个下拉列表框,使得用户在其中选择一个打印机,然后将他们的选择传递给你自己的定制组件,将报告输出到所选的打印机上。
Printers.asp
--------------------------------------------------------------------------------
<% Option Explicit %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <TITLE>System Printers</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF"><FONT SIZE="2" FACE="Verdana, Arial, Helvetica">
<CENTER><H1>Web Server Printers</H1></CENTER><HR Color="Red">
<%
Dim oSystemPrinters
Dim i
Set oSystemPrinters = Server.CreateObject("WebUtils.WebPrinters")
%>
There are <%=oSystemPrinters.PrinterCount%> Server printers<BR>
<%
i = oSystemPrinters.PrinterCount
If i > 0 Then
For i = 0 to i - 1
%>
Printer <% =cstr(i + 1) %>: <%=oSystemPrinters.deviceName(cint(i))%><BR>
<%
Next
End If
Set oSystemPrinters = Nothing ' De-reference the object
%>
</BODY>
</HTML>
|
|
|