Issue when executing IWbemServices.ExecQuery when executing WMI using JNA

六眼飞鱼酱① 提交于 2019-12-11 02:46:43

问题


I am trying to write a Java program which uses JNA to execute WMI query to execute on remote machine (Provided username/password).

I am trying to port this WMI example. I have modified this code here for my testing purpose (directly provided username/password) and it works fine.

But, in my java code, when executing the query using ExecQuery, I am getting the error code 0x80070005.

    String machineName = "machine";
    String userNameWithDomain = "\\administrator";
    String pass = "Password";

    String namespace = "\\\\" + machineName + "\\ROOT\\CIMV2";

    BSTR username = OleAuto.INSTANCE.SysAllocString(userNameWithDomain);
    BSTR password = OleAuto.INSTANCE.SysAllocString(pass);

    BSTR WQL = OleAuto.INSTANCE.SysAllocString("WQL");

    HRESULT hres = null;
    hres = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
    switch (hres.intValue()) {
        case COMUtils.S_OK:
        case COMUtils.S_FALSE:
        case WinError.RPC_E_CHANGED_MODE:
            break;
        default:
            throw new Wbemcli.WbemcliException("Failed to initialize COM library.", hres.intValue());
    }

    hres = Ole32.INSTANCE.CoInitializeSecurity(null, -1, null, null, Ole32.RPC_C_AUTHN_LEVEL_DEFAULT, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, null, Ole32.EOAC_NONE, null);
    if (COMUtils.FAILED(hres) && hres.intValue() != WinError.RPC_E_TOO_LATE) {
        Ole32.INSTANCE.CoUninitialize();
        throw new Wbemcli.WbemcliException("Failed to initialize security.", hres.intValue());
    }

    PointerByReference pSvc = new PointerByReference();
    IWbemLocator loc = IWbemLocator.create();

    BSTR namespaceStr = OleAuto.INSTANCE.SysAllocString(namespace);
    hres = loc.ConnectServer(namespaceStr, username, password, null, 0, null, null, pSvc);
    OleAuto.INSTANCE.SysFreeString(namespaceStr);
    loc.Release();
    if (COMUtils.FAILED(hres)) {
        throw new Wbemcli.WbemcliException(String.format("Could not connect to namespace %s.", namespace), hres.intValue());
    }

    String user = userNameWithDomain.substring(userNameWithDomain.indexOf("\\") + 1);
    String domainName = userNameWithDomain.substring(0, userNameWithDomain.indexOf("\\"));
    COAUTHIDENTITY auth = COAUTHIDENTITY.newAuth(user, domainName, pass);

    Pointer pAuth = new Pointer(-1);
    Pointer.nativeValue(pAuth, -1);
    LPOLESTR COLE_DEFAULT_PRINCIPAL = new LPOLESTR(pAuth);
    hres = Ole32.INSTANCE.CoSetProxyBlanket(pSvc.getValue(), Ole32.RPC_C_AUTHN_DEFAULT, Ole32.RPC_C_AUTHZ_DEFAULT, COLE_DEFAULT_PRINCIPAL, Ole32.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, Ole32.RPC_C_IMP_LEVEL_IMPERSONATE, auth, Ole32.EOAC_NONE);
    if (COMUtils.FAILED(hres)) {
        new IWbemServices(pSvc.getValue()).Release();
        throw new Wbemcli.WbemcliException("Could not set proxy blanket.", hres.intValue());
    }
    IWbemServices svc = new IWbemServices(pSvc.getValue());

    String query = "SELECT BuildNumber,Caption,OSArchitecture,Version FROM Win32_OperatingSystem";

    PointerByReference pEnumerator = new PointerByReference();
    BSTR queryStr = OleAuto.INSTANCE.SysAllocString(query);
    hres = svc.ExecQuery(WQL, queryStr, Wbemcli.WBEM_FLAG_FORWARD_ONLY | Wbemcli.WBEM_FLAG_RETURN_IMMEDIATELY, null, pEnumerator);
    OleAuto.INSTANCE.SysFreeString(queryStr);
    if (COMUtils.FAILED(hres)) {
        svc.Release();
        throw new Wbemcli.WbemcliException(String.format("Query '%s' failed.", query), hres.intValue());
    }
    IEnumWbemClassObject obj = new IEnumWbemClassObject(pEnumerator.getValue());

    System.out.println("Done");

Error occurs at line

hres = svc.ExecQuery(WQL, queryStr, Wbemcli.WBEM_FLAG_FORWARD_ONLY | Wbemcli.WBEM_FLAG_RETURN_IMMEDIATELY, null, pEnumerator);

For the provided credentials, my version of C++ code is working fine. Also, I can use powershell & WMIC to retrieve the data. I have attached the entire java code here

I have used oshi library also.

Any help regarding this would be helpful. Thanks

来源:https://stackoverflow.com/questions/51941549/issue-when-executing-iwbemservices-execquery-when-executing-wmi-using-jna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!