C# 使用WIN32API来获取打印机

  主要使用到winspool.drv中的EnumPrinters函数,代码如下:

  [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)]

  [return: MarshalAs(UnmanagedType.Bool)]

  private static extern bool EnumPrinters ([MarshalAs(UnmanagedType.U4)] PRINTER_ENUM flags,

  [MarshalAs(UnmanagedType.LPStr)] string sName,

  uint iLevel,

  IntPtr pPrinterDesc,

  uint iSize,

  [MarshalAs(UnmanagedType.U4)] ref uint iNeeded,

  [MarshalAs(UnmanagedType.U4)] ref uint iReturned

  );   

  说明:Marshal属性提供了对托管代码与非托管代码见数据封送。  

  EnumPrinters 的 WIN32 API的定义如下:   

  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 pcbNeeded, // bytes received or required

  LPDWORD pcReturned // number of printers enumerated

  );

你可能感兴趣的:(api,object,String,C#,buffer,Types)