C#
  Home arrow C# arrow Page 2 - Smart Cards in .NET, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Smart Cards in .NET, Part 2
By: Digvijay Chauhan
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 33
    2004-11-08

    Table of Contents:
  • Smart Cards in .NET, Part 2
  • The Basics
  • Two Versions
  • The Managed Smart Card Sample
  • Our Interface
  • Points of Interest

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Smart Cards in .NET, Part 2 - The Basics


    (Page 2 of 6 )

    DLLs in the native world contain just the code and no metadata, except for the related exports in one of the tables in the PE Image. But how would the .NET runtime be able to get all the information about the function in a DLL, or the data or inputs .NET expects us to pass to it? Moreover, .NET will need to know whether data is to be returned from the called native function and what kind of data it is. Anyone programming in .NET would be aware of the differences in types in the Managed and Unmanaged world.

    The Platform Invoke or the P/Invoke uses the metadata that we provide in our managed code to locate exported functions and marshal their arguments at run time. The following illustration shows this process.

    Smart Cards in .NET Part 2

    Figure 1 The process of P/Invoke

    When one invokes an unmanaged function from managed code the following sequence of actions occurs:

    First the Common Language Runtime

    • locates the DLL containing the function,

    • loads the DLL into memory,

    • locates the address of the function in memory and pushes its arguments onto the stack, marshaling data as required, using the standard marshaller or as we specify (it’s worth noting that locating and loading the DLL, and locating the address of the function in memory occur only on the first call to the function)

    • transfers control to the unmanaged function.

    Also P/Invoke throws exceptions generated by the unmanaged function to the managed caller. In order to locate a function, one must specify at least the name of the function and name of the DLL that contains it. Each exported function will have a start address in the DLL and is called as the Entrypoint. (Note: It’s not that DLLMain!) To invoke a function you should tell the runtime the DLL name and the function name (or the ordinal number).To do so you use the DllImport attribute with the function prototype. To use this attribute you need to include the following statements to your code. This enables us to use the DllImport attribute available to us for use in our code.

    [Visual Basic]
    Imports System.Runtime.InteropServices

    [C#]
    using System.Runtime.InteropServices

    Now comes the syntax that we may use for declaring such an unmanaged imported function as below. Consider the MesssageBox function that the user32.dll exports for our use. To introduce the MessageBox function in the User32.dll we declare a function (in C#) as

    [DllImport("user32.dll")]
    public static extern int MessageBox(int hWnd, String text, String caption, uint type);

    More C# Articles
    More By Digvijay Chauhan


       · Hi I'm Digvijay . the author of this article.Please feel free to ask any questions...
       · Dig, I really enjoyed the article. Please keep up the great work!:)
       · awesome! u rok!
       · Hi. Your code won't work if you have more readers connected to one pc. I have...
       · hi,your article was excellent,but u did not provide any code for accessing data...
       · hi i read your article, its very nice.. but there are no code to connect and read...
       · Hi Thiru,Sorry but right now i am away from work in Sweden doing my Masters in...
       · Thanks :-)
       · Hi,Thanks for the fix ... it'll help several people.Regards,DIgvijay
       · hi thanks alot for this lesson.i have some questions please : **** i am...
       · hye.. i already read your article.. it great. its give me an overviw abiut smart...
       · to which file should i paste the following code// WinSCard API's to be...
       · you do the attendance system using smart card ?? so have yuo sucessfully done it??...
       · hi digvijay, sir i want to know a few details about smart cards...i...
       · Hi,Yes i did this about two years ago but right now i am quite busy with my...
       · Hi DigVijay, I read your article, it nice, but i am new to MFC, I have to...
       · This is a nice clarifying tutorial.Very useful to me.Thanks again
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek