|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我用Socket方式接收一个tcp数据包存为byte数据,每次接收1024个字节,然后将byte转为16进制进行内容替换后再转为byte数据,再将byte数据用OpenPrinter、StartDocPrinter、StartPagePrinter、WritePrinter、EndPagePrinter、EndDocPrinter、ClosePrinter等Api写入打印机,但现在遇到一个问题,它会把原本同一数据写入到打印机驱动里就变成了若干个1024字节的任务,我是想让它在打印队列里还是一个完整的任务应该怎么做?这个数据可以通过判断接收数据的头部特征十六进制代码52454D4F5445315449就打开打印机句柄并写入打印机队列任务,如果数据里包含结尾特征十六进制代码4A450100001B000000就结束打印并关闭打印机句柄,而没有发现这两个代码的数据就一直让它追加写入同一个打印任务
- using System.Net.Sockets;
- using System.Net;
- using System.Runtime.InteropServices;
- namespace fsd
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
- public class DOCINFOA
- {
- [MarshalAs(UnmanagedType.LPStr)]
- public string pDocName;
- [MarshalAs(UnmanagedType.LPStr)]
- public string pOutputFile;
- [MarshalAs(UnmanagedType.LPStr)]
- public string pDataType;
- }
- [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
- [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool ClosePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
- [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool EndDocPrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool StartPagePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool EndPagePrinter(IntPtr hPrinter);
- [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
- public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
- private static bool SendBytesToPrinter(string printerName, IntPtr intptrBytes, Int32 count)
- {
- Int32 error = 0, written = 0;
- IntPtr intptrPrinter = new IntPtr(0);
- DOCINFOA di = new DOCINFOA();
- bool bSuccess = false;
- di.pDocName = "Test Document";
- di.pDataType = "RAW";
- if (OpenPrinter(printerName.Normalize(), out intptrPrinter, IntPtr.Zero))
- {
- if (StartDocPrinter(intptrPrinter, 1, di))
- {
- if (StartPagePrinter(intptrPrinter))
- {
- bSuccess = WritePrinter(intptrPrinter, intptrBytes, count, out written);
- EndPagePrinter(intptrPrinter);
- }
- EndDocPrinter(intptrPrinter);
- }
- ClosePrinter(intptrPrinter);
- }
- if (bSuccess == false)
- {
- error = Marshal.GetLastWin32Error();
- }
- return bSuccess;
- }
- Socket _socket;
- Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox1.Checked)
- {
- _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.58"), 9178);
- try
- {
- _socket.Bind(endpoint);
- }
- catch (Exception ee)
- {
- MessageBox.Show("连接失败");
- }
- _socket.Listen(10);
- while (true)
- {
- Socket sockClient = _socket.Accept();
- while (true)
- {
- byte[] Receivedata = new byte[1024];
- int length = -1;
- try
- {
- length = sockClient.Receive(Receivedata);
- }
- catch
- {
- break;
- }
- if (length == 0)
- {
- sockClient.Close();
- break;
- }
- else
- {
- string data = BitConverter.ToString(Receivedata);
- data = data.Replace("-", "");
- int Data_head = data.IndexOf("52454D4F5445315449");
- int End_data = data.IndexOf("4A450100001B000000");
- if (data.Contains("804420FB00144008020820820040411101"))
- {
- data = data.Replace("804420FB00144008020820820040411101", "5A4420FB001440080345A33C0040411108");
- }
- string printer_name = "Printer Name";
- byte[] outdata = Enumerable.Range(0, data.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(data.Substring(x, 2), 16))
- .ToArray();
- IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(outdata.Length);
- Marshal.Copy(outdata, 0, pUnmanagedBytes, outdata.Length);
- if (pUnmanagedBytes != 0)
- {
- SendBytesToPrinter(printer_name, pUnmanagedBytes, outdata.Length);
- }
- }
- }
- }
- }
- else
- {
- _socket.Close();
- }
- }
- }
- }
复制代码
|
|