

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
2 messages in net.java.dev.jna.usersRe: [jna-users] Call by reference| From | Sent On | Attachments |
|---|---|---|
| Magnus Zmuda | Sep 28, 2007 1:13 am | |
| Timothy Wall | Sep 28, 2007 5:28 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Call by reference | Actions... |
|---|---|---|
| From: | Timothy Wall (twal...@dev.java.net) | |
| Date: | Sep 28, 2007 5:28:39 am | |
| List: | net.java.dev.jna.users | |
Strings in Java are immutable. The correspond to "const char*" in C.
If you have a buffer you want the DLL to modify, use byte[] or java.nio.Buffer. If the routine is actually allocating a buffer, you'll need a PointerByReference or Pointer[].
On Sep 28, 2007, at 4:14 AM, Magnus Zmuda wrote:
Hi, I've got a dll (device.dll) file that I want to use in my java application. I can't change the dll file I only have a api for it.
Methods in the dll: int open(int typ, WINCALLBACKBCOM cbf, void* param=NULL) int close() int getInfo(int typ, int* len, char *info)
The method getInfo should return the length of buffers in the len parameter and the pointer of buffers should be in info. I use IntByReference for len and a String for info, but when I print them after the method getInfo the data is still the same. Why haven't they been updated? Am I doing something wrong here? Hope someone can help me and that I'm posting in the right mailing list.
My java classes:
/--------CLibrary.java------------\ import com.sun.jna.*; public interface CLibrary extends Library {
interface WINCALLBACKBCOM extends Callback { public int callback(int nStatus, int nTracks); }
}
/--------Device.java--------------\ import com.sun.jna.*; import com.sun.jna.ptr.* ;
public interface Device extends Library {
int open(int typ, CLibrary.WINCALLBACKBCOM cb, Pointer param); int closeReadDevice(); int getInfo(int typ, IntByReference len, String info);
Device instance = (Device) Native.loadLibrary("Device", Device.class); }
/--------test.java------------------\ import com.sun.jna.*; import com.sun.jna.ptr.*;
public class test { public test() { }
public static void main(String[] args) { try { Device lib = Device.instance;
if (lib != null) { System.out.println("loaded);
System.out.println("open"); String[] s = {""}; Pointer p = new StringArray(s); int e = lib.open(0, new CLibrary.WINCALLBACKBCOM() { public int callback(int nStatus, int nTracks) { System.out.println("callback: nStatus = " + nStatus + " nTracks = " + nTracks); return 0; } }, p); System.out.println("open error: " + Integer.toHexString(e));
Thread.sleep(2500);
System.out.println("getInfo"); String s3 = ""; IntByReference iref = new IntByReference(1); int e2 = lib.getInfo(0, iref, s3); System.out.println("getInfo error: " + Integer.toHexString (e2)); System.out.println ("S3 = " + s3); System.out.println("Iref = " + iref.getValue()); } else { System.out.println("Load failed"); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } }
/--------Result---------------------\ loaded open open error: 0 getInfo getInfo error: 0 S3 = Iref = 1







