« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9704137 建立时间:2004年12月20日 |

| |
[jvm]不写jni调用系统函数  原创空间, 软件技术, 电脑与网络
邢红瑞 发表于 2007/8/13 18:09:48 |
最近写jni遇到很多麻烦,经常出现空指针,把jvm给crash掉了,生气的是sun的log文件木有任何价值,其实我只是调用操作系统API,Invoke operating system APIs and functions from native code DLLs/shared libraries directly from Java code without any JNI programmi。其实还是有很多库可以用的,xFunction,J2Native,JNIWrapper可惜都是收费的。jna可以做这件事情,只是发现他太麻烦了,难以使用。Jnative可以做到这点,先定义一个INT64的类,处理vc的PULARGE_INTEGER。import org.xvolks.jnative.JNative;import org.xvolks.jnative.Type;import org.xvolks.jnative.exceptions.NativeException;import org.xvolks.jnative.misc.FreeDiskSpace;import org.xvolks.jnative.misc.basicStructures.AbstractBasicData;import org.xvolks.jnative.misc.machine.Machine;import org.xvolks.jnative.pointers.Pointer;import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
/** * INT64 is a basic structure of 8 bytes that can be passed as a native pointer * */public class INT64 extends AbstractBasicData<Long> { public INT64(long value) { super(value); }
/** * Method getSizeOf * * @return the size of this data */ public int getSizeOf() { return sizeOf(); }
/** * Method createPointer * * @return a MemoryBlock * */ public Pointer createPointer() throws NativeException { pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(sizeOf())); pointer.setLongAt(0, mValue); return pointer; }
/** * Method getValueFromPointer * * @return a T * */ public Long getValueFromPointer() throws NativeException { mValue = pointer.getAsLong(0); return mValue; }
/** * Method sizeOf * * @return the size of this structure */ public static int sizeOf() { return Machine.SIZE * 8; }
}调用的函数String drive="c:";
INT64 lFreeBytesAvailable = new INT64(0); INT64 lTotalNumberOfBytes = new INT64(0); INT64 lTotalNumberOfFreeBytes = new INT64(0);
int i = 0; JNative fs = new JNative("Kernel32.dll", "GetDiskFreeSpaceExA"); fs.setRetVal(Type.INT); fs.setParameter(i++, Type.STRING, drive); fs.setParameter(i++, lFreeBytesAvailable.createPointer()); fs.setParameter(i++, lTotalNumberOfBytes.createPointer()); fs.setParameter(i++, lTotalNumberOfFreeBytes.createPointer()); fs.invoke(); System.out.println(lFreeBytesAvailable.getValueFromPointer());也真是麻烦,记得写c#的时候,调用api很简单的,[DllImport("User32.dll",EntryPoint="MessageBox")]static extern int msgbox(int hwnd,string msg,string caption,int msgtype);幸好java的JInvoke也提供类似的机制,只是不知道收不收费。 @NativeImport(library="User32") public static native int MessageBox(int hwnd, String text, String caption, int type);
public static void main(String[] args) { JInvoke.initialize(); MessageBox(0, "This MessageBox is a native Win32 MessageBox", "Caption", 0); }设置时间
@NativeImport(library="kernel32") static native void GetLocalTime(SYSTEMTIME pst); @NativeImport(library="kernel32") static native void SetLocalTime(SYSTEMTIME pst); JInvoke.initialize(); SYSTEMTIME systemtime = new SYSTEMTIME(); GetLocalTime(systemtime); System.out.println( "\tyear : " + systemtime.wYear + "\n\tmonth : " + systemtime.wMonth + "\n\tDayOfWeek : " + systemtime.wDayOfWeek + "\n\tDay : " + systemtime.wDay + "\n\tHour : " + systemtime.wHour+ "\n\tMinute : " + systemtime.wMinute+ "\n\tSecond : " + systemtime.wSecond+ "\n\tMillisecond : " + systemtime.wMilliseconds); systemtime.wYear=2009; SetLocalTime(systemtime);读取硬盘空间 @NativeImport(library="Kernel32") public static native boolean GetDiskFreeSpaceEx(String lpRootPathName, long[] lpSectorsPerCluster, long[] lpBytesPerSector, long[] lpNumberOfFreeClusters ); long[] bytesPerSector = { 0 }; long[] numberOfFreeClusters = new long[1]; // using the alternate single element array creation syntax long[] totalNumberOfClusters = new long[1]; GetDiskFreeSpaceEx("C:\\", bytesPerSector, numberOfFreeClusters, totalNumberOfClusters); System.out.println("bytesPerSector = "+bytesPerSector[0]); System.out.println("numberOfFreeClusters = "+numberOfFreeClusters[0]); System.out.println("totalNumberOfClusters = "+totalNumberOfClusters[0]); |
|
回复:不写jni调用系统函数 原创空间, 软件技术, 电脑与网络
(游客)发表评论于2007/11/22 10:07:16 |
getRetVal()只是调用是否成功的一个返回值。如:
我们有个HNBridge.dlldll中有getParam(Long pDataHandle,String paramName,REF String paramValue,UINT nMaxValuseLenth) 其中REF String paramValue
是一个返回值,getRetVal()只是校验该函数调用是否成功。
java如何调用REF String paramValue。
这是我的源码
public static final Object[] getparam(long s1,String s2,long s4 ) throws NativeException,IllegalAccessException{
JNative n=null;
try{
n= new JNative("HNBridge.dll", "GetParam");
n.setRetVal(Type.INT);
// n.setMemory(0, "");
//n.set
int i=0;
Pointer s6 = new Pointer(MemoryBlockFactory.createMemoryBlock(1024));
s6.zeroMemory();
s6.setStringAt(0, "ddd");
use=new UserCall("");
// s6.setStringAt(0, "sss");
//pTar=new Pointer(MemoryBlockFactory.createMemoryBlock(36));
n.setParameter(i++, Type.LONG, ""+s1);
n.setParameter(i++, Type.STRING,s2);
// n.setParameter(i++, use.createPointer());
n.setParameter(i++, s6);
n.setParameter(i++, Type.LONG, ""+s4);
n.invoke(); // 调用方法
//System.out.println(n.getParameter(0));
// s3=s6.getAsString();
System.out.println(s6.getAsString()+"fhz");
Object [] mm=new Object[2];
mm[0]=n.getRetVal();
// mm[1]=use.getValueFromPointer();
mm[1]=s6.getAsString();
// mm[1]=s3;
//use.dispose();
return mm;
}
但总是得不到正确值
|
|
回复:不写jni调用系统函数 原创空间, 软件技术, 电脑与网络
1111111111(游客)发表评论于2007/9/26 19:33:40 |
你的网站用[url=http://www.huohuliulanqixiazai.com]火狐[/url]浏览器打开好像有点问题呀 |
|
» 1 »
|