Convert to VBA to VB.NET Need some help here. (DeviceCapabilites API via winspool.drv)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Can I get some help on how to convert the API Declare statement and any other statements I need to update for VB.NET?
I dont expect any miracles but some help of where to get started would help. Thanks!



<div style="color:black; background-color:white
<pre><span style="color:green Declaration for the DeviceCapabilities function API call.
<span style="color:blue Private <span style="color:blue Declare <span style="color:blue Function DeviceCapabilities <span style="color:blue Lib <span style="color:#a31515 "winspool.drv" _
<span style="color:blue Alias <span style="color:#a31515 "DeviceCapabilitiesA" (<span style="color:blue ByVal lpsDeviceName <span style="color:blue As <span style="color:blue String, _
<span style="color:blue ByVal lpPort <span style="color:blue As <span style="color:blue String, <span style="color:blue ByVal iIndex <span style="color:blue As <span style="color:blue Long, lpOutput <span style="color:blue As <span style="color:blue Any, _
<span style="color:blue ByVal lpDevMode <span style="color:blue As <span style="color:blue Long) <span style="color:blue As <span style="color:blue Long

<span style="color:green DeviceCapabilities function constants.
<span style="color:blue Private <span style="color:blue Const DC_PAPERNAMES = 16
<span style="color:blue Private <span style="color:blue Const DC_PAPERS = 2
<span style="color:blue Private <span style="color:blue Const DC_BINNAMES = 12
<span style="color:blue Private <span style="color:blue Const DC_BINS = 6
<span style="color:blue Private <span style="color:blue Const DEFAULT_VALUES = 0



<span style="color:blue Sub GetPaperList()
<span style="color:blue Dim lngPaperCount <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim lngCounter <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim hPrinter <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim strDeviceName <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strDevicePort <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strPaperNamesList <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strPaperName <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim intLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim strMsg <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim aintNumPaper() <span style="color:blue As <span style="color:blue Integer

<span style="color:blue On <span style="color:blue Error <span style="color:blue GoTo GetPaperList_Err

<span style="color:green Get the name and port of the default printer.
strDeviceName = Application.Printer.DeviceName
strDevicePort = Application.Printer.Port

<span style="color:green Get the count of paper names supported by the printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_PAPERNAMES, _
lpOutput:=<span style="color:blue ByVal vbNullString, _
lpDevMode:=DEFAULT_VALUES)

<span style="color:green Re-dimension the array to the count of paper names.
<span style="color:blue ReDim aintNumPaper(1 <span style="color:blue To lngPaperCount)

<span style="color:green Pad the variable to accept 64 bytes for each paper name.
strPaperNamesList = <span style="color:blue String(64 * lngPaperCount, 0)

<span style="color:green Get the string buffer of all paper names supported by the printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_PAPERNAMES, _
lpOutput:=<span style="color:blue ByVal strPaperNamesList, _
lpDevMode:=DEFAULT_VALUES)

<span style="color:green Get the array of all paper numbers supported by the printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_PAPERS, _
lpOutput:=aintNumPaper(1), _
lpDevMode:=DEFAULT_VALUES)

<span style="color:green List the available paper names.
strMsg = <span style="color:#a31515 "Papers available for " & strDeviceName & vbCrLf
<span style="color:blue For lngCounter = 1 <span style="color:blue To lngPaperCount

<span style="color:green Parse a paper name from the string buffer.
strPaperName = Mid(<span style="color:blue String:=strPaperNamesList, _
Start:=64 * (lngCounter - 1) + 1, Length:=64)
intLength = VBA.InStr(Start:=1, String1:=strPaperName, String2:=Chr(0)) - 1
strPaperName = Left(<span style="color:blue String:=strPaperName, Length:=intLength)

<span style="color:green Add a paper number and name to text string for the message box.
strMsg = strMsg & vbCrLf & aintNumPaper(lngCounter) _
& vbTab & strPaperName

<span style="color:blue Next lngCounter

<span style="color:green Show the paper names in a message box.
MsgBox Prompt:=strMsg

GetPaperList_End:
<span style="color:blue Exit <span style="color:blue Sub

GetPaperList_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:=<span style="color:#a31515 "Error Number " & Err.Number & <span style="color:#a31515 " Occurred"
<span style="color:blue Resume GetPaperList_End

<span style="color:blue End <span style="color:blue Sub

<span style="color:blue Sub GetBinList(strName <span style="color:blue As <span style="color:blue String)
<span style="color:green Uses the DeviceCapabilities API function to display a
<span style="color:green message box with the name of the default printer and a
<span style="color:green list of the paper bins it supports.

<span style="color:blue Dim lngBinCount <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim lngCounter <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim hPrinter <span style="color:blue As <span style="color:blue Long
<span style="color:blue Dim strDeviceName <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strDevicePort <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strBinNamesList <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim strBinName <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim intLength <span style="color:blue As <span style="color:blue Integer
<span style="color:blue Dim strMsg <span style="color:blue As <span style="color:blue String
<span style="color:blue Dim aintNumBin() <span style="color:blue As <span style="color:blue Integer

<span style="color:blue On <span style="color:blue Error <span style="color:blue GoTo GetBinList_Err

<span style="color:green Get name and port of the default printer.
strDeviceName = Application.Printers(strName).DeviceName
strDevicePort = Application.Printers(strName).Port

<span style="color:green Get count of paper bin names supported by the printer.
lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_BINNAMES, _
lpOutput:=<span style="color:blue ByVal vbNullString, _
lpDevMode:=DEFAULT_VALUES)

<span style="color:green Re-dimension the array to count of paper bins.
<span style="color:blue ReDim aintNumBin(1 <span style="color:blue To lngBinCount)

<span style="color:green Pad variable to accept 24 bytes for each bin name.
strBinNamesList = <span style="color:blue String(Number:=24 * lngBinCount, Character:=0)

<span style="color:green Get string buffer of paper bin names supported by the printer.
lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_BINNAMES, _
lpOutput:=<span style="color:blue ByVal strBinNamesList, _
lpDevMode:=DEFAULT_VALUES)

<span style="color:green Get array of paper bin numbers supported by the printer.
lngBinCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
lpPort:=strDevicePort, _
iIndex:=DC_BINS, _
lpOutput:=aintNumBin(1), _
lpDevMode:=0)

<span style="color:green List available paper bin names.
strMsg = <span style="color:#a31515 "Paper bins available for " & strDeviceName & vbCrLf
<span style="color:blue For lngCounter = 1 <span style="color:blue To lngBinCount

<span style="color:green Parse a paper bin name from string buffer.
strBinName = Mid(<span style="color:blue String:=strBinNamesList, _
Start:=24 * (lngCounter - 1) + 1, _
Length:=24)
intLength = VBA.InStr(Start:=1, _
String1:=strBinName, String2:=Chr(0)) - 1
strBinName = Left(<span style="color:blue String:=strBinName, _
Length:=intLength)

<span style="color:green Add bin name and number to text string for message box.
strMsg = strMsg & vbCrLf & aintNumBin(lngCounter) _
& vbTab & strBinName

<span style="color:blue Next lngCounter

<span style="color:green Show paper bin numbers and names in message box.
MsgBox Prompt:=strMsg

GetBinList_End:
<span style="color:blue Exit <span style="color:blue Sub
GetBinList_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
Title:=<span style="color:#a31515 "Error Number " & Err.Number & <span style="color:#a31515 " Occurred"
<span style="color:blue Resume GetBinList_End
<span style="color:blue End <span style="color:blue Sub


[/code]

<br/>
<br/>

<br/>

View the full article
 
Back
Top