プリンタのドライバ名を取得          <TOP>


OpenPrinter プリンタオブジェクトをオープン
GetPrinterDriver プリンタのドライバ情報を取得
ClosePrinter プリンタオブジェクトを閉じる
MoveMemory メモリの指定領域をコピー
lstrcpy 文字列をコピーする

 

 

'================================================================
'= プリンタのドライバ名を取得
'=    (GetPrinterDriver.bas)
'================================================================
#include "Windows.bi"

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

#define STANDARD_RIGHTS_REQUIRED &HF0000 '標準的な権利を要求することを示す定数
#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 DRIVER_INFO_2
    cVersion     As Long
    pName        As Long
    pEnvironment As Long
    pDriverPath  As Long
    pDataFile    As Long
    pConfigFile  As Long
End Type

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

' プリンタのドライバ情報を取得する関数の宣言
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&)

' 文字列をコピーする
Declare Function Api_lstrcpy& Lib "Kernel32" Alias "lstrcpyA" (lpszString1 As Any, lpszString2 As Any)

Var Shared Text1 As Object
Var Shared Text2 As Object
Var Shared Edit1 As Object
Var Shared Button1 As Object

Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Text2.Attach GetDlgItem("Text2") : Text2.SetFontSize 14
Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var PrtDevName As String
    Var pd As PRINTER_DEFAULTS
    Var di2 As DRIVER_INFO_2
    Var hPrinter As Long
    Var Level As Long
    Var Need As Long
    Var DrvInfo2Env As String * 128
    Var DrvInfo2Name As String * 128
    Var Ret As Long

    'プリンタ名を指定
    PrtDevName = Edit1.GetWindowText

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

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

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

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

    'バッファを確保
    Var Buffer(Need - 1) As Byte

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

    '取得したプリンタのドライバ情報を構造体へ
    MoveMemory di2, Buffer(0), Len(di2)
        
    Ret = Api_lstrcpy(DrvInfo2Name, ByVal di2.pName)

    '対応環境を切り出し
    Edit1.SetWindowtext PrtDevName
    Text2.SetWindowText Left$(DrvInfo2Name, InStr(DrvInfo2Name, Chr$(0)) - 1)

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

'================================================================
'=
'================================================================
Declare Sub Edit1_SetFocus edecl ()
Sub Edit1_SetFocus()
    Text2.SetWindowText ""
End Sub

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