C # sends instructions to the same port device to get the status of the same port device

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.