我能够使用以下代码将字符串写入LPT1:
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
Byte[] buffer = new byte[input.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(input);
SafeFileHandle fh = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
if (!fh.IsInvalid)
{
FileStream lpt1 = new FileStream(fh, FileAccess.Write);
lpt1.Write(buffer, 0, buffer.Length);
lpt1.Close();
}
由于端口是虚拟的,我可以检查字符串是否写在附件中。问题是:如何在不访问附件的情况下从LPT1读取字符串?我尝试使用相同的方法,但将方法更改为lpt1.Read(buffer, 0, buffer.Length)
并更改为FileAccess.Read
模式。它抛出了一个异常Incorrect function
。