プリンタの共有名を取得          <TOP>


プリンタの共有名を取得します。

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

 

 

'================================================================
'= プリンタの共有名を取得
'=    (GetPrinterShareName.bas)
'================================================================
#include "Windows.bi"

Type PRINTER_DEFAULTS
    pDataType     As Long
    pDevMode      As Long
    DesiredAccess As Long
End Type

Type PRINTER_INFO_2
    pServerName         As Long
    pPrinterName        As Long
    pShareName          As Long
    pPortName           As Long
    pDriverName         As Long
    pComment            As Long
    pLocation           As Long
    pDevMode            As Long
    pSepFile            As Long
    pPrintProcessor     As Long
    pDataType           As Long
    pParameters         As Long
    pSecurityDescriptor As Long
    Attributes          As Long
    Priority            As Long
    DefaultPriority     As Long
    StartTime           As Long
    UntilTime           As Long
    Status              As Long
    cJobs               As Long
    AveragePPM          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) '

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

' プリンタの詳細な情報を取得する関数の宣言
Declare Function Api_GetPrinter& Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter&, ByVal Level&, pPrinter As Any, ByVal cbBuf&, pcbNeeded&)

' メモリの指定領域をコピー
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)

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

Var Shared Text(3) As Object
Var Shared Button1 As Object

For i = 0 To 3
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    Text(i).SetFontSize 14
Next
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var DeviceName As String
    Var pd As PRINTER_DEFAULTS
    Var hWnd As Long
    Var Level As Long
    Var Need As Long
    Var pi2 As PRINTER_INFO_2
    Var ShareName As String * 128
    Var Ret As Long

    'プリンタ名を指定
    DeviceName = "magicolor 2400W"

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

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

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

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

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

    '詳細なプリンタ情報を取得
    Ret = Api_GetPrinter(hWnd, Level, pi2Buffer(0), Need, Need)

    '取得した詳細なプリンタ情報を構造体へ移動
    MoveMemory pi2, pi2Buffer(0), Len(pi2)

    '共有名を複写
    Ret = Api_lstrcpy(ShareName, ByVal pi2.pShareName)

    '共有名取得
    Text(3).SetWindowText Left$(ShareName, InStr(ShareName, Chr$(0)) - 1)

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

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