以下是获取硬盘序列号,CPU 道理一样.
// Sys.java
public class Sys
{
public static native void showHDSerial();
static {
System.loadLibrary("Sys");
}
public static void main(String[] args)
{
showHDSerial();
}
}
1) 编译
javac Sys.java
2)生成 .h 文件
javah -jni Sys
3)打开VC->文件->新建->工程->Win32 DLL (这里简写了)
4)写入工程名;Sys 创建空白工程
5)将Sys.h,jni.h 添加到工程中(其中jni.h在[JAVA_HOME]\include 下).
6)创建Sys.cpp 文件.文件内容如下:
// Sys.cpp
#include "jni.h"
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#pragma pack(1)
typedef struct _GETVERSIONOUTPARAMS {
BYTE bVersion; // Binary driver version.
BYTE bRevision; // Binary driver revision.
BYTE bReserved; // Not used.
BYTE bIDEDeviceMap; // Bit map of IDE devices.
DWORD fCapabilities; // Bit mask of driver capabilities.
DWORD dwReserved[4]; // For future use.
} GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;
typedef struct _IDEREGS {
BYTE bFeaturesReg; // Used for specifying SMART "commands".
BYTE bSectorCountReg; // IDE sector count register
BYTE bSectorNumberReg; // IDE sector number register
BYTE bCylLowReg; // IDE low order cylinder value
BYTE bCylHighReg; // IDE high order cylinder value
BYTE bDriveHeadReg; // IDE drive/head register
BYTE bCommandReg; // Actual IDE command.
BYTE bReserved; // reserved for future use. Must be zero.
} IDEREGS, *PIDEREGS, *LPIDEREGS;
typedef struct _SENDCMDINPARAMS {
DWORD cBufferSize; // Buffer size in bytes
IDEREGS irDriveRegs; // Structure with drive register values.
BYTE bDriveNumber; // Physical drive number to send
// command to (0,1,2,3).
BYTE bReserved[3]; // Reserved for future expansion.
DWORD dwReserved[4]; // For future use.
//BYTE bBuffer[1]; // Input buffer.
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;
typedef struct _DRIVERSTATUS {
BYTE bDriverError; // Error code from driver,
// or 0 if no error.
BYTE bIDEStatus; // Contents of IDE Error register.
// Only valid when bDriverError
// is SMART_IDE_ERROR.
BYTE bReserved[2]; // Reserved for future expansion.
DWORD dwReserved[2]; // Reserved for future expansion.
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;
typedef struct _SENDCMDOUTPARAMS {
DWORD cBufferSize; // Size of bBuffer in bytes
DRIVERSTATUS DriverStatus; // Driver status structure.
BYTE bBuffer[512]; // Buffer of arbitrary length
// in which to store the data read from the drive.
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;
typedef struct _IDSECTOR {
USHORT wGenConfig;
USHORT wNumCyls;
USHORT wReserved;
USHORT wNumHeads;
USHORT wBytesPerTrack;
USHORT wBytesPerSector;
USHORT wSectorsPerTrack;
USHORT wVendorUnique[3];
CHAR sSerialNumber[20];
USHORT wBufferType;
USHORT wBufferSize;
USHORT wECCSize;
CHAR sFirmwareRev[8];
CHAR sModelNumber[40];
USHORT wMoreVendorUnique;
USHORT wDoubleWordIO;
USHORT wCapabilities;
USHORT wReserved1;
USHORT wPIOTiming;
USHORT wDMATiming;
USHORT wBS;
USHORT wNumCurrentCyls;
USHORT wNumCurrentHeads;
USHORT wNumCurrentSectorsPerTrack;
ULONG ulCurrentSectorCapacity;
USHORT wMultSectorStuff;
ULONG ulTotalAddressableSectors;
USHORT wSingleWordDMA;
USHORT wMultiWordDMA;
BYTE bReserved[128];
} IDSECTOR, *PIDSECTOR;
/*+++
Global vars
---*/
GETVERSIONOUTPARAMS vers;
SENDCMDINPARAMS in;
SENDCMDOUTPARAMS out;
HANDLE h;
DWORD i;
BYTE j;
VOID ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{
USHORT i;
CHAR temp;
for (i = 0; i < uscStrSize; i+=2)
{
temp = szString[i];
szString[i] = szString[i+1];
szString[i+1] = temp;
}
}
void DetectIDE(BYTE bIDEDeviceMap){
if (bIDEDeviceMap&1){
if (bIDEDeviceMap&16){
cout<<"ATAPI device is attached to primary controller, drive 0."<<endl;
}else{
cout<<"IDE device is attached to primary controller, drive 0."<<endl;
}
}
if (bIDEDeviceMap&2){
if (bIDEDeviceMap&32){
cout<<"ATAPI device is attached to primary controller, drive 1."<<endl;
}else{
cout<<"IDE device is attached to primary controller, drive 1."<<endl;
}
}
if (bIDEDeviceMap&4){
if (bIDEDeviceMap&64){
cout<<"ATAPI device is attached to secondary controller, drive 0."<<endl;
}else{
cout<<"IDE device is attached to secondary controller, drive 0."<<endl;
}
}
if (bIDEDeviceMap&8){
if (bIDEDeviceMap&128){
cout<<"ATAPI device is attached to secondary controller, drive 1."<<endl;
}else{
cout<<"IDE device is attached to secondary controller, drive 1."<<endl;
}
}
}
void hdid9x(){
ZeroMemory(&vers,sizeof(vers));
//We start in 95/98/Me
h=CreateFile("\\\\.\\Smartvsd",0,0,0,CREATE_NEW,0,0);
if (!h){
cout<<"open smartvsd.vxd failed"<<endl;
exit(0);
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
cout<<"DeviceIoControl failed:DFP_GET_VERSION"<<endl;
CloseHandle(h);
return;
}
//If IDE identify command not supported, fails
if (!(vers.fCapabilities&1)){
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return;
}
//Display IDE drive number detected
DetectIDE(vers.bIDEDeviceMap);
//Identify the IDE drives
for (j=0;j<4;j++){
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (j&1){
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{
in.irDriveRegs.bDriveHeadReg=0xa0;
}
if (vers.fCapabilities&(16>>j)){
//We don't detect a ATAPI device.
cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
continue;
}else{
in.irDriveRegs.bCommandReg=0xec;
}
in.bDriveNumber=j;
in.irDriveRegs.bSectorCountReg=1;
in.irDriveRegs.bSectorNumberReg=1;
in.cBufferSize=512;
if (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0)){
cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
CloseHandle(h);
return;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
cout<<"\tSerial Number:"<<s<<endl;
cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
}
//Close handle before quit
CloseHandle(h);
// CopyRight();
}
void hdidnt(){
char hd[80];
PIDSECTOR phdinfo;
char s[41];
ZeroMemory(&vers,sizeof(vers));
//We start in NT/Win2000
for (j=0;j<4;j++){
sprintf(hd,"\\\\.\\PhysicalDrive%d",j);
h=CreateFile(hd,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if (!h){
continue;
}
if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
CloseHandle(h);
continue;
}
//If IDE identify command not supported, fails
if (!(vers.fCapabilities&1)){
cout<<"Error: IDE identify command not supported.";
CloseHandle(h);
return;
}
//Identify the IDE drives
ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));
if (j&1){
in.irDriveRegs.bDriveHeadReg=0xb0;
}else{
in.irDriveRegs.bDriveHeadReg=0xa0;
}
if (vers.fCapabilities&(16>>j)){
//We don't detect a ATAPI device.
cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
continue;
}else{
in.irDriveRegs.bCommandReg=0xec;
}
in.bDriveNumber=j;
in.irDriveRegs.bSectorCountReg=1;
in.irDriveRegs.bSectorNumberReg=1;
in.cBufferSize=512;
if (!DeviceIoControl(h,DFP_RECEIVE_DRIVE_DATA,&in,sizeof(in),&out,sizeof(out),&i,0)){
cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
CloseHandle(h);
return;
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sModelNumber,40);
s[40]=0;
ChangeByteOrder(s,40);
cout<<endl<<"Module Number:"<<s<<endl;
memcpy(s,phdinfo->sFirmwareRev,8);
s[8]=0;
ChangeByteOrder(s,8);
cout<<"\tFirmware rev:"<<s<<endl;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
cout<<"\tSerial Number:"<<s<<endl;
cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
CloseHandle(h);
}
// CopyRight();
}
JNIEXPORT void JNICALL Java_Sys_showHDSerial(JNIEnv *env, jclass jcls)
{
OSVERSIONINFO VersionInfo;
ZeroMemory(&VersionInfo,sizeof(VersionInfo));
VersionInfo.dwOSVersionInfoSize=sizeof(VersionInfo);
GetVersionEx(&VersionInfo);
switch (VersionInfo.dwPlatformId){
case VER_PLATFORM_WIN32s:
cout<<"Win32s is not supported by this programm."<<endl;
return;
case VER_PLATFORM_WIN32_WINDOWS:
hdid9x();
return;
case VER_PLATFORM_WIN32_NT:
hdidnt();
return;
}
}
7)新建Sys.def 文件,内容如下
EXPORTS
Java_Sys_showHDSerial
编译Sys 工程,生成Sys.dll
9)将Sys.dll 拷到Sys.class 同目录下
10)运行
java Sys
得到如下结果(本人机器)
F:\>java Sys
Module Number:SAMSUNG SP0802N
Firmware rev:TK100-28
Serial Number: S00JJ50Y418303
Capacity:76293M
http://www.javaeye.com/problems/11702
相关推荐
在压缩包中的`jni获得硬盘信息.txt`文件可能包含了如何获取硬盘信息的示例,这同样可以通过JNI实现。获取硬盘使用率通常涉及到读取`/proc/diskstats`或`/sys/block`下的文件,解析其内容并计算磁盘读写速度及使用...
**描述:“Java获得CPU序列号,Java通过JNI连接C语言,从C中获得CPU的序列号。”** - **Java通过JNI连接C语言**:表明了实现该功能的技术路径,即通过JNI机制调用C/C++编写的本地方法来实现对硬件底层的访问。 - **...
- **Java**:可以使用`java.lang.management.ManagementFactory`类获取Windows的硬盘信息,或者通过JNI(Java Native Interface)调用系统特定的API。 - **JavaScript**:在Node.js环境中,可以使用`node-hardware...
3. **性能优化:** 对于某些计算密集型任务,如图像处理、音频处理等,使用C/C++等底层语言编写可能会获得更高的执行效率。此时,可以通过JNI将这些优化过的函数嵌入到Java程序中。 #### 二、JNI实现HelloWorld示例...
Java程序通常会通过JNI调用本地方法,比如Windows的` ManagementObjectSearcher`类,查询WMI(Windows Management Instrumentation)来获取硬盘信息。对于跨平台的应用,可能需要针对不同操作系统编写特定的本地代码...
通过JNA,开发者可以轻松地调用操作系统API,比如Windows API中的函数,以执行底层操作,如本例中的读取硬盘信息。 #### 三、关键类与接口 本案例中涉及的关键类与接口如下: 1. **`Kernel32`**:这是JNA提供的一...
在Java中,我们可以使用Java Native Interface(JNI)来调用Windows API,获得系统的内存信息和CPU使用率。我们可以使用GetSystemInfo函数来获得系统的内存信息,包括物理内存的大小、可用内存的大小和页面文件的...
这样,根据运行的平台,选择对应的实现,即可获得硬件信息。例如,可以有`HardwareInfoProvider`接口,以及`WindowsHardwareInfoProvider`、`LinuxHardwareInfoProvider`和`MacOSHardwareInfoProvider`这三个实现类...
3. **磁盘信息**:包括硬盘类型、容量、使用情况等。Windows中可以使用`wmic diskdrive`或`Get-CimInstance Win32_DiskDrive` PowerShell命令,而Linux中可读取`/dev/disk/*`下的信息或运行`fdisk -l`。源码可能包含...
描述中提到"用Java程序写的,可以获得硬盘的序列号,非常有用!",这表明该程序是用Java编程语言编写的,能够读取并显示硬盘的序列号信息。Java是一种跨平台的编程语言,因此这个程序可以在不同操作系统上运行,如...
- 需要有至少15GB的硬盘空间用于安装系统。 - 安装完成后,确保计算机能够连接互联网,并通过命令行安装必要的软件包。 - 命令示例: ```bash sudo apt-get install ssh flex bison gperf libsdl-dev libesd0-...
根据给定的文件信息,下面是关于“山东大学计算机学院(2018级)嵌入式系统原理与应用限选课期末考试题”的详细知识点整理。 1. 嵌入式系统与普通PC的相同点与不同点 嵌入式系统与普通PC在许多基本的计算机组成原理...
� 良好的盈利模式( 3/7 开),产业链条的各方:运营商、制造商、独立软件生产商都可以获得不错的利 益 。 将移动终端的评价标准从硬件向软件转变,极大的激发了软件开发者的热情。 � Android 的源代码遵循 Apache...
首先,硬件部分通常会涉及计算机系统的组成,包括处理器(CPU)、内存(RAM)、存储设备(硬盘、SSD)、输入输出设备等。讲解这部分时,我们会讨论CPU的架构,如指令集、流水线技术、缓存系统,以及如何通过并行处理...