正在阅读:Delphi必学技巧:枚举打印机Delphi必学技巧:枚举打印机

2004-12-15 16:51 出处:CSDN 作者:apexchu 责任编辑:linjixiong


  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
  );
  #endregion

  #region GetPrinterInfoFromMemory
  private PrinterInfo[] GetPrinterInfoFromMemory(IntPtr prInfo, uint numPrinters)
  {
  PRINTER_INFO_1 pi = new PRINTER_INFO_1();
  
  PrinterInfo[] pInfo = new PrinterInfo[numPrinters];

  for(int i = 0; i < numPrinters; i++)
  {
  //把数据从非托管内存传送到到托管内存
  Marshal.PtrToStructure(prInfo, pi);
  
  pInfo[i].Name = Marshal.PtrToStringAuto(pi.pName);
  pInfo[i].Description = Marshal.PtrToStringAuto(pi.pDescription);
  pInfo[i].Comment = Marshal.PtrToStringAuto(pi.pComment);

  prInfo =  new IntPtr(prInfo.ToInt32() + Marshal.SizeOf(typeof(PRINTER_INFO_1)));
  }
  return pInfo;
  }

  private void ThrowEnumPrinterException()
  {
  throw new EnumPrinterException(string.Format("LastErrorCode: {0}",
  Marshal.GetLastWin32Error()));
  }
  #endregion
  }
  }


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
键盘也能翻页,试试“← →”键

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品