private void bInit_Click() { string ReaderList = "" + Convert.ToChar(0); int indx; int pcchReaders = 0; string rName = ""; //Establish Context retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retCode, ""); return; } // 2. List PC/SC card readers installed in the system retCode = ModWinsCard.SCardListReaders(hContext, null, null, ref pcchReaders); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retCode, ""); return; } byte[] ReadersList = new byte[pcchReaders]; // Fill reader list retCode = ModWinsCard.SCardListReaders(hContext, null, ReadersList, ref pcchReaders); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { mMsg.AppendText("SCardListReaders Error: " + ModWinsCard.GetScardErrMsg(retCode)); return; } else { displayOut(0, 0, " "); } rName = ""; indx = 0; //Convert reader buffer to string while (ReadersList[indx] != 0) { while (ReadersList[indx] != 0) { rName = rName + (char)ReadersList[indx]; indx = indx + 1; } //Add reader name to list cbReader.Items.Add(rName); rName = ""; indx = indx + 1; } if (cbReader.Items.Count > 0) { cbReader.SelectedIndex = 0; } }
private void btnInit_Click(object sender, EventArgs e) { int pcchReaders = 0; retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { WriteLog(1, retCode, ""); return; } retCode = ModWinsCard.SCardListReaders(this.hContext, null, null, ref pcchReaders); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { WriteLog(1, retCode, ""); return; } byte[] ReadersList = new byte[pcchReaders]; //Fill reader list retCode = ModWinsCard.SCardListReaders(this.hContext, null, ReadersList, ref pcchReaders); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { txtMessage.AppendText("SCardListReaders Error: " + ModWinsCard.GetScardErrMsg(retCode)); return; } else { WriteLog(0, 0, " "); } //Convert reader buffer to string int idx = 0; string rName = ""; while (ReadersList[idx] != 0) { while (ReadersList[idx] != 0) { rName = rName + (char)ReadersList[idx]; idx = idx + 1; } //Add reader name to list ddlReaderList.Items.Add(rName); rName = ""; idx = idx + 1; } if (ddlReaderList.Items.Count > 0) { ddlReaderList.SelectedIndex = 0; } }
private static void InitialiseSmartCardReader() { Array.Clear(SendBuff, 0, 262); Array.Clear(RecvBuff, 0, 262); //Establishes the context retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { throw new Exception(retCode.ToString()); } //Verify how many bytes does the reader name has retCode = ModWinsCard.SCardListReaders(hContext, null, null, ref Readercount); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { throw new Exception(retCode.ToString()); } //Creates the byte array to receive the name ReaderListBuff = new byte[Readercount]; //Gets the readers name retCode = ModWinsCard.SCardListReaders(hContext, ReaderGroupBuff, ReaderListBuff, ref Readercount); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { throw new Exception(retCode.ToString()); } //Decodify the readers name sCard = System.Text.Encoding.ASCII.GetString(ReaderListBuff); //Connects to the reader retCode = ModWinsCard.SCardConnect(hContext, sCard, ModWinsCard.SCARD_SHARE_SHARED, ModWinsCard.SCARD_PROTOCOL_T0, ref hCard, ref Protocol); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { throw new Exception(retCode.ToString()); } }
private void btnReaderPort_Click(object sender, System.EventArgs e) { byte[] returnData = null; // Will hold the reader names after the call to SCardListReaders int readerCount = 0; // Total length of the reader names string readerNames = ""; // Will hold the reader names after converting from byte array to a single string. string[] readerList = null; // String array of the Reader Names int idx = 0; // Established using ScardEstablishedContext() retcode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext); if (retcode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retcode, ""); return; } // List PC/SC card readers installed in the system // Call SCardListReaders to get the total length of the reader names retcode = ModWinsCard.SCardListReaders(this.hContext, null, null, ref readerCount); if (retcode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retcode, ""); return; } else { returnData = new byte[readerCount]; // Call SCardListReaders this time passing the array to hold the return data. retcode = ModWinsCard.SCardListReaders(this.hContext, null, returnData, ref readerCount); if (retcode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retcode, ""); return; } else { displayOut(0, 0, "SCardListReaders...OK"); // Convert the return data to a string readerNames = System.Text.ASCIIEncoding.ASCII.GetString(returnData); // Parse the string and split the reader names. Delimited by \0. readerList = readerNames.Split('\0'); // Clear the combo list of all items. cbReader.Items.Clear(); // For each string in the array, add them to the combo list for (idx = 0; idx < readerList.Length; idx++) { cbReader.Items.Add(readerList[idx]); } cbReader.SelectedIndex = 0; } } btnConnect.Enabled = true; btnReset.Enabled = true; }