列出服務(wù)器上的打印機
發(fā)表時間:2024-02-24 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]組件邏輯這個組件以“硬方式”編寫以便說明如何在VB中調(diào)用Wivdons API, 如果你使用VB5中的printer 類也可以實現(xiàn)相同的功能。這個組件調(diào)用了由Windows2000使用的EnumPrinters函數(shù)。這個函數(shù)以五種格式之一返回一個打印機列表,格式是由你確定的“等級”而定。在這個例子...
組件邏輯
這個組件以“硬方式”編寫以便說明如何在VB中調(diào)用Wivdons API, 如果你使用VB5中的printer 類也可以實現(xiàn)相同的功能。
這個組件調(diào)用了由Windows2000使用的EnumPrinters函數(shù)。這個函數(shù)以五種格式之一返回一個打印機列表,格式是由你確定的“等級”而定。在這個例子里,我們使用第四級,它只是列舉了我們NT服務(wù)器所連接的打印機名字和位置(直接連接或網(wǎng)絡(luò)連接)。
注意
在這里顯示的組件代碼只適用于Windows NT,如果你在Windows95上運行Personal Web Server的話,使用第五級。Windows 95將網(wǎng)絡(luò)打印機當(dāng)作本地打印機處理。
第五級的打印結(jié)構(gòu)同第四級的有略微的不同之處,使用VB5 API瀏覽器將PRINTER_INFO_5結(jié)構(gòu)體復(fù)制到你的代碼中。
這是一個功能強大的API調(diào)用,可帶許多不同的參數(shù)。Visual C的幫助文件提供如何使用EnumPrinters函數(shù)的細(xì)節(jié),但將C形式的語法翻譯成VB語句可能具有挑戰(zhàn)勝。你可以從www.microsoft.com網(wǎng)點中下載Microsoft Knowledge Base article Q166008來獲得如何翻譯的額外細(xì)節(jié)信息。
我們將返回信息存在一個長整數(shù)組中因為它比其它更低級的儲存結(jié)構(gòu)更便于操作和索引。通過PtrToStr和StrLen調(diào)用,我們能夠?qū)㈤L整數(shù)數(shù)組轉(zhuǎn)換成字符串,這種結(jié)果在VB中最容易使用。
當(dāng)確定了有多少打印機與系統(tǒng)相連,我們redim(重定義)兩個數(shù)組(m_aDeviceName和m_ServerName)來存儲所發(fā)現(xiàn)打印機數(shù)目。如果沒有發(fā)現(xiàn)打印機,這些代碼將不會執(zhí)行,因為錯誤處理代碼將接管程序的執(zhí)行。
編寫源代碼
再一次啟動一個新的VB6 ActiveX DLL項目,VB顯示了一個缺省的代碼窗口名叫Class1,按F4,按以下修改屬性表:
(Name)WebPrinters
Instancing 5 - MultiUse
選擇Projects Project 1 Properties將項目名改為Web Utils,并選擇Unattended Execution。選項最后最后單擊對話框的Make表欄并選擇Auto Increment,按OK鍵保存設(shè)置。
下面代碼包含了打印機組件的源代碼,這個組件只是列舉了你系統(tǒng)上的打印機——你可以擴展其代碼事完成實際的打印任務(wù)。不象先前的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
創(chuàng)建DLL
將你的工作保存到磁盤上,例如:c:\Utils。
選擇File Make WebUtils DLL來創(chuàng)建DLL。這個組件將保存在同你保存源代碼相同路徑下。
在命令行中通過以下命令注冊DLL。
RegSvr32 c:\Utils\WebUtils.dll
在頁面中使用組件
下面顯示的ASP代碼將列出與你系統(tǒng)相連的所有打印機。通過改寫這些代碼,你可以添加一個下拉列表框,使得用戶在其中選擇一個打印機,然后將他們的選擇傳遞給你自己的定制組件,將報告輸出到所選的打印機上。
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>