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?

PS 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?


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.

链接地址: http://www.djcxy.com/p/27966.html

上一篇: VS2015项目不会在调试模式下运行

下一篇: SCardEstablishContext未在发布模式下设置上下文指针