我做了1个java调用c++的dll
想做一个java调用C的dll就不知道怎么做了在xx.c里 写
JNIEXPORT jint JNICALL Java_testdll_getValue
(JNIEnv *, jobject)
这些报错 之前在XX.CPP里写可以 c语言不了解 谁有
java调C的dll的例子呀,或者给我讲讲也行 谢谢
想做一个java调用C的dll就不知道怎么做了在xx.c里 写
JNIEXPORT jint JNICALL Java_testdll_getValue
(JNIEnv *, jobject)
这些报错 之前在XX.CPP里写可以 c语言不了解 谁有
java调C的dll的例子呀,或者给我讲讲也行 谢谢
解决方案 »
- 新人想接触struts2,各位大虾推荐推荐...
- 表tb_table中有time列,类型为datatime,如何取当天时间的所有数据
- No SqlMapClient specified是什么错误?怎么解决啊???
- 日文系统下,含日文注释的JAVA代码生成到XML后,"①"乱码,请各位帮忙解决,
- 关于Struts页面映射的问题
- ibatis中文问题,急!
- 进到Tomcat的manager页面不能启动admin,http://localhost:8080/admin访问失
- 关于集合的问题(急)
- JTree控件如何利用鼠标右键弹出Popup菜单
- 在java中如何验证ssl证书的有效性
- bootstrap chm
- javascript提交
有三个加载方式:
根加载器(顶级):Bootstrap Loader
扩展类加载器:Extended Loader
系统类加载器:AppClass Loader其中顶级加载器 并不是调用java加载的,而是调用C dll加载的,你可以看一下这一块的java源码。
我们的项目里有一段,贴出来参考一下吧package org.scavino.twain;import ins.filemanager.fileuploader.FileUploaderConst;
import ins.filemanager.fileuploader.FileUploaderFrame;
import ins.filemanager.fileuploader.FileUploaderParams;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;import javax.swing.JOptionPane;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class JTwain {
private static final Log logger = LogFactory
.getLog(JTwain.class); private static final JTwain mInstance = new JTwain(); protected final String DLL_NAME = "jtwain";
private JTwain() {
initLib();
}
public static JTwain getInstance(){
return mInstance;
} public native boolean isTwainAvailble();
public native String[] getAvailableSources();
public native String[] acquire();
public native String[] acquire(String sourceName); /*
* Loads the C/JNI Libray
*/
private void initLib(){ try {
String libraryPath = System.getProperty("java.library.path"); if ((libraryPath == null) || (libraryPath.trim().length() == 0)) {
logger.info("");
return;
} String systemDir = null;
String firstEntry = null;
String original;
String entry; StringTokenizer st = new StringTokenizer(libraryPath, ";");
int i = 0;
while (i < st.countTokens()) {
original = st.nextToken();
entry = original;
if (i == 0) {
firstEntry = entry;
}
if (entry.endsWith("\\")) {
entry = entry.substring(0, entry.length() - 1);
}
entry = entry.toLowerCase();
if (entry.endsWith("windows\\system")) {
systemDir = original;
break;
}
if (entry.endsWith("winnt\\system32")) {
systemDir = original;
break;
}
if (entry.endsWith("windows\\system32")) {
systemDir = original;
File file = new File(original.replace("System32", "SysWow64"));
if(file.exists()){
systemDir = file.getPath();
}
break;
}
i++;
}
if (systemDir == null) {
systemDir = firstEntry;
}
installFile(new URL(FileUploaderConst.FileUploadURL), systemDir
+ "/jtwain.dll");
System.loadLibrary(DLL_NAME);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e);
}
finally {
// Send to your logging subsystem
System.out.println("Loading : " + DLL_NAME + ".dll");
}
}
/**
* dll
*/
protected static void installFile(URL sourceUrl, String destFileName) {
File destFile = new File(destFileName);
if (!destFile.exists()) {
try {
System.err.println("installing file " + destFileName);
destFile.getParentFile().mkdirs();
URLConnection connection = sourceUrl.openConnection();
InputStream is = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buff = new byte[8192];
BufferedInputStream in = new BufferedInputStream(is,
buff.length);
BufferedOutputStream out = new BufferedOutputStream(fos,
buff.length);
int i;
int count = 0;
while ((i = in.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, i);
count += i;
}
in.close();
out.close();
} catch (Exception exception) {
exception.printStackTrace();
logger.error(exception);
}
}
}}