Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
scardconnect (winscard)
The SCardConnect function establishes a connection (using a specific resource manager context) between the calling application and a smart card contained by a specific reader. If no card exists in the specified reader, an error is returned.

C# Signature:

[DllImport("winscard.dll", EntryPoint="SCardConnect", CharSet=CharSet.Auto)]
static extern int SCardConnect(
     int hContext,
     [MarshalAs(UnmanagedType.LPTStr)] string szReader, //I was getting SCARD_E_UNKNOWN_READER until i removed [MarshalAs(UnmanagedType.LPTStr)]
     UInt32 dwShareMode,
     UInt32 dwPreferredProtocols,
     out int phCard,
     out UInt32 pdwActiveProtocol);

User-Defined Types:

Value of dwShareMode:

SCARD_SHARE_SHARED = 0x00000002 - This application will allow others to share the reader

SCARD_SHARE_EXCLUSIVE = 0x00000001 - This application will NOT allow others to share the reader

SCARD_SHARE_DIRECT = 0x00000003 - Direct control of the reader, even without a card

Value of dwPreferredProtocols

SCARD_PROTOCOL_T0 - Use the T=0 protocol (value = 0x00000001)

SCARD_PROTOCOL_T1 - Use the T=1 protocol (value = 0x00000002)

SCARD_PROTOCOL_RAW - Use with memory type cards (value = 0x00000004)



Tips & Tricks:

Please add some!

Sample Code:

DWORD dwActiveProtocol;
LONG rv;

rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
rv = SCardConnect(hContext, "Reader X", SCARD_SHARE_SHARED,
    SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);


error CS1502: The best overloaded method match for 'PCSC_ContactlessNet.SmartCard.SCardConnect(System.IntPtr, string, uint, uint, out int, out uint)' has some invalid arguments

error CS1503: Argument '5': cannot convert from 'System.IntPtr*' to 'out int'

error CS1503: Argument '6': cannot convert from 'int*' to 'out uint'


Alternative Managed API:



Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version