java如何判断一个dll库是否加载_在Java上加载DLL库

这篇博客展示了如何在Java中使用JNI(Java Native Interface)来加载和执行DLL库,特别是一个名为CRC32的示例。通过创建CRC32.java、CRC32.h和CRC32.c文件,详细阐述了从声明JNI函数到实现CRC32算法的过程。
摘要由CSDN通过智能技术生成

这是一个如何在Java中执行CRC32的JNI示例:

CRC32.java:

public class CRC32 {

// JNI function must be declared native

public static native int crc32(int crc, final byte[] buf);

/*

public static void main(String[] argv) {}

*/

static {

System.loadLibrary("crc32"); // Load your dll with System.loadLibrary

}

}使用javah -jni 创建头文件

CRC32.h:

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class CRC32 */

#ifndef _Included_CRC32

#define _Included_CRC32

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class: CRC32

* Method: crc32

* Signature: (I[B)I

*/

JNIEXPORT jint JNICALL Java_CRC32_crc32

(JNIEnv *, jclass, jint, jbyteArray);

#ifdef __cplusplus

}

#endif

#endifCRC32.c:

此文件显示了JNI用法的示例:

/* For a look at the actual CRC32 algorithm, look at zlib's crc32.c */

#include

#include

#ifdef _MSC_VER

typedef unsigned __int8 uint8_t;

typedef unsigned __int32 uint32_t;

#else

# include

#endif

#include "CRC32.h"

uint32_t crc32(uint32_t crc, const void *const buf, size_t len);

uint32_t crc32(uint32_t crc, const void *const buf, size_t len) {

(void)crc;

(void)buf;

(void)len;

return 0;

}

JNIEXPORT jint JNICALL Java_CRC32_crc32(JNIEnv *jenv, jclass jcls,

jint jcrc, jbyteArray jbuf)

{

size_t len;

uint32_t crc;

jint scrc;

const void *buf;

jthrowable exc;

len = (*env)->GetArrayLength(env, jbuf);

crc = *((uint32_t *)&jcrc);

buf = (*env)->GetPrimitiveArrayCritical(env, jbuf, 0);

crc = crc32(crc, buf, len);

(*env)->ReleasePrimitiveArrayCritical(env, jbuf, buf, 0);

*((uint32_t *)&scrc) = crc;

return scrc;

}希望有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值