Using system;using system.diagnostics;using system.runtime.interopservices;using system.text;using System.Windows.Forms; namespace printermonitor{public partial class Form1:form {const UINT GENERIC_READ = 0x80000000; const UINT GENERIC_WRITE = 0x40000000; const UINT FILE_ATTRIBUTE_NORMAL = 0x80; #region Win32 API [DllImport ("Kernel32.dll")] private static extern int CreateFile (string lpfil ename, uint dwdesiredaccess, intd wsharemode, int lpsecurityattributes, int DW Creationdisposition, uint dwflagsandattributes, int htemplatefile); [DllImport ("Kernel32.dll")] private static extern bool WriteFile (int hfile, byte[] lpbuffer , int nnumberofbytestowrite, ref int lpnumberofbyteswritten, int lpoverlapped) ; [DllImport ("Kernel32.dll")] private static extern BOOL DefineDosDevice (int dwFlags, string lpdevicename, string lptargetpath); [DllImport ("Kernel32.dll")] private static extern bool CloseHandle (int hobject); [DllImport ("Kernel32.dll")] private static extern bool ReadFile (int hfile, byte[] lpbuffer, int nnumberofbytestoread, ref int lpnumberofbytesread, int lpoverlapped); #endregion public Form1 () {InitializeComponent (); } private void Button1_Click (object sender, Eventargse) {intihandle =-1; try {int i = 0; Create Instance DefineDosDevice (0x00000001, "Lptportname", @ "\device\parallel0"); Ihandle = CreateFile (@ "\\.\lptportname", Generic_read | Generic_write, 0, 0, 3, file_attribute_normal, 0); if (Ihandle!=-1) {byte[] MyByte = new byte[3]{0x12, 0x14, 0x14};//the command to be sent (16 binary) WriteFile (Ihandle, MyByte, MyByte. Length, refi, 0); byte[] mybyte1 = new Byte[3]; String Content= ""; int j = 0; ReadFile (Ihandle, mybyte1, 3, ref j, 0); if (mybyte1! = null) {foreach (var tempbyte in mybyte1) { Content + = tempbyte.tostring (); }} MessageBox.Show (content);//Gets the status value} else {MessageBox.Show ("Failed to create file!"); }} catch (Exception ex) {MessageBox.Show (ex). Message); } finally {if (Ihandle > 0) {closehandle (Ihandle) ; } } } }}
C # sends instructions to the same port device to get the status of the same port device