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.
GetPrinter (winspool)
.
C# Signature:
[DllImport("winspool.drv", SetLastError=true)]
public static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 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
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);
VB.NET Sample Code:
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)
Alternative Managed API:
Do you know one? Please contribute it!
Retrieves information about a specified printer.
6/22/2022 1:59:09 AM - -80.120.27.82
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
ByRef is a VB keyword that specifies a variable to be passed as a parameter BY REFERENCE. In other words, the pointer to the variable is passed and any change to its value made within the function or sub will change its value outside the function/sub.
4/25/2007 3:19:29 AM - anonymous
Used by the EnumPrinters call in Winspool.
9/13/2013 2:04:16 AM - -193.109.238.134
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).