Inteface with Smart Card reader

Hi

i m working on small project . In my project i have to interface with smart card .I m doing my project in C# 2005 . But i dont know how to program with Smart Card reader.how to read and write to Smart Card. Help me

[231 byte] By [ShibuBhattarai] at [2007-12-22]
# 1
What kind of smart card reader is it? What kind of interface does it use, USB, Serial, PCI, etc?
PeterRitchie at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

i need to interface smart card with serial port

ShibuBhattarai at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
Contact the manufacturer of the reader and ask them for the protocol specification. Then use the .NET 2.0 SerialPort class to read the data.
nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4

I too am doing similar work utilizing smart card reading integration into a .Net C# application. My particular scenario involves a Gemplus USB reader. I've been using the Interop assembly to work via the winscard.dll and have successfully called SCardEstablichContext, SCardListReaderGroups, SCardListReaders, and SCardReleaseContext. However,

I'm stuck at SCardConnect. It appears that an error is thrown to which there is not corresponding translation. So, I have no idea what I'm dealing with.

Is there any documentation, ANYWHERE, that uses C# as the primary language instead of the native code? At least that way, I could verify that my dll imports are coded correctly for the native function to work properly.

As prevalent as smart cards are becoming, I'm surprised that there aren't better documentation resources readily available.

Thanks for any and all responses.

BW

bwhunan at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5
Lot's of hits on this query!
nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6
Lots of hits, yes. But a lot of hits with the same information on MSDN is not a help. I found 4 pages of hits with people citing C++ function calls with the translated C# code. And the hits that do provide code, most of the examples don't get past SCardListReader. I have all that....an explanation as to how to use functions and how many times to use the same function, with and without certain parameters would be helpful.

Thanks for the response though; I do appreciate your taking the time.

R/BW
bwhunan at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7
Can you shows us your P/Invoke declaration for SCardConnect and the code that calls it? What value does it return?

nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8
The P/Invoke:

[DllImport("winscard.dll", EntryPoint = "SCardConnectA", CharSet = CharSet.Auto)]
public static extern int SCardConnect(int hContext,string szReader,UInt32 dwShareMode,UInt32 dwPreferredProtocols,
out UInt32 phCard,
out UInt32 pdwActiveProtocol);

The code that calls (which is where I feel I need more for the function to work...)

string szReader = globalreader; //Gemplus USB Smart Card Reader 0

//SCARD_SHARE_EXCLUSIVE = 0;

UInt32 Mphcard = 0;
UInt32 MpdwActiveProtocol = 0;
UInt32 SCARD_PROTOCOL = 0x80000000; //default PTS - I've also tried 0
//UInt32 SCARD_PROTOCOL = 2;

int ret4 = NativeMethods.SCardConnect(context, szReader, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL, out Mphcard, out MpdwActiveProtocol);

if (ret4 == SCARD_S_SUCCESS)

{
lblResults.Text +=
"\n" + ret4 + " - SCardConnect call is a success";
}

if (ret4 != SCARD_S_SUCCESS)

{

//string rethex = String.Format("{0:x2}", ret4);

lblResults.Text += "\n" + ret4 + " - SCardConnect call is not a success \n";

Error(ret4, "SCardConnect() is throwing an error");

}

The error I receive is -2146435063; which I believe equates to 0x80100009 (Unknown Reader). Not sure why I keep getting this error since the reader is returned from the SCardListReaders call. The out variable never get filled. I get the return value of 0 for the SCardEstablishContext, SCardListGroups, and SCardListReaders, and SCardReleaseContext.

Thanks,
R/BW

bwhunan at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 9
First thing to change: "SCardConnectA" to "SCardConnectW". If you still get an error code, let us know what it is.

nobugz at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 10

I have the same issues. after i change it to SCardConnectW. it's still giving me the error code

thanks.

lxl6428 at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 11

I am using EMVCO USB SmartCard reader and I want to know how to start connecting and reading data?

any resources or samples please?

JassimRahma at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 12

Hi!

I'm working on a small project that will read and write in a Smart Card but i don't know how to do it. i'm doing my project in Visual Basic. Need some help.

Thanks..

ladykai at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 13

http://pinvoke.net/default.aspx/winscard.ASCIIEncoding

http://www.codeproject.com/smartclient/SmartcardFmwk.asp

Try Changing the Charset.Auto to Unicode or ascii (just try it solved an issue for me once)

JelleB. at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 14

Try these parameters in scardconnect:

dwShareMode = SCARD_SHARE_SHARED

dwPreferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1

waiwaipang at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified