SCardListReaders method is returning empty card reader name

481 views
Skip to first unread message

kiran iyengar

unread,
Jun 14, 2013, 5:37:29 AM6/14/13
to jna-...@googlegroups.com

public interface WinScard extends StdCallLibrary
{
    WinScard    INSTANCE    = (WinScard) Native.loadLibrary("winscard", WinScard.class, W32APIOptions.UNICODE_OPTIONS);

    public int SCardEstablishContext(int dwScope, Integer pvReserved1, Integer pvReserved2, ByReference phContext);

//LONG WINAPI SCardListReaders(
//  _In_      SCARDCONTEXT hContext,
//  _In_opt_  LPCTSTR mszGroups,
//  _Out_     LPTSTR mszReaders,
//  _Inout_   LPDWORD pcchReaders
//);

    public int SCardListReadersA(Long hContext, String mszGroups, Buffer mszReaders, IntBuffer pcchReaders);
}

public class ReadCard
{

    public static void main(String[] args)
    {
        WinScard win = WinScard.INSTANCE;

        LongByReference phContext = new LongByReference();

        int result = win.SCardEstablishContext(2, 0, 0, phContext);

        if (result == 0)
        {
            IntBuffer intBuf = IntBuffer.allocate(100);
            CharBuffer buf = CharBuffer.allocate(100);

            result = win.SCardListReadersA(phContext.getValue(), null, buf, intBuf);

            if (result == 0)
            {
                System.out.println(new String(buf.array())); //This line is printing some special character.
            }
        }

    }
}

I am getting single special character when I print on the console, I have tried char array, byte arrays for mszReaders but all are returning the same output.
I have sample vb code it is getting the correct reader name. 
Unable to identify the problem.
Thanks in advance

Timothy Wall

unread,
Jun 14, 2013, 7:29:02 AM6/14/13
to jna-...@googlegroups.com
You *probably* don't want "Long" or "LongByReference". These use 64-bit values, which are *usually* only applicable on a 64-bit platform. If the values are opaque pointers (void *) or (void **), then use Pointer and PointerByReference instead.

As for strings, if you're using the ansi version (-A), then your buffers need to be byte buffers. When you use unicode (-W), use Java char buffers.

And in both cases, use Native.toString() to convert the byte/char buffer into a Java String.

Finally, use IntByReference to provide the callee with the address of an int into which to write the "returned" count value. Make sure you check its value and the function return value, since these w32 APIs often behave differently if you didn't provide enough space in your buffer (and in this case, you didn't initialize the count field, so you probably told the function your buffer is of size zero, in which case it'll write the required buffer size into the count field and leave your buffer untouched).
> --
> You received this message because you are subscribed to the Google Groups "Java Native Access" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Reply all
Reply to author
Forward
0 new messages