111,126
社区成员
发帖
与我相关
我的任务
分享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);
}
#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