一厂MES,含注塑,喷涂,冲孔
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
8.3 KiB

3 months ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
namespace InjectionPC
{
public class PrinterHelper
{
#region WIN32 API - Print Module
[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);
#endregion
#region public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) - 바이트 데이터를 출력 합니다.
/// <summary>
///
/// </summary>
/// <param name="szPrinterName"></param>
/// <param name="pBytes"></param>
/// <param name="dwCount"></param>
/// <returns></returns>
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
#endregion
#region public static bool SendFileToPrinter(string szPrinterName, string szFileName) - 파일의 raw 데이터를 출력 합니다.
/// <summary>
///
/// </summary>
/// <param name="szPrinterName"></param>
/// <param name="szFileName"></param>
/// <returns></returns>
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
#endregion
#region public static bool SendStringToPrinter(string szPrinterName, string szString) - 문자열을 print 합니다.
/// <summary>
///
/// </summary>
/// <param name="szPrinterName"></param>
/// <param name="szString"></param>
/// <returns></returns>
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
//Int32 dwCount;
byte[] pvBuffer = utf16_gbk(szString);
int bCount = pvBuffer.Length;
// allocate some unmanaged memory
pBytes = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(bCount);
// copy the byte[] into the memory pointer
System.Runtime.InteropServices.Marshal.Copy(pvBuffer, 0, pBytes, bCount);
// Send the converted byte[] to the printer.
SendBytesToPrinter(szPrinterName, pBytes, bCount);
// free the unmanaged memory
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pBytes);
return true;
}
public static bool tcpPrint(string str)
{
bool result = false;
byte[] cmdBytes = utf16_gbk(str);
TcpClient tcp = new TcpClient();
try
{
tcp.Connect("192.168.1.30", 9100);
tcp.SendTimeout = 1000;
tcp.ReceiveTimeout = 1000;
if (tcp.Connected)
{
tcp.Client.Send(cmdBytes);
result = true;
}
}
catch (Exception ex)
{
throw new Exception("打印失败,请检查打印机或网络设置。", ex);
}
finally
{
if (tcp != null)
{
if (tcp.Client != null)
{
tcp.Client.Close();
tcp.Client = null;
}
tcp.Close();
tcp = null;
}
}
return result;
}
public static byte[] utf16_gbk(string text)
{
//声明字符集
System.Text.Encoding utf16, gb2312;
//utf16
utf16 = System.Text.Encoding.GetEncoding("UTF-16");
//gb2312
gb2312 = System.Text.Encoding.GetEncoding("GBK");
byte[] lvbytes = utf16.GetBytes(text);
lvbytes = System.Text.Encoding.Convert(utf16, gb2312, lvbytes);
return lvbytes;
}
#endregion
}
}