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