java如何调用dll

来源:互联网 发布:ngrok 没有域名 编辑:程序博客网 时间:2024/06/08 08:41

我是一个C/C++程序辕,最近突然写个dll给java调用。查了相关的资料,基本上实现了。现在总结一下。


package com.vision.io;

import java.io.FileOutputStream;import java.io.OutputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.io.File;public class USBFileReader {/** * 打开USB设备上的文件 * @param path * @return */private static native long openDeviceFile(String path);/** * 关闭设备文件 * @param path * @return */private static native void closeDeviceFile(long fileHandler);/** * 从设备中读取多个字节 * @param filehandler 文件句柄  * @param b 缓存 * @param off 偏移 * @param len 长度 * @return * @throws IOException */private native int readBytesFromDevice(long filehandler,byte b[], int off, int len) throws IOException;/** * 从设备中读取一个字节 * @param filehandler * @return * @throws IOException */public native int readByteFromDevice(long filehandler) throws IOException;/** * 取得设备中文件的长度 * @param filehandler * @return */private native int getDeviceFileLength(long filehandler);public native long skip(long n,long filehandler) throws IOException;public native int available(long filehandler) throws IOException;static{System.loadLibrary("Java_com_vision_io");   }    public static void main(String[] args)  {  USBFileReader Reader;  OutputStream OutFile;  String sz;  long hFile;  int nSize;  int nSizeReaded = 0;  int nTotalSizeReaded = 0;  byte[] byBuff;    sz = "1.1.1.1 dll"; // 这个文件名由两个部分组成。前面是路径,后面是扩展名(如果有的)。中间用空格分离。    Reader = new USBFileReader();  hFile = Reader.openDeviceFile(sz);    sz = "1";  try  {  Reader.available(hFile);  }  catch(IOException e)  {    }  nSize = Reader.getDeviceFileLength(hFile);  byBuff = new byte[nSize];    try  {  OutFile = new FileOutputStream(sz);    while (true)  {  nSizeReaded = Reader.readBytesFromDevice(hFile, byBuff, nTotalSizeReaded, 1024);  if (nSizeReaded == -1)  {  break;  }    nTotalSizeReaded += nSizeReaded;  OutFile.write(byBuff, 0, nSizeReaded);  }    OutFile.close();  }  catch(IOException e)  {    }    // test  readByteFromDevice  sz = "3";  try  {  OutFile = new FileOutputStream(sz);  Reader.skip(0, hFile);    while (true)  {  nSizeReaded = Reader.readByteFromDevice(hFile);  if (nSizeReaded == -1)  {  break;  }    //nTotalSizeReaded += nSizeReaded;  OutFile.write(nSizeReaded);  }    OutFile.close();  }  catch(IOException e)  {    }    Reader.closeDeviceFile(hFile);  }}

保存生成USBFileReader.java。

进入命令行 输入 javac -d  . USBFileReader.java。 生成了包 com.vision.io

然后输入命令 javah com.vision.io.USBFileReader 生成 com_vision_io_USBFileReader.h文件

文件类容如下;


/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_vision_io_USBFileReader */#ifndef _Included_com_vision_io_USBFileReader#define _Included_com_vision_io_USBFileReader#ifdef __cplusplusextern "C" {#endif/* * Class:     com_vision_io_USBFileReader * Method:    openDeviceFile * Signature: (Ljava/lang/String;)J */JNIEXPORT jlong JNICALL Java_com_vision_io_USBFileReader_openDeviceFile  (JNIEnv *, jclass, jstring);/* * Class:     com_vision_io_USBFileReader * Method:    closeDeviceFile * Signature: (J)V */JNIEXPORT void JNICALL Java_com_vision_io_USBFileReader_closeDeviceFile  (JNIEnv *, jclass, jlong);/* * Class:     com_vision_io_USBFileReader * Method:    readBytesFromDevice * Signature: (J[BII)I */JNIEXPORT jint JNICALL Java_com_vision_io_USBFileReader_readBytesFromDevice  (JNIEnv *, jobject, jlong, jbyteArray, jint, jint);/* * Class:     com_vision_io_USBFileReader * Method:    readByteFromDevice * Signature: (J)I */JNIEXPORT jint JNICALL Java_com_vision_io_USBFileReader_readByteFromDevice  (JNIEnv *, jobject, jlong);/* * Class:     com_vision_io_USBFileReader * Method:    getDeviceFileLength * Signature: (J)I */JNIEXPORT jint JNICALL Java_com_vision_io_USBFileReader_getDeviceFileLength  (JNIEnv *, jobject, jlong);/* * Class:     com_vision_io_USBFileReader * Method:    skip * Signature: (JJ)J */JNIEXPORT jlong JNICALL Java_com_vision_io_USBFileReader_skip  (JNIEnv *, jobject, jlong, jlong);/* * Class:     com_vision_io_USBFileReader * Method:    available * Signature: (J)I */JNIEXPORT jint JNICALL Java_com_vision_io_USBFileReader_available  (JNIEnv *, jobject, jlong);#ifdef __cplusplus}#endif#endif


根据这个文件编写dll就可以让java调用了。这里要对参数说明一下:

JNIEnv * : 这是一个接口的指针。提供了相关的C数据和java数据之间转换的接口。可以很方便对java数据操作。

jobject  :  这是java实例对象。没有用到,忽略。

后面的参数是具体传入的参数。


在开发dll中缺少相关的文件。在java目录中寻找,然后覆盖到vc的相关目录。



原创粉丝点击