Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wbemcli classes needed to query WMI #994

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Features
* [#984](https://github.com/java-native-access/jna/issues/984): Added `CM_Locate_DevNode`, `CM_Get_Parent`, `CM_Get_Child`, `CM_Get_Sibling`, `CM_Get_Device_ID`, and `CM_Get_Device_ID_Size` to `c.s.j.platform.win32.Cfgmgr32.java` and a `c.s.j.platform.win32.Cfgmgr32Util` class for `CM_Get_Device_ID` - [@dbwiddis](https://github.com/dbwiddis).
* [#988](https://github.com/java-native-access/jna/issues/988): Added `PdhLookupPerfIndexByEnglishName` to `c.s.j.platform.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis).
* [#992](https://github.com/java-native-access/jna/pull/992): Improve stability of windows tests and add appveyor configuration for windows CI builds - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#994](https://github.com/java-native-access/jna/issues/994): Added `CoInitializeSecurity` and `CoSetProxyBlanket` to `c.s.j.platform.win32.Ole32`, added new `c.s.j.platform.win32.Wbemcli` classes needed to query WMI, and added a `WbemcliUtil` class implementing WMI queries. - [@dbwiddis](https://github.com/dbwiddis).
* [#995](https://github.com/java-native-access/jna/pull/995): Added structures and methods to `c.s.j.platform.mac.SystemB` for Process, Network interface, Swapfile, Time, and Filesystem info - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
Expand Down
250 changes: 250 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/COM/Wbemcli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/* Copyright (c) 2018 Daniel Widdis, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32.COM;

import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.platform.win32.Guid.CLSID;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.Variant.VARIANT;
import com.sun.jna.platform.win32.WTypes;
import com.sun.jna.platform.win32.WTypes.BSTR;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;

/**
* This header is used by Remote Desktop Services. It contains programming
* interfaces for enumerating and querying Common Information Model (CIM)
* objects.
*/
public interface Wbemcli {

public static final int WBEM_FLAG_RETURN_IMMEDIATELY = 0x00000010;
public static final int WBEM_FLAG_FORWARD_ONLY = 0x00000020;
public static final int WBEM_INFINITE = 0xFFFFFFFF;

// Non-error constants
// https://docs.microsoft.com/en-us/windows/desktop/wmisdk/wmi-non-error-constants
public static final int WBEM_S_NO_ERROR = 0x0;
public static final int WBEM_S_FALSE = 0x1;
public static final int WBEM_S_TIMEDOUT = 0x40004;
public static final int WBEM_S_NO_MORE_DATA = 0x40005;

// Error constants
// https://docs.microsoft.com/en-us/windows/desktop/wmisdk/wmi-error-constants
public static final int WBEM_E_INVALID_NAMESPACE = 0x8004100e;
public static final int WBEM_E_INVALID_CLASS = 0x80041010;
public static final int WBEM_E_INVALID_QUERY = 0x80041017;

// CIM Types
public static final int CIM_ILLEGAL = 0xfff;
public static final int CIM_EMPTY = 0;
public static final int CIM_SINT8 = 16;
public static final int CIM_UINT8 = 17;
public static final int CIM_SINT16 = 2;
public static final int CIM_UINT16 = 18;
public static final int CIM_SINT32 = 3;
public static final int CIM_UINT32 = 19;
public static final int CIM_SINT64 = 20;
public static final int CIM_UINT64 = 21;
public static final int CIM_REAL32 = 4;
public static final int CIM_REAL64 = 5;
public static final int CIM_BOOLEAN = 11;
public static final int CIM_STRING = 8;
public static final int CIM_DATETIME = 101;
public static final int CIM_REFERENCE = 102;
public static final int CIM_CHAR16 = 103;
public static final int CIM_OBJECT = 13;
public static final int CIM_FLAG_ARRAY = 0x2000;

/**
* Contains and manipulates both WMI class definitions and class object
* instances.
*/
class IWbemClassObject extends Unknown {

public IWbemClassObject() {
}

public IWbemClassObject(Pointer pvInstance) {
super(pvInstance);
}

public HRESULT Get(WString wszName, int lFlags, VARIANT.ByReference pVal, IntByReference pType,
IntByReference plFlavor) {
// Get is 5th method of IWbemClassObjectVtbl in WbemCli.h
return (HRESULT) _invokeNativeObject(4,
new Object[] { getPointer(), wszName, lFlags, pVal, pType, plFlavor }, HRESULT.class);
}
}

/**
* Used to enumerate Common Information Model (CIM) objects.
*/
class IEnumWbemClassObject extends Unknown {

public IEnumWbemClassObject() {
}

public IEnumWbemClassObject(Pointer pvInstance) {
super(pvInstance);
}

public HRESULT Next(int lTimeOut, int uCount, Pointer[] ppObjects, IntByReference puReturned) {
// Next is 5th method of IEnumWbemClassObjectVtbl in
// WbemCli.h
return (HRESULT) _invokeNativeObject(4,
new Object[] { getPointer(), lTimeOut, uCount, ppObjects, puReturned }, HRESULT.class);
}

public IWbemClassObject[] Next(int lTimeOut, int uCount) {
Pointer[] resultArray = new Pointer[uCount];
IntByReference resultCount = new IntByReference();
HRESULT result = Next(lTimeOut, uCount, resultArray, resultCount);
COMUtils.checkRC(result);
IWbemClassObject[] returnValue = new IWbemClassObject[resultCount.getValue()];
for (int i = 0; i < resultCount.getValue(); i++) {
returnValue[i] = new IWbemClassObject(resultArray[i]);
}
return returnValue;
}
}

/**
* Used to obtain the initial namespace pointer to the IWbemServices
* interface for WMI on a specific host computer.
*/
class IWbemLocator extends Unknown {

public static final CLSID CLSID_WbemLocator = new CLSID("4590f811-1d3a-11d0-891f-00aa004b2e24");
public static final GUID IID_IWbemLocator = new GUID("dc12a687-737f-11cf-884d-00aa004b2e24");

public IWbemLocator() {
}

private IWbemLocator(Pointer pvInstance) {
super(pvInstance);
}

public static IWbemLocator create() {
PointerByReference pbr = new PointerByReference();

HRESULT hres = Ole32.INSTANCE.CoCreateInstance(CLSID_WbemLocator, null, WTypes.CLSCTX_INPROC_SERVER,
IID_IWbemLocator, pbr);
if (COMUtils.FAILED(hres)) {
return null;
}

return new IWbemLocator(pbr.getValue());
}

public HRESULT ConnectServer(BSTR strNetworkResource, BSTR strUser, BSTR strPassword, BSTR strLocale,
int lSecurityFlags, BSTR strAuthority, IWbemContext pCtx, PointerByReference ppNamespace) {
// ConnectServier is 4th method of IWbemLocatorVtbl in WbemCli.h
return (HRESULT) _invokeNativeObject(3, new Object[] { getPointer(), strNetworkResource, strUser,
strPassword, strLocale, lSecurityFlags, strAuthority, pCtx, ppNamespace }, HRESULT.class);
}

public IWbemServices ConnectServer(String strNetworkResource, String strUser, String strPassword,
String strLocale, int lSecurityFlags, String strAuthority, IWbemContext pCtx) {
BSTR strNetworkResourceBSTR = OleAuto.INSTANCE.SysAllocString(strNetworkResource);
BSTR strUserBSTR = OleAuto.INSTANCE.SysAllocString(strUser);
BSTR strPasswordBSTR = OleAuto.INSTANCE.SysAllocString(strPassword);
BSTR strLocaleBSTR = OleAuto.INSTANCE.SysAllocString(strLocale);
BSTR strAuthorityBSTR = OleAuto.INSTANCE.SysAllocString(strAuthority);

PointerByReference pbr = new PointerByReference();

try {
HRESULT result = ConnectServer(strNetworkResourceBSTR, strUserBSTR, strPasswordBSTR, strLocaleBSTR,
lSecurityFlags, strAuthorityBSTR, pCtx, pbr);

COMUtils.checkRC(result);

return new IWbemServices(pbr.getValue());
} finally {
OleAuto.INSTANCE.SysFreeString(strNetworkResourceBSTR);
OleAuto.INSTANCE.SysFreeString(strUserBSTR);
OleAuto.INSTANCE.SysFreeString(strPasswordBSTR);
OleAuto.INSTANCE.SysFreeString(strLocaleBSTR);
OleAuto.INSTANCE.SysFreeString(strAuthorityBSTR);
}
}
}

/**
* Used by clients and providers to access WMI services. The interface is
* implemented by WMI and WMI providers, and is the primary WMI interface.
*/
class IWbemServices extends Unknown {

public IWbemServices() {
}

public IWbemServices(Pointer pvInstance) {
super(pvInstance);
}

public HRESULT ExecQuery(BSTR strQueryLanguage, BSTR strQuery, int lFlags, Pointer pCtx,
PointerByReference ppEnum) {
// ExecQuery is 21st method of IWbemServicesVtbl in WbemCli.h
return (HRESULT) _invokeNativeObject(20,
new Object[] { getPointer(), strQueryLanguage, strQuery, lFlags, pCtx, ppEnum }, HRESULT.class);
}

public IEnumWbemClassObject ExecQuery(String strQueryLanguage, String strQuery, int lFlags, Pointer pCtx) {
BSTR strQueryLanguageBSTR = OleAuto.INSTANCE.SysAllocString(strQueryLanguage);
BSTR strQueryBSTR = OleAuto.INSTANCE.SysAllocString(strQuery);
try {
PointerByReference pbr = new PointerByReference();

HRESULT res = ExecQuery(strQueryLanguageBSTR, strQueryBSTR, lFlags, pCtx, pbr);

COMUtils.checkRC(res);

return new IEnumWbemClassObject(pbr.getValue());
} finally {
OleAuto.INSTANCE.SysFreeString(strQueryLanguageBSTR);
OleAuto.INSTANCE.SysFreeString(strQueryBSTR);
}
}
}

/**
* Optionally used to communicate additional context information to
* providers when submitting IWbemServices calls to WMI
*/
class IWbemContext extends Unknown {

public IWbemContext() {
}

public IWbemContext(Pointer pvInstance) {
super(pvInstance);
}
}
}
Loading