我做了1个java调用c++的dll 
想做一个java调用C的dll就不知道怎么做了在xx.c里 写
JNIEXPORT jint JNICALL Java_testdll_getValue
(JNIEnv *, jobject)
这些报错 之前在XX.CPP里写可以 c语言不了解 谁有
java调C的dll的例子呀,或者给我讲讲也行 谢谢 

解决方案 »

  1.   

    百毒、骨骼,搜索JNA,会有大量的例程
      

  2.   

    hi 这个还真没用过,不过我看过java 类加载机制,
    有三个加载方式:
    根加载器(顶级):Bootstrap Loader
    扩展类加载器:Extended Loader 
    系统类加载器:AppClass Loader其中顶级加载器 并不是调用java加载的,而是调用C dll加载的,你可以看一下这一块的java源码。
      

  3.   

    好像是 System.loadLibrary这个吧。
    我们的项目里有一段,贴出来参考一下吧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);
    }
    }
    }}