C # Call WIN32API Series 2 to list Intranet shared printers

Source: Internet
Author: User

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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.