0

I am calling winscard.dll methods from C# and everything has been working fine in a debug mode. Problem i am getting is in release mode

My call to establish context is

[DllImport("winscard.dll")]
public static extern int SCardEstablishContext(int dwScope, int pvReserved1, int pvReserved2, ref int phContext);

In my test app when I call this the pntContext variable appears to get set properly in debug mode. But, in release mode its not getting set. But strangely enough the return code is still 0 (success).

So, I'm just wondering what are the circumstances that could cause this, or what other things could I be doing wrong?

P.S. Also one thing i check is compilation platform is set to Any CPU. I were trying with changing platform and it get worked when I have x86 as platform and release mode. Such a odd behavior that why it then worked in debug mode?

Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • What OS are you working on? I've used this api on win7 and win8 with anyCpu compilation and I haven't noticed the problem you are experiencing. Anyway I'm using this syntax Dim hContext As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(mContext)) mLastError = SCardEstablishContext(Scope.User, IntPtr.Zero, IntPtr.Zero, hContext) – AlexF Sep 01 '14 at 08:36

1 Answers1

4

I see nothing unusual there because your marshaling is completely wrong for 64bit Windows where pointers are 8 byte long. You should use IntPtr type which is 4 bytes long on 32bit Windows and 8 bytes long on 64bit Windows.

[DllImport("winscard.dll"]
public static extern Int32 SCardEstablishContext(
    [In] Int32 dwScope,
    [In] IntPtr pvReserved1,
    [In] IntPtr pvReserved2,
    [In, Out] ref IntPtr phContext);

Even better choice in your case would be to use proven managed winscard.dll wrapper like pcsc-sharp.

jariq
  • 11,681
  • 3
  • 33
  • 52