First, let's take a look at the definition of the EnumPrinters function.
BOOL EnumPrinters (
DWORD Flags, // printer object types
LPTSTR Name, // name of printer object
DWORD Level, // information level
LPBYTE pPrinterEnum, // printer information buffer
DWORD cbBuf, // size of printer information buffer
LPDWORD encoded needed, // bytes encoded ed or required
LPDWORD pcReturned // number of printers enumerated
);
This api has several response parameters. The most important one is that the buffer referred to by pPrinterEnum is
The structure array of PRINTER_INFO_N, where N changes according to the Level parameter. Here we use 1, so the structure used is
Typedef struct _ PRINTER_INFO_1 {
DWORD Flags;
LPTSTR pDescription;
LPTSTR pName;
LPTSTR pComment;
} PRINTER_INFO_1
C # introduce the dynamic library before calling the API. EnumPrinters is in the winspool. drv dynamic library. The import statement is as follows:
[DllImport ("winspool. drv", CharSet = CharSet. Auto)]
Then define the PRINTER_INFO_1 Structure
[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Struct PRINTER_INFO_1
{
Int flags;
[Financialas (UnmanagedType. LPTStr)]
Public string pDescription;
[Financialas (UnmanagedType. LPTStr)]
Public string pName;
[Financialas (UnmanagedType. LPTStr)]
Public string pComment;
}
All right, the source code is as follows:
Using System;
Using System. Collections;
Using System. Runtime. InteropServices;
Using System. Diagnostics;
Using System. Drawing. Printing;
Public class QuickTest {
[DllImport ("winspool. drv", CharSet = CharSet. Auto)]
Static extern bool EnumPrinters (int flags, string name, int level, IntPtr pPrinterEnum,
Int cbBuf, out int joined needed, out int pcReturned );
Private const int PRINTER_ENUM_NETWORK = 0x00000040;
Private const int PRINTER_ENUM_LOCAL = 0x00000002;
Private const int PRINTER_ENUM_REMOTE = 0x00000010;
[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Struct PRINTER_INFO_1
{
Int flags;
[Financialas (UnmanagedType. LPTStr)]
Public string pDescription;
[Financialas (UnmanagedType. LPTStr)]
Public string pName;
[Financialas (UnmanagedType. LPTStr)]
Public string pComment;
}
Public void EnumeratePrintersWin ()
{
Bool Success;
Int cbRequired;
Int nEntries;
IntPtr outb = IntPtr. Zero;
Success = EnumPrinters (PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null, 1, outb, 0, out cbRequired, out nEntries );
Outb = Marshal. AllocHGlobal (cbRequired );
Success = EnumPrinters (PRINTER_ENUM_NETWORK | PRINTER_ENUM_LOCAL | PRINTER_ENUM_REMOTE, null, 1, outb, cbRequired, out cbRequired, out nEntries );
PRINTER_INFO_1 [] portsArray = new PRINTER_INFO_1 [cbRequired];
IntPtr current = outb;
Try {
For (int I = 0; I <portsArray. Length; I ++)
{
PortsArray [I] = (PRINTER_INFO_1) Marshal. PtrToStructure (current,
Typeof (PRINTER_INFO_1 ));
Current = (IntPtr) (int) current + Marshal. SizeOf (typeof (PRINTER_INFO_1 )));
Console. writeLine (I + ":" + portsArray [I]. pName + "" + portsArray [I]. pDescription + "" + portsArray [I]. pComment + "");
}
}
Catch (Exception ){
// Console. WriteLine (exp. StackTrace );
}
Marshal. FreeHGlobal (outb );
}
Public QuickTest (){
}
Public static void Main (){
QuickTest qt = new QuickTest ();
Qt. EnumeratePrintersWin ();
}
}