プリンタのドライバ対応OSを取得          <TOP>


OpenPrinter プリンタオブジェクトをオープン
EnumPrinters 使用可能なプリンタ・プリントサーバーなどを列挙
GetPrinterDriver プリンタのドライバ情報を取得
ClosePrinter プリンタオブジェクトを閉じる
MoveMemory メモリの指定領域をコピー
 

 

'================================================================
'= プリンタのドライバ対応OSを取得
'=    (GetPrinterDriver2.bas)
'================================================================
#include "Windows.bi"

#define MAX_DEVICENAME 64                '
#define STANDARD_RIGHTS_REQUIRED &HF0000 '標準的な権利を要求することを示す
#define PRINTER_ENUM_NAME &H8            '
#define PRINTER_ACCESS_ADMINISTER &H4    'プリンタアクセス権の管理者権限を示す
#define PRINTER_ACCESS_USE &H8           'プリンタアクセス権のユーザー権限を示す
#define PRINTER_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE) '(&HF0000 or &H4 or &H8)

Type PRINTER_DEFAULTS
    pDatatype     As Long
    pDevMode      As Long
    DesiredAccess As Long
End Type

Type DRIVER_INFO_2
    cVersion     As Long
    pName        As Long
    pEnvironment As Long
    pDriverPath  As Long
    pDataFile    As Long
    pConfigFile  As Long
End Type

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

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

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

' プリンタのドライバ情報を取得する関数の宣言
Declare Function Api_GetPrinterDriver& Lib "winspool.drv" Alias "GetPrinterDriverA" (ByVal hPrinter&, ByVal pEnvironment$, ByVal Level&, pDriverInfo As Any, ByVal cdBuf&, pcbNeeded&)

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

' メモリの指定領域をコピー
Declare Sub MoveMemory Lib "Kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal length&)

Var Shared Text1 As Object
Var Shared Text2 As Object
Var Shared Combo1 As Object
Var Shared Button1 As Object

Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Text2.Attach GetDlgItem("Text2") : Text2.SetFontSize 14
Combo1.Attach GetDlgItem("Combo1") : Combo1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.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

    'ローカルプリンタから検索(サーバー名 \\xxx 指定可)
    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つ分を抜き取る
        MoveMemory PI_5(CT), Buffer(CT * Len(PI_5(CT))), Len(PI_5(CT))

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

        Combo1.AddString PrinterName
    Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var PrinterDeviceName As String
    Var pd As PRINTER_DEFAULTS
    Var di2 As DRIVER_INFO_2
    Var hPrinter As Long
    Var Level As Long
    Var Needed As Long
    Var Ret  As Long

    'プリンタ名を指定
    PrinterDeviceName = Combo1.GetText(Combo1.GetCursel)
    If PrinterDeviceName = "" Then Exit Sub

    'プリンタアクセス権を指定
    pd.DesiredAccess = PRINTER_ALL_ACCESS

    'プリンタのオブジェクトハンドルを取得
    Ret = Api_OpenPrinter(PrinterDeviceName, hPrinter, pd)

    '構造体のレベルを指定
    Level = 2

    'バッファに必要なサイズを取得
    Ret = Api_GetPrinterDriver(hPrinter, ByVal 0, Level, ByVal 0, 0, Needed)

    'バッファを確保
    Var Buffer(Needed - 1)

    'プリンタのドライバ情報を取得
    Ret = Api_GetPrinterDriver(hPrinter, ByVal 0, Level, Buffer(0), Needed, Needed)

    '取得したプリンタのドライバ情報を構造体へ移動
    MoveMemory di2, Buffer(0), Len(di2)

    'プリンタのドライバ対応OSを表示
    Select Case di2.cVersion
        Case 0
            Text2.SetWindowText "Windows 95 or Windows 98"
        Case 1
            Text2.SetWindowText "Windows NT 3.51"
        Case 2
            Text2.SetWindowText "Windows NT 4.0"
        Case 3
            Text2.SetWindowText "Windows 2000"
        Case Else
            Text2.SetWindowText "判定できません。"
    End Select

    'プリンタオブジェクトをクローズ
    Ret = Api_ClosePrinter(hPrinter)
End Sub

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