private void bInit_Click(object sender, EventArgs e) { 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(this.hContext, null, null, ref pcchReaders); if (retCode != ModWinsCard.SCARD_S_SUCCESS) { displayOut(1, retCode, ""); return; } EnableButtons(); byte[] ReadersList = new byte[pcchReaders]; // Fill reader list retCode = ModWinsCard.SCardListReaders(this.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; } }
public string GetBlockValue(int block, int hCard) { var value = @""; _returnCode = AuthenticateBlock(block, hCard); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } _recvBuffLen = _sentBuff[4] + 2; _sentBuff = new byte[255]; _recvBuff = new byte[255]; _sentBuff = APDUCommands.GetBlockValue(block); _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff.Length, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to fetch block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } for (int j = 0; j <= _recvBuffLen - 3; j++) { value += Convert.ToChar(_recvBuff[j]); } return(value); }
public void UpdateBlockValue(int block, string value, int hCard) { _returnCode = AuthenticateBlock(block, hCard); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } _recvBuffLen = 0x02; _sentBuff = new byte[255]; _recvBuff = new byte[255]; _sentBuff = APDUCommands.SetBlockValue(block, value); _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff[4] + 5, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS || _recvBuff[0].ToString(@"X2") != @"90") { throw new ApplicationException(@"Fail to update block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } }
private void Connect() { if (IsConnected) { _returnCode = ModWinsCard.SCardDisconnect(_hCard, ModWinsCard.SCARD_EJECT_CARD); IsConnected = false; } //_returnCode = APDUCommands.SetPICCParams(_piccParams); _returnCode = ModWinsCard.SCardConnect(_hContext, _name, ModWinsCard.SCARD_SHARE_SHARED, ModWinsCard.SCARD_PROTOCOL_T0 | ModWinsCard.SCARD_PROTOCOL_T1, ref _hCard, ref _protocol); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { _returnCode = ModWinsCard.SCardConnect(_hContext, _name, ModWinsCard.SCARD_SHARE_DIRECT, ModWinsCard.SCARD_PROTOCOL_UNDEFINED, ref _hCard, ref _protocol); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Failed to connect to reader " + _name, new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } } IsConnected = true; }