473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I call API SCardTransmit(........) from C#?

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 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
0 3674

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

Similar topics

1
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...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
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...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
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...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
2
by: concoction | last post by:
What parameters i need to pass to send user data into the smart card using SCardTransmit function. what other functions are required to achieve writing user data?
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...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.