Print | server component logic
This component is written "hard" to show how to invoke the Wivdons API in VB, and if you use the printer class in VB5, you can achieve the same function.
This component invokes the EnumPrinters function that is used by Windows2000. This function returns a list of printers in one of five formats, which is determined by the "rank" you determine. In this example, we use level fourth, which simply enumerates the names and locations of the printers that our NT servers are connected to (direct or network connections).
Attention
The component code shown here applies only to Windows NT, and if you run personal Web server on Windows95, use level fifth. Windows 95 treats a network printer as a local printer.
The fifth-tier print structure is slightly different from level fourth, using the VB5 API browser to copy the PRINTER_INFO_5 structure to your code.
This is a powerful API call that can take many different parameters. Visual C's Help file provides details on how to use the EnumPrinters function, but translating C-form syntax into a VB statement can be challenging. You can download the Microsoft Knowledge Base article Q166008 from the www.microsoft.com network to obtain additional details about how to translate.
We will return the information in a long integer group because it is easier to manipulate and index than other lower-level storage structures. By Ptrtostr and Strlen calls, we are able to convert long integer arrays into strings, which are easiest to use in VB.
When we determine how many printers are connected to the system, we ReDim (redefine) two arrays (M_adevicename and M_servername) to store the number of discovered printers. If the printer is not found, the code will not execute because the error-handling code will take over the execution of the program.
Writing source code
Once again, start a new VB6 ActiveX DLL project, VB shows a default code window named Class1, press F4, press the following modify property sheet:
(Name) Webprinters
Instancing 5-multiuse
Select Projects | Project 1 Properties changes the item name to Web Utils and selects unattended Execution. Option finally click the Make Table Bar of the dialog box and select Auto Increment, and press OK to save the settings.
The following code contains the source code for the printer component, which simply lists the printers on your system-you can extend the code to complete the actual print task. Unlike the previous VB component, this example uses the Get and let properties of VB. The discussion of these properties is beyond the scope of this example, but Microsoft's Visual books Online (bundled with VB5) contains a deep explanation of these two attributes.
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 Lbuffe