[DllImport("winspool.drv", SetLastError=true)]
public static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);
[DllImport("winspool.drv", SetLastError=true)]
public static extern bool GetPrinter(IntPtr hPrinter, uint dwLevel, IntPtr pPrinter, uint dwBuf, ref uint dwNeeded);
Public Declare Ansi Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByVal pPrinter As IntPtr, ByVal cbBuf As Integer, ByRef pcbNeeded As Integer) As Integer
None.
IntPtr pHandle;
PRINTER_DEFAULTS defaults = new PRINTER_DEFAULTS();
OpenPrinter(PrinterName, out pHandle, defaults);
uint cbNeeded = 0;
bool bRet = GetPrinter(pHandle, 2, IntPtr.Zero, 0, ref cbNeeded);
if (cbNeeded > 0)
{
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
bRet = GetPrinter(pHandle, 2, pAddr, cbNeeded, ref cbNeeded);
if (bRet)
{
PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
// Now use the info from Info2 structure etc
}
Marshal.FreeHGlobal(pAddr);
}
ClosePrinter(pHandle);
Dim PI2 As New PRINTER_INFO_2
Dim pBuf As IntPtr
Dim hPrinter As Integer
Dim cbNeeded As Integer
Dim rc As Integer
rc = OpenPrinter(sPrinterName, hPrinter, 0&)
If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
'The first call will fail, but it will return the length of the buffer we need to allocate
rc = GetPrinter(hPrinter, 2, Nothing, 0, cbNeeded)
'Allocate a memory buffer and fill the buffer with the printer info
pBuf = Marshal.AllocHGlobal(cbNeeded)
rc = GetPrinter(hPrinter, 2, pBuf, cbNeeded, cbNeeded)
If rc = 0 Then Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
'Convert the memory pointer to the printer info structre and free the pointer
PI2 = Marshal.PtrToStructure(pBuf, GetType(PRINTER_INFO_2))
Marshal.FreeHGlobal(pBuf)
ClosePrinter(hPrinter)
Do you know one? Please contribute it!