我在C#中的方法,这是我从.DLLC#出的IntPtr相当于在Java中
[DllImport("somedll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int find([MarshalAs(UnmanagedType.AnsiBStr, SizeConst = 64)] string atr, out IntPtr int);
[DllImport("somedll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int getData(IntPtr int, int dataId, byte[] dataBuffer, ref int dataBufferSize);
调用这个方法C#调用如下
static IntPtr number = IntPtr.Zero;
static int res = 0;
try{
number = IntPtr.Zero;
res = find(null, out number);
if (number == IntPtr.Zero)
throw new ApplicationException("Something is wrong");
uint dataBufferSize = 1024;
res = getData(number, 1, null, ref dataBufferSize);
}
我没有发现什么在Java中是相同的。
如果我不喜欢这样写道:
public int find(String atr, Pointer int);
它说
java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:419)
at com.sun.jna.Function.invoke(Function.java:354)
at com.sun.jna.Library$Handler.invoke(Library.java:244)
如果我不喜欢这样写道:
public int find(String atr, IntByReference int);
什么也没有发生。
Java代码
IntByReference iref = new IntByReference();
res = find(null, iref);
Pointer pointer = iref.getPointer();
int dataBufferSize = 1024;
byte[] dataBuffer = new byte[dataBufferSize];
res = getData(Pointer.nativeValue(pointer), 1, dataBuffer, dataBufferSize);
find返回0,这意味着OK,但返回的getData 6,这意味着内存地址也不好。通过什么也没有发生我的意思是任何其他res比0.
所以,也许你应该放弃C#的标签,并添加JNI,JNA或标签? –
(我的意思是,你根本不需要添加任何标签,如果你愿意的话,甚至可以使用完全误导性的标签,但那些可能提供答案的人可能不会看到你的问题。) –
可能的重复[如何做相当于Java基元的引用传递](http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for -primitives-in-java) – kennytm