Public Partial classReadcard:form {//function of operating IC card, encapsulated in IC class[StructLayout (layoutkind.sequential)] Public unsafe classIC {//Initializing the device[DllImport ("Mwic_32.dll", EntryPoint ="Auto_init", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static extern intAuto_init (intPortintbaud); //Device Password Format[DllImport ("Mwic_32.dll", EntryPoint ="SETSC_MD", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static extern intSETSC_MD (intIcdev,intmode); //get device Current status[DllImport ("Mwic_32.dll", EntryPoint ="Get_status", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static externInt16 Get_status (intIcdev, int16*State ); //turn off the device communication interface[DllImport ("Mwic_32.dll", EntryPoint ="Ic_exit", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static extern intIc_exit (intIcdev); //make the device beep[DllImport ("Mwic_32.dll", EntryPoint ="Dv_beep", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static extern intDv_beep (intIcdev,intTime ); //write data to the IC card[DllImport ("Mwic_32.dll", EntryPoint ="swr_4442", SetLastError =true, CharSet = charset.ansi, exactspelling =true, CallingConvention =Callingconvention.stdcall)] Public Static extern intswr_4442 (intIcdev,intOffsetintLenChar*w_string); //Check Card password[DllImport ("Mwic_32.dll", EntryPoint ="csc_4442", SetLastError =true, CharSet = CharSet.Auto, exactspelling =true, CallingConvention =Callingconvention.winapi)] Public Static externInt16 csc_4442 (intIcdev,intLen, [MarshalAs (UnmanagedType.LPArray)]byte[] p_string); } PublicReadcard () {InitializeComponent (); } //The following is mainly to write the TextBox data to the IC card Private voidButton1_Click (Objectsender, EventArgs e) { //Initialize intIcdev = Ic.auto_init (0,9600); if(Icdev <0) MessageBox.Show ("Port initialization failed, please check that the interface cable is connected correctly. ","Error Hints", MessageBoxButtons.OK, messageboxicon.information); intmd = IC.SETSC_MD (Icdev,1);//Device Password Format unsafe{Int16 Status=0; Int16 result=0; Result= Ic.get_status (Icdev, &status); if(Result! =0) {MessageBox.Show ("Device Current status Error! "); intD1 = Ic.ic_exit (Icdev);//turn off the device return; } if(Status! =1) {MessageBox.Show ("Please insert the IC card"); intD2 = Ic.ic_exit (Icdev);//turn off the device return; } } unsafe { //the card's password defaults to 6 f (password: FFFFFF), 1 F for 16 is 15, 2 F for 16 is 255. byte[] pwd =New byte[3] {255,255,255 }; //byte[] pwd = new Byte[3] {0xff, 0xff, 0xff}; //char[] Pass=new ch{0xff,0xff,0xff};Int16 checkic_pwd = IC. csc_4442 (Icdev,3, PWD); if(Checkic_pwd <0) {MessageBox.Show ("IC card password Error! "); return; } Charstr ='a'; intWrite =-1; for(intj =0; J < TextBox1.Text.Length; J + +) {str= Convert.tochar (TextBox1.Text.Substring (J,1)); Write= ic.swr_4442 (Icdev, -+ J, TextBox1.Text.Length, &str); } if(Write = =0) { intBeep = Ic.dv_beep (Icdev, -);// BeepsMessageBox.Show ("The data has been successfully written to the IC card! "); } Else{MessageBox.Show ("data written to IC card failed! "); } } intD = ic.ic_exit (Icdev);//turn off the device//find the data based on the card numberOleDbConnection con=NewOleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source="+"Price.mdb"+"; Persist Security Info=false"); OleDbDataAdapter DAP=NewOleDbDataAdapter ("SELECT * from worker where icid= '"+ TextBox1.Text +"'", con); DataSet DS=NewDataSet (); Dap. Fill (DS,"Table"); if(ds. Tables.count >0) {TextBox2.Text= ds. tables[0]. rows[0][0]. ToString (); TextBox3.Text= ds. tables[0]. rows[0][1]. ToString (); Textbox4.text= ds. tables[0]. rows[0][2]. ToString (); Textbox5.text= ds. tables[0]. rows[0][3]. ToString (); } Else{MessageBox.Show ("the user does not exist! "); } } Private voidButton2_Click (Objectsender, EventArgs e) {application.exit (); } }}
Operating IC Card