64位版本的DeviceCapabilities Lib“winspool.drv”

时间:2015-07-28 09:50:43

标签: vba 64-bit

winspool.drv库中是否有64位版本的Function DeviceCapabilities?我正在寻找的是转换: 私有声明函数DeviceCapabilities Lib“winspool.drv”_     别名“DeviceCapabilitiesA”(ByVal lpDeviceName As String,_     ByVal lpPort As String,ByVal iIndex Long,lpOutput As Any,_     ByVal dev As Long)As Long

显然,我将Declare Function更改为声明PtrSafe函数但是哪个Long变量更改并且它们是否更改为LongLong或LongPtr?奇怪的是,过去一小时内互联网的拖网没有提到任何对此的引用?

2 个答案:

答案 0 :(得分:0)

Programmatically retrieve printer capabilities

我在Microsoft Access中修改了此链接代码以使用64位。 并且,通过执行参考设置“ Microsoft ACCESS XX.0对象库”,我修改了以下代码以在Microsoft Excel中工作。 但是,以下代码是一个不同的代码:那就是原始代码

For lngCounter = 1 To lngPaperCount

但是,此代码将导致错误。 通过执行减一来避免此错误的发生。

For lngCounter = 1 To lngPaperCount -1

您可能会想到这样的以下代码,但是代码也会导致错误。

For lngCounter = 0 To lngPaperCount

我不知道是打印机导致错误还是64位Microsoft Office导致错误。

Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function DeviceCapabilities Lib "winspool.drv" _
Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, _
ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
ByVal lpDevMode As Long) As Long
#Else
' Declaration for the DeviceCapabilities function API call.
Private Declare Function DeviceCapabilities Lib "winspool.drv" _
Alias "DeviceCapabilitiesA" (ByVal lpsDeviceName As String, _
ByVal lpPort As String, ByVal iIndex As Long, lpOutput As Any, _
ByVal lpDevMode As Long) As Long
#End If
' DeviceCapabilities function constants.
Private Const DC_PAPERNAMES = 16
Private Const DC_PAPERS = 2
Private Const DC_BINNAMES = 12
Private Const DC_BINS = 6
Private Const DEFAULT_VALUES = 0

Sub GetPaperList()
Dim lngPaperCount As Long
Dim lngCounter As Long
Dim hPrinter As Long
Dim strDeviceName As String
Dim strDevicePort As String
Dim strPaperNamesList As String
Dim strPaperName As String
Dim intLength As Integer
Dim strMsg As String
Dim aintNumPaper() As Integer

On Error GoTo GetPaperList_Err

' Get the name and port of the default printer.
strDeviceName = Access.Application.Printer.DeviceName
strDevicePort = Access.Application.Printer.Port

' Get the count of paper names supported by the printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_PAPERNAMES, _
    lpOutput:=ByVal vbNullString, _
    lpDevMode:=DEFAULT_VALUES)

' Re-dimension the array to the count of paper names.
ReDim aintNumPaper(1 To lngPaperCount)

' Pad the variable to accept 64 bytes for each paper name.
strPaperNamesList = String(64 * lngPaperCount, 0)

' Get the string buffer of all paper names supported by the printer.
lngPaperCount = DeviceCapabilities(lpsDeviceName:=strDeviceName, _
    lpPort:=strDevicePort, _
    iIndex:=DC_PAPERNAMES, _
    lpOutput:=ByVal strPaperNamesList, _
    lpDevMode:=DEFAULT_VALUES)

' 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)

' List the available paper names.
strMsg = "Papers available for " & strDeviceName & vbCrLf
For lngCounter = 1 To lngPaperCount

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

    ' Add a paper number and name to text string for the message box.
    strMsg = strMsg & vbCrLf & aintNumPaper(lngCounter) _
        & vbTab & strPaperName

Next lngCounter

' Show the paper names in a message box.
MsgBox Prompt:=strMsg

GetPaperList_End:
Exit Sub

GetPaperList_Err:
MsgBox Prompt:=Err.Description, Buttons:=vbCritical & vbOKOnly, _
    Title:="Error Number " & Err.Number & " Occurred"
Resume GetPaperList_End

End Sub

答案 1 :(得分:-1)

我现在使用上述函数声明如下:

私有声明PtrSafe功能DeviceCapabilities Lib“winspool.drv”_         别名“DeviceCapabilitiesA”(ByVal lpDeviceName As String,_         ByVal lpPort As String,ByVal iIndex Long,lpOutput As Any,_         ByVal dev As Long)As Long

用于处理API代码行的函数 sCurrentPrinter = Trim $(左$(ActivePrinter,InStr(ActivePrinter,“on”))) 需要改为 sCurrentPrinter = ActivePrinter