转载:C#Lpt端口打印类的操作浅析
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
转载:C#Lpt端⼝打印类的操作浅析
转载:
C#LPT端⼝打印类的操作是什么呢?⾸先让我们看看什么是LPT端⼝(打印机专⽤)?LPT端⼝是⼀种增强了的双向并⾏传输接⼝,在USB 接⼝出现以前是扫描仪,打印机最常⽤的接⼝。
最⾼传输速度为1.5Mbps,设备容易安装及使⽤,但是速度⽐较慢,下⾯是C#LPT端⼝打印类的操作具体实例:
/// <summary>
/// LPTControl 的摘要说明,C#LPT端⼝打印类的操作
/// </summary>
public class LPTControls
{
public LPTControls() { }
[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
);
//C#LPT端⼝打印类的操作
private int iHandle;
public bool Open()
{
iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(String Mystring)
{
if (iHandle != -1)
{
int i = 0;
OVERLAPPED x = new OVERLAPPED();
byte[] mybyte =
System.Text.Encoding.Default.GetBytes(Mystring);
return WriteFile(
iHandle, mybyte, mybyte.Length, ref i, ref x);
}
else
{
throw new Exception("端⼝未打开!");
}
}
public bool Close()
{
return CloseHandle(iHandle); }
}。