Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
getprinterdriver (winspool)
TODO - a short description

C# Signature:

[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
static extern int GetPrinterDriver(IntPtr hPrinter, string pEnvironment, uint Level, IntPtr pDriverInfo, uint cbBuf, out uint pcbNeeded);

VB Signature:

User-Defined Types:









As always, only do SetLastError=true if you actually intend to call GetLastError.

Tips & Tricks:

Please add some!

Sample Code:

    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern int GetPrinterDriver(IntPtr hPrinter, string pEnvironment, uint Level, IntPtr pDriverInfo,
    int cbBuf, out int pcbNeeded);

    /// <summary>
    /// The .NET definition of the win32 DRIVER_INFO_8 structure.
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal class DRIVER_INFO_8
    public uint cVersion;
    public string pName;
    public string pEnvironment;
    public string pDriverPath;
    public string pDataFile;
    public string pConfigFile;
    public string pHelpFile;
    public string pDependentFiles;
    public string pMonitorName;
    public string pDefaultDataType;
    public string pszzPreviousNames;
    public ComTypes.FILETIME ftDriverDate;
    public UInt64 dwlDriverVersion;
    public string pszMfgName;
    public string pszOEMUrl;
    public string pszHardwareID;
    public string pszProvider;
    public string pszPrintProcessor;
    public string pszVendorSetup;
    public string pszzColorProfiles;
    public string pszInfPath;
    public uint dwPrinterDriverAttributes;
    public string pszzCoreDriverDependencies;
    public ComTypes.FILETIME ftMinInboxDriverVerDate;
    public UInt64 dwlMinInboxDriverVerVersion;

    /// <summary>
    /// This returns the data type to use so that we can just throw bytes at the printer. Recent printer drivers use
    /// the XPS driver model and require the data type to be XPS_PASS. For older drivers the data type needs to be
    /// RAW.
    /// </summary>
    /// <param name="printerHandle">The handle to a printer, returned by OpenPrinter.</param>
    /// <param name="dataType">The returned data type, XPS_PASS or RAW.</param>
    /// <returns>true if successful, otherwise false.</returns>
    private static bool GetDataType(IntPtr printerHandle, out string dataType)
        IntPtr driverInfo = new IntPtr(0);
        int needed;
        DRIVER_INFO_8 driverInfo8;

        dataType = null;
        // The first call to GetPrinterDriver is just to get the buffer length required
        if (NativeMethods.GetPrinterDriver(printerHandle, null, 8, driverInfo, 0, out needed) != 0)
        // There's something wrong if the above call doesn't fail with a zero length buffer
        return false;

        if (Marshal.GetLastWin32Error() != 122) // ERROR_INSUFFICIENT_BUFFER
        return false;

        driverInfo = Marshal.AllocHGlobal(needed);
        if (NativeMethods.GetPrinterDriver(printerHandle, null, 8, driverInfo, needed, out needed) == 0)
            return false;

        driverInfo8 = (DRIVER_INFO_8)Marshal.PtrToStructure(driverInfo, typeof(DRIVER_INFO_8));
        dataType = (driverInfo8.dwPrinterDriverAttributes & 2) == 2 ? "XPS_PASS" : "RAW"; // 2 = PRINTER_DRIVER_XPS
        return true;

Alternative Managed API:

Do you know one? Please contribute it!


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version