プリンタの印刷ジョブを取得(U)          <TOP>


プリンタジョブ(保留状態)を取得します。

MoveMemory メモリブロックを別の領域に移動する

OpenPrinter プリンタオブジェクトをオープン

ClosePrinter プリンタオブジェクトを閉じる

EnumJobs プリンタの印刷ジョブを列挙

EnumPrinters 使用可能なプリンタ・プリントサーバーなどを列挙

 

コンボボックスでプリンタ名を指定し、タイマーにてキューを随時取得しています。

  

 

'================================================================
'= プリンタジョブの状態を表示
'=    (EnumJobs.bas)
'================================================================
#include "Windows.bi"

Type PRINTER_INFO_5
    pPrinterName As Long
    pPortName As Long
    Attributes As Long
    DeviceNotSelectedTimeOut As Long
    TransmissionRetryTimeOut As Long
End Type

' ある位置から別の位置にメモリブロックを移動する関数の宣言
Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)

' プリンタオブジェクトをオープン
Declare Function Api_OpenPrinter& Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName$, phPrinter&, pDefault As Any)

' プリンタオブジェクトを閉じる
Declare Function Api_ClosePrinter& Lib "winspool.drv" Alias "ClosePrinter" (ByVal hPrinter&)

' プリンタの印刷ジョブを列挙
Declare Function Api_EnumJobs& Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter&, ByVal FirstJob&, ByVal NoJobs&, ByVal Level&, pJob As Any, ByVal cdBuf&, pcblNeeded&, pcReturned&)

' 使用可能なプリンタ・プリントサーバーなどを列挙する
Declare Function Api_EnumPrinters& Lib "winspool.drv" Alias "EnumPrintersA" (ByVal Flags&, ByVal Name$, ByVal Level&, pPrinterEnum As Any, ByVal cbBuf&, pcbNeeded&, pcReturned&)

#define PRINTER_ENUM_DEFAULT &H1        'デフォルトのプリンタに関する情報を列挙
#define PRINTER_ENUM_LOCAL &H2          'Nameの設定を無視して、ローカルプリンタを列挙
#define PRINTER_ENUM_NAME &H8           'Nameで指定されたプリンタを列挙
#define PRINTER_ENUM_SHARED &H20        '共有属性を持つプリンタを列挙
#define MAX_DEVICENAME 64

Var Shared Timer1 As Object
Var Shared Text1 As Object
Var Shared Combo1 As Object

Timer1.Attach GetDlgItem("Timer1")
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14

Var Shared PrinterName As String

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var PrintServer As String
    Var Needed As Long
    Var Returned As Long
    Var Level As Long
    Var CT As Long
    Var Ret As Long

    'ローカルプリンタから検索
    PrintServer = ""

    'PRINTER_INFO_5構造体を受け取る
    Level = 5

    'バッファに必要なバイト数を調べる
    Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrintServer, Level, Chr$(0), 0, Needed, Returned)
    If Needed = 0 Then End

    '全プリンタ情報を得る
    Var Buffer(Needed - 1) As byte
    Ret = Api_EnumPrinters(PRINTER_ENUM_NAME, PrintServer, Level, Buffer(0), Needed, Needed, Returned)

    '構造体リストの準備
    Var PI_5(Returned - 1) As PRINTER_INFO_5

    For CT = 0 To Returned - 1

        'バッファから構造体1つ分を抜き取る
        CopyMemory PI_5(CT), Buffer(CT * Len(PI_5(CT))), Len(PI_5(CT))

        'プリンタ名を得る
        PrinterName = String$(MAX_DEVICENAME, Chr$(0))
        CopyMemory PrinterName, ByVal PI_5(CT).pPrinterName, Len(PrinterName)
        PrinterName = KLeft$(PrinterName, KInStr(1, PrinterName, Chr$(0)) - 1)

        Combo1.AddString PrinterName
    Next

    Timer1.SetInterval 50
    Timer1.Enable -1
End Sub

'================================================================
'=
'================================================================
Declare Sub Combo1_Change edecl ()
Sub Combo1_Change()
    Var hPrinter As Long
    Var Ret As Long

    PrinterName = Combo1.GetText(Combo1.GetCursel)
End Sub

'================================================================
'=
'================================================================
Declare Sub Timer1_Timer edecl ()
Sub Timer1_Timer()
    Var hPrinter As Long
    Var Needed As Long
    Var Returned As Long
    Var JobCount As Long
    Var Ret As Long

    Ret = Api_OpenPrinter(PrinterName, hPrinter, ByVal 0)
    Ret = Api_EnumJobs(hPrinter, 0, 99, 1, ByVal 0, 0, Needed, Returned)
    If Needed > 0 Then
        Var JobsBuffer(Needed - 1) As Byte
        Ret = Api_EnumJobs(hPrinter, 0, 99, 1, JobsBuffer(0), Needed, Needed, Returned)
        If Returned > 0 Then
            JobCount = Returned
        Else
            JobCount = 0
        End If
    Else
        JobCount = 0
    End If

    Ret = Api_ClosePrinter(hPrinter)

    Text1.SetWindowText "キューに" & Str$(JobCount) & " のドキュメント"
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End