|
使用windows打印机名
[DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)]string strPrinterName, out IntPtr ptrPrinter, IntPtr ptrDefalut); [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool ClosePrinter(IntPtr ptrPrinter); [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool AddJob(IntPtr ptrPrinter, Int32 iLevel, IntPtr ptrJob, Int32 iSize, out Int32 iCpSize); [DllImport("winspool.drv", CharSet = CharSet.Auto)] public static extern bool ScheduleJob(IntPtr ptrPrinter, Int32 JobID); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct ADDJOB_INFO_1 { [MarshalAs(UnmanagedType.LPTStr)] public string lpPath; public Int32 JobID; } /// /// 发送数据到打印机 /// 使用windows打印机名 /// /// 打印机 /// 数据 public static void WinApiPrintByte(string p_PrintName, byte[] p_Byte) { if (p_PrintName != null && p_PrintName.Length > 0) { IntPtr _PrintHandle; IntPtr _JobHandle = Marshal.AllocHGlobal(100); if (OpenPrinter(p_PrintName, out _PrintHandle, IntPtr.Zero)) { ADDJOB_INFO_1 _JobInfo = new ADDJOB_INFO_1(); int _Size; AddJob(_PrintHandle, 1, _JobHandle, 100, out _Size); _JobInfo = (ADDJOB_INFO_1)Marshal.PtrToStructure(_JobHandle, typeof(ADDJOB_INFO_1)); // System.IO.File.WriteAllBytes(p_PrintName, p_Byte); System.IO.File.WriteAllBytes(_JobInfo.lpPath, p_Byte); ScheduleJob(_PrintHandle, _JobInfo.JobID); Thread.Sleep(20); ClosePrinter(_PrintHandle); Marshal.FreeHGlobal(_JobHandle); } } } |