java 获取硬件的信息

1.

(主板序列号)Motherboard serial number
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MiscUtils {
  private MiscUtils() {  }

  public static String getMotherboardSN() {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs =
         "Set objWMIService = GetObject(/"winmgmts:.//root//cimv2/")/n"
        + "Set colItems = objWMIService.ExecQuery _ /n"
        + "   (/"Select * from Win32_BaseBoard/") /n"
        + "For Each objItem in colItems /n"
        + "    Wscript.Echo objItem.SerialNumber /n"
        + "    exit for  ' do the first cpu only! /n"
        + "Next /n";

      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }

  public static void main(String[] args){
    String cpuId = MiscUtils.getMotherboardSN();
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, cpuId, "Motherboard serial number",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}
2.

硬盘序列号
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DiskUtils {
  private DiskUtils() {  }

  public static String getSerialNumber(String drive) {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs = "Set objFSO = CreateObject(/"Scripting.FileSystemObject/")/n"
                  +"Set colDrives = objFSO.Drives/n"
                  +"Set objDrive = colDrives.item(/"" + drive + "/")/n"
                  +"Wscript.Echo objDrive.SerialNumber";  // see note
      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }

  public static void main(String[] args){
    String sn = DiskUtils.getSerialNumber("C");
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, sn, "Serial Number of C:",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}
2.

硬盘序列号, 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();    
   
  }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njzdl/archive/2009/10/15/4676302.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Java的一些库来获取电脑的硬件信息,然后根据这些信息生成license。下面是一个简单的示例代码,展示了如何使用Java获取电脑硬件信息: ```java import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class HardwareInfo { public static void main(String[] args) { try { // 获取计算机名 String computerName = InetAddress.getLocalHost().getHostName(); System.out.println("计算机名:" + computerName); // 获取MAC地址 NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); byte[] mac = network.getHardwareAddress(); StringBuilder macAddress = new StringBuilder(); if (mac != null) { for (int i = 0; i < mac.length; i++) { macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); } } System.out.println("MAC地址:" + macAddress.toString()); } catch (UnknownHostException | SocketException e) { e.printStackTrace(); } } } ``` 这段代码使用了`InetAddress`类获取计算机名,使用`NetworkInterface`类获取MAC地址。您可以根据需要扩展代码,获取其他硬件信息,如CPU序列号、主板序列号等。 请注意,这种方式只能提供一些基本的硬件信息,并不能保证唯一性。如果您需要更精确的硬件信息来生成license,可以考虑使用其他方式,如使用第三方库获取更多的硬件信息,或者结合其他硬件标识符生成唯一的license。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值