C# 调用系统API函数直接连接pos打印机 打印
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// PrintPos 的摘要说明
/// </summary>
public class PrintPos
{
private FileStream fs = null;
[DllImport("kernel32.dll")]//调用系统API打印函数
public static extern IntPtr CreateFile
(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);
public PrintPos()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 开始打印,本地打印机ltp1 端口打印 调用方法:PrintPos.PrintPage("dsdfdsfdsfsdfdsfsdfdsfs");
/// </summary>
/// <param name="strPos"></param>
/// <returns></returns>
public string PrintPage(string strPos)
{
IntPtr iHandle = CreateFile("LPT1", 0x40000000, 0, 0, 3, 0, 0);
//判断是否连接上打印机 -1为false
if (iHandle.ToInt32() == -1)
{
return "没有连接到打印机";
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
//StreamReader sr = new StreamReader(fs);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.WriteLine(strPos, 0, 500);
sw.Close();
fs.Close();
return "已经成功连接打印机";
}
}
}
*******************************************************************************************************
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsApplication2
{
/// <summary>
///POSPrinter的摘要说明。
/// </summary>
public class POSPrinter
{
const int OPEN_EXISTING=3;
string prnPort="COM1";
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
public POSPrinter()
{
//
//TODO:在此处添加构造函数逻辑
//
}
public POSPrinter(string prnPort)
{
this.prnPort=prnPort;//打印机端口
}
public string PrintLine(string str)
{
IntPtr iHandle=CreateFile(prnPort,0x40000000,0,0,OPEN_EXISTING,0,0);
if(iHandle.ToInt32()==-1)
{
return"没有连接打印机";
}
else
{
FileStream fs=new FileStream(iHandle,FileAccess.ReadWrite);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.UTF8);//写数据
sw.WriteLine(str);
//开钱箱
//sw.WriteLine(Chr(&H1B)&Chr(70)&Chr(0)&Chr(20)&Chr(&HA0))
sw.Close();
fs.Close();
return"";
}
}
}
}
*********************************************************************************************************************
public class LPTControl
{
[StructLayout(LayoutKind.Sequential)]
Private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
private int iHandle;
public bool Open()
{
iHandle=CreateFile("lpt1",0x40000000,0,0,3,0,0);
if(iHandle !=-1)
{
return true;
}
else
{
writeline函数 return false;
}
}
public bool Write(String Mystring)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论