如何在同一程序中同时获取处理器ID和系统板ID? [英] how to get both processor and system board ids in same program?

查看:81
本文介绍了如何在同一程序中同时获取处理器ID和系统板ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.xybion.java.IDs;
import java.util.Enumeration;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class Test1 {
    public static void main(String[] args) {
        ComThread.InitMTA();
        try {
            ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
            Variant instances = wmi.invoke("InstancesOf", "Win32_BaseBoard");
            Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
            while (en.hasMoreElements())
            {
                ActiveXComponent bb = new ActiveXComponent(en.nextElement().getDispatch());
                System.out.println(bb.getPropertyAsString("SerialNumber"));
                break;
            }
        } finally {
            ComThread.Release();
        }
    }
}





我正在使用此代码获取序列号.我想在同一代码中同时获得系统板ID和处理器ID.
可以使用列表或集合之类的概念吗?
或我怎么能得到这个?
提前谢谢.
Siraj





i''m using this code for getting serialnumber. I want to get both systemboard id and processor id in a same code.
Is it possible to use concept like list or set???
or How could i get this?
Thanks in advance.
Siraj

推荐答案

您必须使用此代码来获取处理器以及主板ID,这是经过测试的代码,因为在Java中没有用于此的API,因此我们使用Windows API并将其用作Java Script方式,我们必须在访问信息方面使用相同的属性.

Hi you must use this code for getting the processor as well as Main board ID this is the tested code because in java there is no API for this so we use the Windows API and using it as Java Script fashion and we done you must use the same attribute in which regards you access info.

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_Processor\") \n"
        + "For Each objItem in colItems \n"
        + "    Wscript.Echo objItem.Name \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);
  }
}


这篇关于如何在同一程序中同时获取处理器ID和系统板ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆