2 messages in net.java.dev.jna.usersRe: [jna-dev] Problem reading a memor...
FromSent OnAttachments
Timothy WallMar 17, 2008 6:54 am 
Timothy WallMar 17, 2008 8:31 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-dev] Problem reading a memory cardActions...
From:Timothy Wall (twal@dev.java.net)
Date:Mar 17, 2008 6:54:31 am
List:net.java.dev.jna.users

On Mar 17, 2008, at 8:37 AM, LUCKY LUCKE wrote:

Hi guys, i need your help-advice once again.

Im trying to map the function SCardComand through the following code (i revised it after Albert's and Timothy's advice) :

public interface SCard32 extends StdCallLibrary {

StdCallFunctionMapper mapper = new StdCallFunctionMapper(); SCard32 INSTANCE = (SCard32) Native.loadLibrary("SCard32", SCard32.class);

public int SCardComand(NativeLong Handle, String Cmd, NativeLong CmdLen, String dataIn, NativeLong dataInLen, OutData dataOut, NativeLong dataoutlen); }

Where dataOut is an instance of a Memory's subclass

Use byte[] instead. It's more succinct and makes more clear your intention. However, both operations are equivalent.

If the first argument is a w32 HANDLE, use the W32API.HANDLE type and pass null. Again, this is mostly style.

Then i run the command

OutData out = new OutData(256); int response = lib.SCardComand(new NativeLong(0), "Device,InfoDeviceIDCard,1,Type", new NativeLong(0), null, new NativeLong(0), out, new NativeLong(256));

If I'm reading this correctly, you're passing a cmdLen of zero; is this supposed to be the length of the command string?

byte newByte[] = new byte[256];

out.read(0, newByte, 0, 256); // byte array[] = out.getByteArray(0, 6);

for (int j = 0; j < newByte.length; j++) { if (j != 0 && j % 16 == 0) { System.out.println(); } System.out.print(newByte[j] + " "); }

If you use a byte array, you can simply call Native.toString(byte[]) to convert to a Java String. It'll automatically stop reading when it encounters a NUL byte.

where i get a bunch of integers. What i want to ask is whether i do the right mapping for the LPSTR native type (the pointer to the byte array), or not. When i try the command "Device,List" which produces the output

'CHIPDRIVE micro' at COM2 'CHIPDRIVE exten I' at COM2-1 'CHIPDRIVE ................................ 'whatever the driver is....................

and it would need a two dimensional array to hold this data,

Not clear what you mean by needing a two dimensional array to hold this data.

i get a VM crash, that is the OutData mapping is not correct. I also get a VM crash when i try to write to the card, no matter if i pass the command as a java String or a byte array (im not trying to write outside of the memory's bounds. I checked this)

Presumably when you write to the card you have to set inData/inDataLen appropriately. You'll get a crash if you pass "null".