Win32API プリンタドライバを列挙する

インストールされているプリンタドライバ情報を列挙するためには、EnumPrinterDrivers関数を用いる。

BOOL EnumPrinterDrivers(
  LPTSTR pName,         // サーバー名へのポインタ
  LPTSTR pEnvironment, // 環境名へのポインタ
  DWORD Level,          // 構造体のレベル
  LPBYTE pDriverInfo,  // 構造体配列へのポインタ
  DWORD cbBuf,          // 配列のサイズ (バイト数)
  LPDWORD pcbNeeded,   // コピーされた (または必要な) バイト数へのポインタ
  LPDWORD pcReturned   // DRIVER_INFO 構造体の数へのポインタ
);



LevelでpDriverInfoが指す構造体の種類を指定する。DRIVER_INFO_1構造体, DRIVER_INFO_2構造体, DRIVER_INFO_3構造体 を指定するためにはLevelにそれぞれ1, 2, 3を指定する。




  • Levelを1として、DRIVER_INFO_1構造体を用いるサンプルプログラム
  • #include <windows.h>
    #include <stdio.h>
    
    #include <winspool.h>
    #pragma comment(lib, "winspool.lib")
    
    int main()
    {
        DWORD dwNeeded;
        LPBYTE lpDriverInfo;
        DWORD dwReturned;
        DRIVER_INFO_1 *pInfo;
        DWORD i;
    
        //第5引数cbBufに0を指定し、dwNeededにプリンタドライバ情報を
        //格納するための必要なバッファサイズを得る
        EnumPrinterDrivers(NULL,
            NULL,
            1,
            NULL,
            0,
            &dwNeeded,
            &dwReturned);
            
        //プリンタドライバ情報を格納するためのバッファを確保
        lpDriverInfo = (LPBYTE)LocalAlloc(LPTR, dwNeeded);
        if (lpDriverInfo == NULL) {
            return 0;
        }
    
        //プリンタドライバ情報を取得
        EnumPrinterDrivers(NULL,
            NULL,
            1,
            lpDriverInfo,
            dwNeeded,
            &dwNeeded,
            &dwReturned);
       
        pInfo = (DRIVER_INFO_1 *)lpDriverInfo;
    
        for (i = 0; i < dwReturned; i++) {
            wprintf(L"%s\n", pInfo->pName);
            pInfo++;
        }
    
        LocalFree(lpDriverInfo);
    
        return 0;
    }


  • 実行結果
  • PDF Complete Converter
    Microsoft XPS Document Writer
    Microsoft Shared Fax Driver
    HP Officejet H470 series
    Adobe PDF Converter







  • Levelを2として、DRIVER_INFO_2構造体を用いるサンプルプログラム
  • #include <windows.h>
    #include <stdio.h>
    
    #include <winspool.h>
    #pragma comment(lib, "winspool.lib")
    
    int main()
    {
        DWORD dwNeeded;
        LPBYTE lpDriverInfo;
        DWORD dwReturned;
        DRIVER_INFO_2 *pInfo;
        DWORD i;
    
        EnumPrinterDrivers(NULL,
            NULL,
            2,
            NULL,
            0,
            &dwNeeded,
            &dwReturned);
            
    
        lpDriverInfo = (LPBYTE)LocalAlloc(LPTR, dwNeeded);
        if (lpDriverInfo == NULL) {
            return 0;
        }
    
        EnumPrinterDrivers(NULL,
            NULL,
            2,
            lpDriverInfo,
            dwNeeded,
            &dwNeeded,
            &dwReturned);
       
        pInfo = (DRIVER_INFO_2 *)lpDriverInfo;
    
        for (i = 0; i < dwReturned; i++) {
            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);
            pInfo++;
        }
    
        LocalFree(lpDriverInfo);
    
        return 0;
    }



  • 実行結果
  • --------------------------------------------------------------
    Name: PDF Complete Converter
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_ui.dll
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_ui.dll
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_core.dll
    Environment: Windows x64
    
    --------------------------------------------------------------
    Name: Microsoft XPS Document Writer
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\unidrvui.dll
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdui.gpd
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
    Environment: Windows x64
    
    --------------------------------------------------------------
    Name: Microsoft Shared Fax Driver
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\FXSDRV.DLL
    Environment: Windows x64
    
    --------------------------------------------------------------
    Name: HP Officejet H470 series
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRVUI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\hph47003.GPD
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRV.DLL
    Environment: Windows x64
    
    --------------------------------------------------------------
    Name: Adobe PDF Converter
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\PS5UI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\ADPDF9J.PPD
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\PSCRIPT5.DLL
    Environment: Windows x64
    








  • Levelを3として、DRIVER_INFO_3構造体を用いるサンプルプログラム
  • #include <windows.h>
    #include <stdio.h>
    
    #include <winspool.h>
    #pragma comment(lib, "winspool.lib")
    
    int main()
    {
        DWORD dwNeeded;
        LPBYTE lpDriverInfo;
        DWORD dwReturned;
        DRIVER_INFO_3 *pInfo;
        DWORD i;
    
        EnumPrinterDrivers(NULL,
            NULL,
            3,
            NULL,
            0,
            &dwNeeded,
            &dwReturned);
            
    
        lpDriverInfo = (LPBYTE)LocalAlloc(LPTR, dwNeeded);
        if (lpDriverInfo == NULL) {
            return 0;
        }
    
        EnumPrinterDrivers(NULL,
            NULL,
            3,
            lpDriverInfo,
            dwNeeded,
            &dwNeeded,
            &dwReturned);
       
        pInfo = (DRIVER_INFO_3 *)lpDriverInfo;
    
        for (i = 0; i < dwReturned; i++) {
            
            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);
    
            pInfo++;
        }
    
        LocalFree(lpDriverInfo);
    
        return 0;
    }


  • 実行結果
  • --------------------------------------------------------------
    Name: PDF Complete Converter
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_ui.dll
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_ui.dll
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_core.dll
    Environment: Windows x64
    DefaultDataType: RAW
    DependentFiles: C:\Windows\system32\spool\DRIVERS\x64\3\pdfc_recv.dll
    HelpFile: C:\Windows\system32\spool\DRIVERS\x64\3\pdfcstd.chm
    MonitorName: (null)
    
    --------------------------------------------------------------
    Name: Microsoft XPS Document Writer
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\unidrvui.dll
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdui.gpd
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
    Environment: Windows x64
    DefaultDataType: (null)
    DependentFiles: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdui.ini
    HelpFile: C:\Windows\system32\spool\DRIVERS\x64\3\unidrv.hlp
    MonitorName: (null)
    
    --------------------------------------------------------------
    Name: Microsoft Shared Fax Driver
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\FXSDRV.DLL
    Environment: Windows x64
    DefaultDataType: (null)
    DependentFiles: C:\Windows\system32\spool\DRIVERS\x64\3\FXSWZRD.DLL
    HelpFile: (null)
    MonitorName: (null)
    
    --------------------------------------------------------------
    Name: HP Officejet H470 series
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRVUI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\hph47003.GPD
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRV.DLL
    Environment: Windows x64
    DefaultDataType: (null)
    DependentFiles: C:\Windows\system32\spool\DRIVERS\x64\3\hph4700s.ini
    HelpFile: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRV.HLP
    MonitorName: PCL hpz3l5k2
    
    --------------------------------------------------------------
    Name: Adobe PDF Converter
    Version: 3
    ConfigFile: C:\Windows\system32\spool\DRIVERS\x64\3\PS5UI.DLL
    DataFile: C:\Windows\system32\spool\DRIVERS\x64\3\ADPDF9J.PPD
    DriverPath: C:\Windows\system32\spool\DRIVERS\x64\3\PSCRIPT5.DLL
    Environment: Windows x64
    DefaultDataType: (null)
    DependentFiles: C:\Windows\system32\spool\DRIVERS\x64\3\ADUIGP.DLL
    HelpFile: C:\Windows\system32\spool\DRIVERS\x64\3\PSCRIPT.HLP
    MonitorName: (null)
    





    参考
    http://msdn.microsoft.com/ja-jp/library/cc428569.aspx EnumPrinterDrivers
    http://msdn.microsoft.com/ja-jp/library/dd162501.aspx DRIVER_INFO_1 structure
    http://msdn.microsoft.com/ja-jp/library/dd162502.aspx DRIVER_INFO_2 structure
    http://msdn.microsoft.com/ja-jp/library/dd162503.aspx DRIVER_INFO_3 structure