Sunday, 23 July 2017

c# - SCardEstablishContext not setting context pointer in release mode



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?


Answer



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.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...