473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I call API SCardTransmit(........) from C#? I have worked on it for over two weeks.

SCardTransmit(.....) is an API, you can find it from MSDN, from
"Winscard.dll" that used to send
commands, in bytes, to smart card and get response, also in bytes, from
that smart card, and the following is the way I used to call that API
from C#.

protected byte[] Execute()
{
//Handle the smart card that I have already got.
UIntPtr cardHandle = got card handle ;

//Buffer used to contain the command sent to smart card
//this command is used to change pin1 of sim card.
byte[] sendBuffer = build buffer needed ;

//Length of the buffer above.
int sendBufLen = sendBuffer.Length ;

//Buffer used to store the response info from the
//smart card. In my example, it is always the same,
//and I don't know the reason why.
byte[] recvBuffer = new byte[2] ;

//Length of the response buffer.
int recvBufLen = 2 ;

//The PCI info sent to the smart card,
//I get the address of this PCI from "Winscard.dll",
//and method "GetPciT0()" is defined bellow.
IntPtr sendPci = this.GetPciT0() ;

//The PCI info return back from smart card.
//and the type "Pci" is a struct I defined as bellow,
//and it is the same as its counterpart defined in C
//from MSDN.
//[StructLayout(LayoutKind.Sequential)]
//public struct Pci
//{
// public Pci(int protocol, int length)
// {
// this.protocol = protocol ;
// this.pciLength = length ;
// }
// public int protocol ;
// public int pciLength ;
//}
Pci recvPci = new Pci(0, 0) ;

//Send the command to smart card.
int ret = SCardTransmit(cardHandle, sendPci, sendBufLen,
sendBufLen, recvPci, recvBuffer,
recvBufLen
)
return recvBuffer ;
}

//Get the address of Pci from "Winscard.dll".
private IntPtr GetPciT0()
{
IntPtr handle = LoadLibrary("Winscard.dll") ;
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
FreeLibrary(handle) ;
return pci ;
}

[DllImport("Winscard.dll")]
private extern static int SCardTransmit
(UIntPtr cardHandle, IntPtr sendPci,
byte[] sendBuffer, int sbLength,
[In, Out] Pci recvPci,
[Out] IntPtr recvBuffer,
[In, Out] int rbLength
) ;

[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName) ;

[DllImport("kernel32.dll")]
private extern static void FreeLibrary(IntPtr handle) ;

[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr handle, string
procName) ;

The API SCardTransmit(....) can be called successfully, because the
return value from that API is "0", and also the pin1 value in smart card
is changed, but the problem is that the value of "recvBuffer" is always
the same, "0200" in hexstyle, and the hope value is "9000" if the pin1
is changed and "9804" if do not have enough rights to change that pin1.
I don't know where the problem is and how I can handle it.
there may be some problem with the transition of "recvBuffer" from
managed code to unmanaged code, I guess.

The card reader I used is "SCM SCR331 0", for other infomation about
SCardTransmit(.....), please refer to MSDN.

Hope your response, best regards.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
1 9608
Hariri
1
hello Aaron,

thanks from your code sample,
i'm creating a win app with c#.net 2.0 and must read magnetic cards, :cool:
if you know about this technic?
are you have a good sample for this way?? :)

thanks
hossein
Jul 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Patrick | last post by:
I am almost certain that I could use HTTP Post/Get to submit XML Web Service call (over SSL as well, if using Version 3 of MSXML2) from an ASP Application? However, would I only be able to call...
0
by: Aaron | last post by:
SCardTransmit(.....) is an API from "Winscard.dll" that used to send commands, in bytes, to smart card and get response, also in bytes, from that smart card, and the following is the way I used to...
3
by: Aaron | last post by:
SCardTransmit(.....) is an API, you can find it from MSDN, from "Winscard.dll" that used to send commands, in bytes, to smart card and get response, also in bytes, from that smart card, and the...
8
by: Schwarz | last post by:
Hi guyz I'm stuck up in this smart card SCardTransmit function. I still don't get how this function works . I'm currently trying to write some data to the smart card. Can anyone help me with some...
2
by: Peter Kirk | last post by:
Hi there in c# what is a good way of selecting which method to call based on a string? For example, my method can accept a string parameter (there are 50 or so valid strings that can be...
1
by: 'Mani | last post by:
Hi All, I have an application written in C++ which uses GSoap to make webservice requests. The application works fine in Windows 2003 and Windows XP. But when we tried to run the application...
0
by: Ramazzotti... | last post by:
Hi All i am using Asp.net2.0, C#, VS2005 and i am using winscrsd.dll, ScardTransmit. i have this error no. = 2148532228 in return, so please i need your help. thank you in advance. this is the...
6
by: semedao | last post by:
Hi, I have problem to determine that I don't have any internet connection when I have Lan Nic available that is connected to other computer and not to the internet. I am using the...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.