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 java.util.Map.Entry;
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, UINT32, FLOAT, DATETIME, BOOLEAN, UINT64
}
/**
* For WMI queries requiring array input
*/
private static final ValueType[] STRING_TYPE = { ValueType.STRING };
private static final ValueType[] UINT32_TYPE = { ValueType.UINT32 };
private static final ValueType[] FLOAT_TYPE = { ValueType.FLOAT };
/**
* Get a single Unsigned Integer value from WMI (as Long)
*
* @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 selectUint32From(String namespace, String wmiClass, String property, String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, property,
wmiClass, whereClause, UINT32_TYPE);
if (result.containsKey(property) && !result.get(property).isEmpty()) {
return (Long) result.get(property).get(0);
}
return 0L;
}
/**
* Get multiple Unsigned Integer values from WMI (as Longs)
*
* @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> selectUint32sFrom(String namespace, String wmiClass, String properties,
String whereClause) {
Map> result = queryWMI(namespace == null ? DEFAULT_NAMESPACE : namespace, properties,
wmiClass, whereClause, UINT32_TYPE);
HashMap> longMap = new HashMap<>();
for (Entry> entry : result.entrySet()) {
ArrayList longList = new ArrayList<>();
for (Object obj : entry.getValue()) {
longList.add((Long) obj);
}
longMap.put(entry.getKey(), 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, FLOAT_TYPE);
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, FLOAT_TYPE);
HashMap> floatMap = new HashMap<>();
for (Entry> entry : result.entrySet()) {
ArrayList floatList = new ArrayList<>();
for (Object obj : entry.getValue()) {
floatList.add((Float) obj);
}
floatMap.put(entry.getKey(), 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, STRING_TYPE);
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, STRING_TYPE);
HashMap> strMap = new HashMap<>();
for (Entry> entry : result.entrySet()) {
ArrayList strList = new ArrayList<>();
for (Object obj : entry.getValue()) {
strList.add((String) obj);
}
strMap.put(entry.getKey(), 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, or a single
* element array
* @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. If only one propertyType is given, all
* Objects will have that type. 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) {
return queryWMI(namespace == null ? DEFAULT_NAMESPACE : 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 (String prop : props) {
values.put(prop, new ArrayList