我做了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的例子呀,或者给我讲讲也行 谢谢
解决方案 »
- 紧急求救,1.4的程序我用1.5编译了之后 添加了两个jar包 xerces.jar xercesimpl.jar 好像是包冲突了报错
- java bean 转化成XML是遇到错误,无法将bean里面的值取出!
- 大神帮忙,如何进行系统后台进程的优化控制?小弟雪中急等...
- Runtime.exec调用外部程序,如何做到不与其产生关联?
- 关于JDBC、Hibernate数据持久化的事务处理
- EJB如何引用外部的jar包?
- 关于structs+hibernate+spring我迷惘了!!!!
- Session过期之后清除Session,自动跳转到登录页面,急急急,在线等
- 请教: jaxp中的TransformerFactory问题
- j2ee在Win98下该如何配置,
- 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);
}
}
}}