Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Oshi (https://github.com/dblock/oshi)
*
* Copyright (c) 2010 - 2016 The Oshi Project Team
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Maintainers:
* dblock[at]dblock[dot]org
* widdis[at]gmail[dot]com
* enrico.bianchi[at]gmail[dot]com
*
* Contributors:
* https://github.com/dblock/oshi/graphs/contributors
*/
package oshi.util.platform.windows;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.Variant.VARIANT;
import com.sun.jna.platform.win32.WTypes.BSTR;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;
import oshi.jna.platform.windows.Ole32;
import oshi.jna.platform.windows.COM.EnumWbemClassObject;
import oshi.jna.platform.windows.COM.WbemClassObject;
import oshi.jna.platform.windows.COM.WbemLocator;
import oshi.jna.platform.windows.COM.WbemServices;
import oshi.util.ParseUtil;
/**
* Provides access to WMI queries
*
* @author widdis[at]gmail[dot]com
*/
public class WmiUtil {
private static final Logger LOG = LoggerFactory.getLogger(WmiUtil.class);
public static final String DEFAULT_NAMESPACE = "ROOT\\CIMV2";
private static boolean securityInitialized = false;
/**
* Enum for WMI queries for proper parsing from the returned VARIANT
*/
public enum ValueType {
STRING, LONG, FLOAT, DATETIME
}
/**
* Get a single Long value from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param property
* The property whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A Long containing the value of the requested property
*/
public static Long selectLongFrom(String namespace, String wmiClass, String property, String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, property,
wmiClass, whereClause, ValueType.LONG);
if (result.containsKey(property) && !result.get(property).isEmpty()) {
return (Long) result.get(property).get(0);
}
return 0L;
}
/**
* Get multiple Long values from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param properties
* A comma delimited list of properties whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A map, with each property as the key, containing Longs with the
* value of the requested properties. Each list's order corresponds
* to other lists.
*/
public static Map> selectLongsFrom(String namespace, String wmiClass, String properties,
String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, properties,
wmiClass, whereClause, ValueType.LONG);
HashMap> longMap = new HashMap<>();
for (String key : result.keySet()) {
ArrayList longList = new ArrayList<>();
for (Object obj : result.get(key)) {
longList.add((Long) obj);
}
longMap.put(key, longList);
}
return longMap;
}
/**
* Get a single Float value from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param property
* The property whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A Float containing the value of the requested property
*/
public static Float selectFloatFrom(String namespace, String wmiClass, String property, String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, property,
wmiClass, whereClause, ValueType.FLOAT);
if (result.containsKey(property) && !result.get(property).isEmpty()) {
return (Float) result.get(property).get(0);
}
return 0f;
}
/**
* Get multiple Float values from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param properties
* A comma delimited list of properties whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A map, with each property as the key, containing Floats with the
* value of the requested properties. Each list's order corresponds
* to other lists.
*/
public static Map> selectFloatsFrom(String namespace, String wmiClass, String properties,
String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, properties,
wmiClass, whereClause, ValueType.FLOAT);
HashMap> floatMap = new HashMap<>();
for (String key : result.keySet()) {
ArrayList floatList = new ArrayList<>();
for (Object obj : result.get(key)) {
floatList.add((Float) obj);
}
floatMap.put(key, floatList);
}
return floatMap;
}
/**
* Get a single String value from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param property
* The property whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A string containing the value of the requested property
*/
public static String selectStringFrom(String namespace, String wmiClass, String property, String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, property,
wmiClass, whereClause, ValueType.STRING);
if (result.containsKey(property) && !result.get(property).isEmpty()) {
return (String) result.get(property).get(0);
}
return "";
}
/**
* Get multiple String values from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param properties
* A comma delimited list of properties whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @return A map, with each property as the key, containing strings with the
* value of the requested properties. Each list's order corresponds
* to other lists.
*/
public static Map> selectStringsFrom(String namespace, String wmiClass, String properties,
String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, properties,
wmiClass, whereClause, ValueType.STRING);
HashMap> strMap = new HashMap<>();
for (String key : result.keySet()) {
ArrayList strList = new ArrayList<>();
for (Object obj : result.get(key)) {
strList.add((String) obj);
}
strMap.put(key, strList);
}
return strMap;
}
/**
* Get multiple individually typed values from WMI
*
* @param namespace
* The namespace or null to use the default
* @param wmiClass
* The class to query
* @param properties
* A comma delimited list of properties whose value to return
* @param whereClause
* A WQL where clause matching properties and keywords
* @param propertyTypes
* An array of types corresponding to the properties
* @return A map, with each property as the key, containing Objects with the
* value of the requested properties. Each list's order corresponds
* to other lists. The type of the objects is identified by the
* propertyTypes array. It is the responsibility of the caller to
* cast the returned objects.
*/
public static Map> selectObjectsFrom(String namespace, String wmiClass, String properties,
String whereClause, ValueType[] propertyTypes) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, properties,
wmiClass, whereClause, propertyTypes);
return result;
}
/**
* Query WMI for values
*
* @param namespace
* The namespace to query
* @param properties
* A single property or comma-delimited list of properties to
* enumerate
* @param wmiClass
* The WMI class to query
* @param valType
* The type of data being queried, to control how VARIANT is
* parsed
* @return A map, with the string value of each property as the key,
* containing a list of Objects which can be cast appropriately per
* valType. The order of objects in each list corresponds to the
* other lists.
*/
private static Map> queryWMI(String namespace, String properties, String wmiClass,
String whereClause, ValueType valType) {
// Set up empty map
String[] props = properties.split(",");
ValueType[] propertyTypes = new ValueType[props.length];
for (int i = 0; i < props.length; i++) {
propertyTypes[i] = valType;
}
return queryWMI(namespace, properties, wmiClass, whereClause, propertyTypes);
}
/**
* Query WMI for values
*
* @param namespace
* The namespace to query
* @param properties
* A single property or comma-delimited list of properties to
* enumerate
* @param wmiClass
* The WMI class to query
* @param propertyTypes
* An array corresponding to the properties, containing the type
* of data being queried, to control how VARIANT is parsed
* @return A map, with the string value of each property as the key,
* containing a list of Objects which can be cast appropriately per
* valType. The order of objects in each list corresponds to the
* other lists.
*/
private static Map> queryWMI(String namespace, String properties, String wmiClass,
String whereClause, ValueType[] propertyTypes) {
// Set up empty map
Map> values = new HashMap<>();
String[] props = properties.split(",");
for (int i = 0; i < props.length; i++) {
values.put(props[i], new ArrayList