JAVA类:

package org.hexun.wangchong.admin.gethardware;

public class GetHardWareInfo {
static {
System.loadLibrary("disknum");
}

public native static String getHardWareInfo();

public static void main(String s[]) {
System.out.println(getHardWareInfo());
}
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在linux终端下生成.h的头文件

javac GetHardWareInfo .java //进到gethardwar目录下

javah org.hexun.wangchong.admin.gethardware.GetHardWareInfo //进行org的父目录下

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

生成的头文件内容如下:

org_hexun_wangchong_admin_gethardware_GetHardWareInfo.h :

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_hexun_wangchong_admin_gethardware_GetHardWareInfo */

#ifndef _Included_org_hexun_wangchong_admin_gethardware_GetHardWareInfo
#define _Included_org_hexun_wangchong_admin_gethardware_GetHardWareInfo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_hexun_wangchong_admin_gethardware_GetHardWareInfo
* Method: getHardWareInfo
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_hexun_wangchong_admin_gethardware_GetHardWareInfo_getHardWareInfo
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

写一个.c格式的文件,命名为 disknum.c 内容如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/io.h>
#include "org_hexun_wangchong_admin_gethardware_GetHardWareInfo.h"

typedef unsigned short WORD;
static char * ConvertToString (WORD diskdata [256], int firstIndex, int lastIndex)
{
static char string [1024];
int index = 0;
int position = 0;

// each integer has two characters stored in it backwards
for (index = firstIndex; index <= lastIndex; index++)
{
// get low byte for 1st character
string [position] = (char) (diskdata [index] / 256);
position++;

// get high byte for 2nd character
string [position] = (char) (diskdata [index] % 256);
position++;
}

// end the string
string [position] = '\0';

// cut off the trailing blanks
for (index = position - 1; index > 0 && ' ' == string [index]; index--)
string [index] = '\0';

return string;
}
static int WaitIde()
{
int al;

while ((al=inb(0x1F7))>=0x80) ;
return al;
}
static void ReadIDE(WORD pw[256])
{
int al;
int i;

for (i = 0; i < 256; i++)
pw[0] = '\0';
ioperm(0x1f0, 16, 1);
WaitIde();
outb(0xA0,0x1F6);
al = WaitIde();
if ((al&0x50)!=0x50) return;

outb(0xA0,0x1F6);
outb(0xEC,0x1F7);
al = WaitIde();
if ((al&0x58)!=0x58) return;

for (i=0;i<256;i++) {
pw[i] = inw(0x1F0);
}
}

JNIEXPORT jstring JNICALL Java_org_hexun_wangchong_admin_gethardware_GetHardWareInfo_getHardWareInfo
(JNIEnv *env, jobject t)
{
char *hd_sn_ptr;
if (sizeof(WORD) != 2)
fprintf(stderr, "%s", "type is error!\n");
WORD pw[256];
ReadIDE(pw);
hd_sn_ptr = ConvertToString (pw, 10, 19);
// fprintf(stdout, "[%s]\n", hd_sn_ptr);
//
jstring str ;
str = (*env)->NewStringUTF(env,hd_sn_ptr);
///
return str;

}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在终端下敲入

gcc -I/usr/lib/j2sdk1.5-ibm/include -fPIC -c disknum.c

gcc -shared -WI -soname disknum.o -o libdisknum.so //此处须在disknum前处加一个lib 即libdisknum.so

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

将 libdisknum.so库拷贝到 jdk目录/jre/lib/i386下

终端运行 java GetHardWareInfo 击可

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Logo

更多推荐