protected override void Run() { if (PagedData != null) { IntPtr hPrinter; PRINTER_DEFAULTS defaults = new PRINTER_DEFAULTS { DesiredAccess = PRINTER_ACCESS_MASK.PRINTER_ACCESS_USE, pDatatype = "RAW" }; if (OpenPrinter(PrinterName, out hPrinter, ref defaults)) { DOC_INFO_1 docInfo = new DOC_INFO_1 { Datatype = "RAW", DocName = JobName, OutputFile = null }; uint jobid = StartDocPrinter(hPrinter, 1, ref docInfo); if (jobid < 0) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (PagedData.Prologue != null) { WritePrinter(hPrinter, PagedData.Prologue); } if (PagedData.PageData != null) { foreach (byte[] pagedata in PagedData.PageData) { StartPagePrinter(hPrinter); WritePrinter(hPrinter, pagedata); EndPagePrinter(hPrinter); } } if (PagedData.Epilogue != null) { WritePrinter(hPrinter, PagedData.Epilogue); } if (!EndDocPrinter(hPrinter)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } ClosePrinter(hPrinter); } } }
public static extern uint StartDocPrinter(int hPrinter, uint Level, ref DOC_INFO_1 pDocInfo);
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOC_INFO_1 di);
public static void SpoolFile(ref string sFile, ref string PrnName, ref string AppName = "") { int hPrn = 0; byte[] Buffer = null; short hFile = 0; int Written = 0; DOC_INFO_1 di = default(DOC_INFO_1); int i = 0; const int BufSize = 0x4000; AppName = "4POS Barcode"; // // Extract filename from passed spec, and build job name. // Fill remainder of DOC_INFO_1 structure. // if (Strings.InStr(sFile, "\\")) { for (i = Strings.Len(sFile); i >= 1; i += -1) { if (Strings.Mid(sFile, i, 1) == "\\") { break; // TODO: might not be correct. Was : Exit For } di.pDocName = Strings.Mid(sFile, i, 1) + di.pDocName; } } else { di.pDocName = sFile; } if (Strings.Len(AppName)) { di.pDocName = AppName; } di.pOutputFile = Constants.vbNullString; di.pDatatype = "RAW"; // // Open printer for output to obtain handle. // Set it up to begin recieving raw data. // OpenPrinter(PrnName, ref hPrn, ref 0); StartDocPrinter(hPrn, 1, ref di); StartPagePrinter(hPrn); // // Open file and pump it to the printer. // hFile = FreeFile(); FileSystem.FileOpen(hFile, sFile, OpenMode.Binary, OpenAccess.Read); // // Read in 16K buffers and spool. // //UPGRADE_WARNING: Lower bound of array Buffer was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"' Buffer = new byte[BufSize + 1]; for (i = 1; i <= FileSystem.LOF(hFile) / BufSize; i++) { //UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' FileSystem.FileGet(hFile, Buffer); WritePrinter(hPrn, ref Buffer[1], BufSize, ref Written); } // // Get last chunk of file if it doesn't // fit evenly into a 16K buffer. // if (FileSystem.LOF(hFile) % BufSize) { //UPGRADE_WARNING: Lower bound of array Buffer was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"' Buffer = new byte[(FileSystem.LOF(hFile) % BufSize) + 1]; //UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' FileSystem.FileGet(hFile, Buffer); WritePrinter(hPrn, ref Buffer[1], Information.UBound(Buffer), ref Written); } FileSystem.FileClose(hFile); // // Shut down spooling process. // EndPagePrinter(hPrn); EndDocPrinter(hPrn); ClosePrinter(hPrn); }
//UPGRADE_WARNING: Structure DOC_INFO_1 may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"' private static extern int StartDocPrinter(int hPrn, int Level, ref DOC_INFO_1 pDocInfo);
internal static extern bool StartDocPrinter(IntPtr hPrinter, int level, [In][MarshalAs(UnmanagedType.LPStruct)] DOC_INFO_1 di);
private static extern uint StartDocPrinter(IntPtr hPrinter, uint Level, ref DOC_INFO_1 pDocInfo);
public static bool PrintDocument(string printerName, string fileName) { FileStream fileStream = new FileStream(fileName, FileMode.Open); BinaryReader binaryReader = new BinaryReader(fileStream); Byte[] bytes = new Byte[fileStream.Length]; IntPtr pUnmanagedBytes = new IntPtr(0); Int32 nLength; UInt32 nWritten; PRINTER_DEFAULTS printerDefaults = new PRINTER_DEFAULTS(); DOC_INFO_1 docinfo1 = new DOC_INFO_1(); IntPtr hPrinter; int iRes; nLength = Convert.ToInt32(fileStream.Length); bytes = binaryReader.ReadBytes(nLength); pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength); Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength); printerDefaults.pDatatype = null; printerDefaults.pDevMode = null; printerDefaults.DesiredAccess = PRINTER_ACCESS_USE; iRes = OpenPrinter(printerName, out hPrinter, printerDefaults); if (iRes == 0) { Console.WriteLine("[-] OpenPrinter failed: {0}", Marshal.GetLastWin32Error()); Marshal.FreeCoTaskMem(pUnmanagedBytes); return(false); } docinfo1.pDocName = "RAW document"; docinfo1.pOutputFile = null; docinfo1.pDatatype = "RAW"; iRes = StartDocPrinter(hPrinter, 1, docinfo1); if (iRes == 0) { Console.WriteLine("[-] StartDocPrinter failed: {0}", Marshal.GetLastWin32Error()); ClosePrinter(hPrinter); Marshal.FreeCoTaskMem(pUnmanagedBytes); return(false); } iRes = StartPagePrinter(hPrinter); if (iRes == 0) { Console.WriteLine("[-] StartPagePrinter failed: {0}", Marshal.GetLastWin32Error()); EndDocPrinter(hPrinter); ClosePrinter(hPrinter); Marshal.FreeCoTaskMem(pUnmanagedBytes); return(false); } iRes = WritePrinter(hPrinter, pUnmanagedBytes, (uint)nLength, out nWritten); if (iRes == 0) { Console.WriteLine("[-] WritePrinter failed: {0}", Marshal.GetLastWin32Error()); EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); ClosePrinter(hPrinter); Marshal.FreeCoTaskMem(pUnmanagedBytes); return(false); } EndPagePrinter(hPrinter); EndDocPrinter(hPrinter); ClosePrinter(hPrinter); Marshal.FreeCoTaskMem(pUnmanagedBytes); return(true); }
private static extern int StartDocPrinter( IntPtr hPrinter, Int32 Level, DOC_INFO_1 pDocInfo );
public static extern bool StartDocPrinter(IntPtr phPrinter, int pgCount, DOC_INFO_1 docInfo);
private static extern int StartDocPrinter(int hPrn, int Level, ref DOC_INFO_1 pDocInfo);