Win32API 指定されたプリンタのプリンタドライバ情報を取得する

プリンタドライバ情報を取得するためには、GetPrinterDriver関数を用いる。



プリンタドライバ情報を取得する手順としては、

1.まずOpenPrinter関数でプリンタオブジェクトのハンドルを取得する

2.次に、取得したプリンタオブジェクトのハンドルを用いてGetPrinterDriver関数を呼び出す。

3.最後に、プリンタオブジェクトのハンドルを使い終わったらClosePrinter関数を用いてハンドルを閉じる。



OpenPrinter関数のプロトタイプは以下のとおり

BOOL OpenPrinter(
  LPTSTR pPrinterName,         // プリンタ名、またはプリントサーバー名
  LPHANDLE phPrinter,          // プリンタまたはプリントサーバーのハンドル
  LPPRINTER_DEFAULTS pDefault  // プリンタの既定値
);



GetPrinterDriver関数のプロトタイプは以下のとおり

BOOL GetPrinterDriver(
  HANDLE hPrinter,     // プリンタオブジェクト
  LPTSTR pEnvironment, // 環境のアドレス
  DWORD Level,          // 構造体のレベル
  LPBYTE pDriverInfo,  // 構造体配列のアドレス
  DWORD cbBuf,          // 配列のサイズ (バイト数)
  LPDWORD pcbNeeded    // 取得した (必要な) バイト数を受け取る変数のアドレス
);



構造体のレベルが「1」であれば、DRIVER_INFO_1構造体へのポインタを、
構造体のレベルが「2」であれば、DRIVER_INFO_2構造体へのポインタを、
構造体のレベルが「3」であれば、DRIVER_INFO_3構造体へのポインタを、
第4引数のpDriverInfoに指定する。



ここでは、DRIVER_INFO_3構造体の定義のみを示す。

typedef struct _DRIVER_INFO_3 {
  DWORD  cVersion;//ドライバが書かれているOSのバージョン。「3」がサポートされている
  LPTSTR pName;//指定されたドライバの名前
  LPTSTR pEnvironment;//ドライバが書かれた環境(例:Windows x86, Windows IA64, and Windows x64)
  LPTSTR pDriverPath;//デバイスドライバファイルのパス
  LPTSTR pDataFile;//ドライバデータファイルのパス
  LPTSTR pConfigFile;//デバイスドライバのDLLファイルのパス
  LPTSTR pHelpFile;//デバイスドライバのヘルプファイルのパス
  LPTSTR pDependentFiles;//ドライバが依存しているファイルの名前
  LPTSTR pMonitorName;//指定された言語モニター
  LPTSTR pDefaultDataType;//プリントジョブの既定のデータ型
} DRIVER_INFO_3, *PDRIVER_INFO_3;


ClosePrinter関数のプロトタイプは以下のとおり

BOOL ClosePrinter(
  HANDLE hPrinter   // プリンタオブジェクトのハンドル
);



GetPrinterDriver関数を使って、プリンタドライバの情報を取得する例を以下に示す。情報を取得するプリンタドライバは「Microsoft XPS Document Writer」で、構造体のレベルは「3」としている。

#include <windows.h>
#include <stdio.h>

#include <winspool.h>
#pragma comment(lib, "winspool.lib")

int main()
{
    HANDLE hPrinter;
    LPBYTE pDriverInfo = NULL;
    DWORD dwNeeded;
    DRIVER_INFO_3 *pInfo;

    OpenPrinter(TEXT("Microsoft XPS Document Writer"), &hPrinter, NULL);

    if (hPrinter == NULL) {
        return 1;
    }

    GetPrinterDriver(hPrinter,
        NULL,
        3,
        NULL,
        0,
        &dwNeeded);

    pDriverInfo = (LPBYTE)LocalAlloc(LPTR, dwNeeded);
    if (pDriverInfo == NULL) {
        ClosePrinter(hPrinter);
        return 1;
    }

    GetPrinterDriver(hPrinter,
        NULL,
        3,
        pDriverInfo,
        dwNeeded,
        &dwNeeded);

    pInfo = (DRIVER_INFO_3 *)pDriverInfo;

    wprintf(L"Name: %s\n", pInfo->pName);
    wprintf(L"Version: %d\n", pInfo->cVersion);
    wprintf(L"ConfigFile: %s\n", pInfo->pConfigFile);
    wprintf(L"DataFile: %s\n", pInfo->pDataFile);
    wprintf(L"DriverPath: %s\n", pInfo->pDriverPath);
    wprintf(L"Environment: %s\n", pInfo->pEnvironment);
    wprintf(L"DefaultDataType: %s\n", pInfo->pDefaultDataType);
    wprintf(L"DependentFiles: %s\n", pInfo->pDependentFiles);
    wprintf(L"HelpFile: %s\n", pInfo->pHelpFile);
    wprintf(L"MonitorName: %s\n", pInfo->pMonitorName);


    LocalFree(pDriverInfo);
    ClosePrinter(hPrinter);

    return 0;
}




実行結果(拙者の開発環境はWindows Vista)

Name: Microsoft XPS Document Writer
Version: 3
ConfigFile: C:\Windows\system32\spool\DRIVERS\W32X86\3\unidrvui.dll
DataFile: C:\Windows\system32\spool\DRIVERS\W32X86\3\mxdwdui.gpd
DriverPath: C:\Windows\system32\spool\DRIVERS\W32X86\3\mxdwdrv.dll
Environment: Windows NT x86
DefaultDataType: (null)
DependentFiles: C:\Windows\system32\spool\DRIVERS\W32X86\3\mxdwdui.ini
HelpFile: C:\Windows\system32\spool\DRIVERS\W32X86\3\unidrv.hlp
MonitorName: (null) 





参考

OpenPrinter関数
http://msdn.microsoft.com/ja-jp/library/cc410486.aspx

GetPrinterDriver関数
http://msdn.microsoft.com/ja-jp/library/cc410379.aspx

ClosePrinter関数
http://msdn.microsoft.com/ja-jp/library/cc428316.aspx

DRIVER_INFO_1構造体
http://msdn.microsoft.com/en-us/library/dd162501.aspx

DRIVER_INFO_2構造体
http://msdn.microsoft.com/en-us/library/dd162502.aspx

DRIVER_INFO_3構造体
http://msdn.microsoft.com/en-us/library/dd162503(v=vs.85).aspx