关于c#向打印机发送

foutcast 2009-03-04 03:30:20
c#直接向打印机发送指令,网上查了下,说就是createfile,然后write就可以了,但是我发送的指令他都会打出来,而不是执行。如何发送一个16进制的指令1B4BFF,请帖主要代码,谢谢。下面是我自己的代码:
iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
if (!iHandle.ToInt32().Equals(1))
{
int i;
OVERLAPPED x;
string str = "1B4BFF";
int len = str.Length / 2;
byte[] ab = new byte[len];
for (int ii = 0; ii < len; ii++)
{
string tmp = str.Substring(ii * 2, 2);
ab[ii] = Convert.ToByte(tmp, 16);
}
WriteFile(iHandle.ToInt32(), ab, ab.Length, out i, out x);

}
...全文
221 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
CraxyMouse 2009-03-04
  • 打赏
  • 举报
回复


#region //Some Value Of Print
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
#endregion

#region //API Createfile
[DllImport("kernel32.dll")]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
#endregion

#region //API CloseHandle
[DllImport("kernel32.dll")]
private static extern bool CloseHandle( int hObject);
#endregion

#region //CreateLPTFile
private void CreateLPTFile(string str)
{
try
{
string LPT = "LPT1";
int hPort = CreateFile(LPT,GENERIC_WRITE,0, 0,OPEN_EXISTING,0,0);
System.IntPtr hPortP = new IntPtr(hPort);
FileStream fs;
fs = new FileStream(hPortP, FileAccess.Write, false);
StreamWriter writer = new StreamWriter(fs);
writer.AutoFlush = false;
writer.WriteLine(str);
writer.Flush();
writer.Close();
fs.Close();
CloseHandle(hPort);
}
catch(IOException)
{
throw new MyCustomException("I/O Error, Please Check Printer Connection!");
}
}
#endregion
foutcast 2009-03-04
  • 打赏
  • 举报
回复
另外:连接lpt1或者com1发送指令的写法是不是一样的
indeep 2009-03-04
  • 打赏
  • 举报
回复
这个我也不懂啊 希望有人解答

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧