9 Feb 2008 Chicago   » (Journeyer)

.Net Interop

This post should really be titled "Please write 1000 times, Vista is Not XP". Yesterday, I had the pleasure of having to look at how to write .Net wrappers around a Windows dll - to be precise, the Smart Card interface - WinSCard.dll. We've previously written code in C, but really the time is to move on and use C#. The problem's been addressed before, and it dosn't take a genius to find Google has references to Smart Cards in .NET. In particular, this is the second part of a three part essay. The first part discusses using a COM object to wrap around the dll functions, but this is the simpler way of doing it.

Apart from the required usinglines, the code to do this, is relativly simple:

public class MyClass{
  [DllImport("WinScard.dll")]
  public static extern int SCardEstablishContext(
    uint dwScope,
    int nNotUsed1,
    int nNotUsed2,
    ref int phContext);
}

This is a fully functioning class which provides the interface to one of the functions in the DLL. Now, whats the problem?

Well, in this instance, its a security violation. Some of the dll functions can be called and run without any problems at all, but some of them cause AccessViolationException. The helpful hint that comes with it suggests that this is because memory is corrupt (and after lots of uhming and ahhing we decide its not). No, the problem is because the memory is protected. In Vista that is. The same code running on XP provides no problems whatsoever.

Now heres my problem - my Google skills, usually pretty nifty, are failing me - I am struggling to search the great intertubes for a solution I am still scratching my head. The combinations of keywords I'm using still only come back with results are either:

1) Useless 2) People suffering the problem who havn't solved it. 3) Results about something similar in XP which has the same exception which is fixed by a MS .Net hotfix (which is irrelevant). 4) People who have a solution along the lines of "run your code as root".

So I need to spend more time looking at that soon.

Latest blog entries     Older blog entries

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!