| 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 } } |