473,411 Members | 2,289 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,411 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 9613
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.