Java如何调用Windows system32里的dll文件 各位大侠,如何用Java调用Windows system32里的dll文件。能否给出具体代码与步骤,小弟万分感谢~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class HelloWorld { public native void displayHelloWorld();←左方的native型態是通知Java這個函數會使用到外部函數 static { System.loadLibrary("hello"); ←左方的程式碼代表我要載入hello.dll的動態連結,hello.dll的製作我在稍後會做介紹 } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } } #include #include "HelloWorld.h" #include JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("HelloWorld"); return; } 谢谢啦,不过这个JNI过程可以实现。 我现在需要的是直接对windows中system32文件夹里面的dll进行调用。不用自己重新生成个dll。 System.loadLibrary("pimdll"),这里扩展名.dll可省略java调dll只能调JNI方式生成的DLL,不能调其他DLL 强烈推荐楼主一个东西,叫JNA它可以直接调用动态链接库,而不用写一句本地代码它的地址是:https://jna.dev.java.net我可以举一个非常简单的例子给你:/** * * @author hadeslee */import com.sun.jna.Library;import com.sun.jna.Native;import java.util.logging.Logger;interface Kernel32 extends Library { Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); int GetLogicalDriveStringsA(int length, byte[] buffer); int GetSystemDirectoryA(byte[] buffer, int size); int GetTickCount(); int GetSystemTime(int[] is);}public class Test1 { private static Logger log = Logger.getLogger(Test1.class.getName()); public static void main(String[] args) throws Exception { Kernel32 lib = Kernel32.INSTANCE; byte[] buffer2 = new byte[100]; lib.GetLogicalDriveStringsA(buffer2.length / 2, buffer2); for (byte bt : buffer2) { System.out.print((char) bt); } System.out.println(); byte[] buffer = new byte[50]; lib.GetSystemDirectoryA(buffer, 50); for (byte bt : buffer) { System.out.print((char) bt); } System.out.println(); System.out.println(lib.GetTickCount()); int[] is=new int[1]; System.out.println(lib.GetSystemTime(is)); System.err.println(is[0]);; }} Runtime.getRuntime().exec() 这个我已经知道了。我是指直接调用system32文件夹里的dll文件,实现某个功能。 请问达人这样能否实现? 如果能实现的话, 具体步骤和代码是什么?多谢了 万分感谢 你只要定义一个接口,它继承自Library,然后在里面写上DLL里面有的函数,名字要相同,然后这个接口的实现就由JNA来搞定了,你直接用它就行了. 楼上的达人,能否具体说明一下,我是个java菜鸟,多谢多谢 Java for(:) 怎么取到当前LIST的序号? java的单例模式本身分为哪几种? 用户界面线程中光标位置总是重置问题 急求:请大家给我推荐关于JAVA的书籍 我对System.in.read()不太会用,请赐教! 子类要使用父类中的private class可以怎么处理? 我是Java菜鸟,提一个巨简单的问题,分多得用不完了,进来都给分 求能够快速大量访问网络的办法。 如何修改一个类中方法的变量的值? BasicCodeGenerator 基本类代码生成器JBuilder插件 有个类文件无法编译,求助,分不够立刻加! 突然想到一个问题,进来看。
public native void displayHelloWorld();←左方的native型態是通知Java這個函數會使用到外部函數
static {
System.loadLibrary("hello"); ←左方的程式碼代表我要載入hello.dll的動態連結,hello.dll的製作我在稍後會做介紹
}
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}
#include "HelloWorld.h"
#include
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("HelloWorld");
return;
}
java调dll只能调JNI方式生成的DLL,不能调其他DLL
它可以直接调用动态链接库,而不用写一句本地代码
它的地址是:https://jna.dev.java.net
我可以举一个非常简单的例子给你:
/**
*
* @author hadeslee
*/
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.util.logging.Logger;interface Kernel32 extends Library { Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); int GetLogicalDriveStringsA(int length, byte[] buffer); int GetSystemDirectoryA(byte[] buffer, int size);
int GetTickCount();
int GetSystemTime(int[] is);
}public class Test1 { private static Logger log = Logger.getLogger(Test1.class.getName()); public static void main(String[] args) throws Exception {
Kernel32 lib = Kernel32.INSTANCE;
byte[] buffer2 = new byte[100];
lib.GetLogicalDriveStringsA(buffer2.length / 2, buffer2);
for (byte bt : buffer2) {
System.out.print((char) bt);
}
System.out.println();
byte[] buffer = new byte[50];
lib.GetSystemDirectoryA(buffer, 50);
for (byte bt : buffer) {
System.out.print((char) bt);
}
System.out.println();
System.out.println(lib.GetTickCount());
int[] is=new int[1];
System.out.println(lib.GetSystemTime(is));
System.err.println(is[0]);;
}
}
然后这个接口的实现就由JNA来搞定了,你直接用它就行了.