Search code examples
javawmiwmicwmi-queryjacob

Read windows registry info from remote system using Jacob


Im trying to run some WMI queries using JACOB, and so far i've been successfull in getting the services and processes however i need to query the registry to see if a certain key is there

i've stummbled across this link

but i dont understand how to implement it

in order to query the services i've used the following code

ActiveXComponent wmi = null;
        wmi = new ActiveXComponent("WbemScripting.SWbemLocator"); <-- side question what is the WbemScripting...
variantParameters[0] = new Variant("localhost");
        variantParameters[1] = new Variant("root\\cimv2"); <-- what is this root?
String query = "Select ExitCode,Name,ProcessId,StartMode,State,Status from Win32_Service where State='Running' and Name='MSDTC'";
        Variant vCollection = wmiconnect
                .invoke("ExecQuery", new Variant(query));

is there a place with decent documentation for this? and how to implement queries on the registry?

Thanks

UPDATE

Im trying a new implementation where i try to call the StdRegProv

and i have the following code

        int HKEY_LOCAL_MACHINE = 0x80000002;
    String strKeyPath = "SYSTEM\\CurrentControlSet\\Services";
    String [] sNames = new String [5];
    ActiveXComponent wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
    // no connection parameters means to connect to the local machine
    Variant variantParameters[] = new Variant[4];
    variantParameters[0] = new Variant("192.168.1.2");
    variantParameters[1] = new Variant("root\\default");
    variantParameters[2] = new Variant("admin");
    variantParameters[3] = new Variant("pass");
    Dispatch services = wmi.invoke("ConnectServer", variantParameters).toDispatch();
    Dispatch oReg = Dispatch.call(services, "Get", "StdRegProv").toDispatch(); 

    Variant ret = Dispatch.call(oReg, "EnumKey", HKEY_LOCAL_MACHINE, strKeyPath, sNames); 
    System.out.println("EnumKey: HKEY_LOCAL_MACHINE\\"+strKeyPath+"="+ret);

I was hoping to get the sNames array filled with data but its just nulls


Solution

  • I was unable to do it with Jacob but succeeded using j-interop library

    here is the code that cost me so much suffering

    IJIAuthInfo authInfo = new JIDefaultAuthInfoImpl("remoteComputerIpAddress", "wmiUserName", "wmiUserPassword");
            IJIWinReg registry = null;
            try {
                registry = JIWinRegFactory.getSingleTon().getWinreg(authInfo, "remoteComputerIpAddress", true);
                JIPolicyHandle policyHandle = registry.winreg_OpenHKLM();
                JIPolicyHandle policyHandle2 = registry.winreg_OpenKey(policyHandle, "SOFTWARE\\wisemon",
                        IJIWinReg.KEY_ALL_ACCESS);
                // JIPolicyHandle policyHandle3 =
                // registry.winreg_OpenKey(policyHandle2,"wisemon",IJIWinReg.KEY_ALL_ACCESS);
                System.out.println("Printing first 1000 entries under HKEY_LOCAL_MACHINE\\BCD00000000...");
                for (int i = 0; i < 1; i++) {
                    // String[] values = registry.winreg_EnumKey(policyHandle3,i);
                    // Object[] values = registry.winreg_EnumValue(policyHandle3,i);
                    Object[] values = registry.winreg_QueryValue(policyHandle2, "name", 100);
                    Object[] values2 = registry.winreg_QueryValue(policyHandle2, "date", 100);
                    System.out.println(new String((byte[]) values[1]));
                    System.out.println(new String((byte[]) values2[1]));
                }
            } catch (UnknownHostException | JIException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                System.out.println("Closing registry connection");
                registry.closeConnection();
            }