Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than winspool, prefix the name with the module name and a period.
GetPrinterDriver (winspool)
.
C# Signature:
[DllImport("winspool.drv", SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern bool GetPrinterDriver(IntPtr hPrinter,
[MarshalAs(UnmanagedType.LPTStr)] string environment,
uint level,
IntPtr driverInfo,
int cbBuf,
out uint pcbNeeded);
[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:
Declare Function GetPrinterDriver Lib "winspool.dll" (TODO) As TODO
As always, only do SetLastError=true if you actually intend to call GetLastError.
Tips & Tricks:
Please add some!
Sample Code:
public const uint DRIVER_INFO_8 = 8;
public const uint PRINTER_DRIVER_XPS = 0x00000002;
[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);
private String GetPrinterDriver(string printerName)
/// <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;
[MarshalAs(UnmanagedType.LPTStr)]
public string pName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pEnvironment;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDriverPath;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDataFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pConfigFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pHelpFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDependentFiles;
[MarshalAs(UnmanagedType.LPTStr)]
public string pMonitorName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDefaultDataType;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszzPreviousNames;
public ComTypes.FILETIME ftDriverDate;
public UInt64 dwlDriverVersion;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszMfgName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszOEMUrl;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszHardwareID;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszProvider;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszPrintProcessor;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszVendorSetup;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszzColorProfiles;
[MarshalAs(UnmanagedType.LPTStr)]
public string pszInfPath;
public uint dwPrinterDriverAttributes;
[MarshalAs(UnmanagedType.LPTStr)]
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 hPrinter = new IntPtr(0);
DocInfo1 di = new DocInfo1();
if (OpenPrinter(printerName.Normalize(), out hPrinter, 0))
{
DriverInfo8 driverInfo = new DriverInfo8();
IntPtr pDriverInfo = IntPtr.Zero;
uint bytesNeeded;
// assume type 3 driver
di.pDataType = "RAW";
GetPrinterDriver(hPrinter, null, DRIVER_INFO_8, pDriverInfo, 0, out bytesNeeded);
pDriverInfo = Marshal.AllocHGlobal((int)bytesNeeded);
if (GetPrinterDriver(hPrinter, null, DRIVER_INFO_8, pDriverInfo, (int)bytesNeeded, out bytesNeeded))
{
driverInfo = (DriverInfo8) Marshal.PtrToStructure(pDriverInfo, typeof(DriverInfo8));
// if type 4 driver
if ((driverInfo.dwPrinterDriverAttributes & PRINTER_DRIVER_XPS) != 0)
{
di.pDataType = "XPS_PASS";
}
}
IntPtr driverInfo = new IntPtr(0);
int needed;
DRIVER_INFO_8 driverInfo8;
Marshal.FreeHGlobal(pDriverInfo);
}
}
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;
}
try
{
driverInfo = Marshal.AllocHGlobal(needed);
if (NativeMethods.GetPrinterDriver(printerHandle, null, 8, driverInfo, needed, out needed) == 0)
{
return false;
}
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).