3

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

naoru
  • 2,149
  • 5
  • 34
  • 58
  • Is there a reason why you're using java and not .NET? According to the Web site, Jacob updates finished in 2004. Is it still relevant? A simple Google search for the terns _java read remote windows registry_ turned up several ways to achieve what I think you are trying to achieve **without** Jacob. – Abra Sep 03 '19 at 17:33
  • @abra - what other alternatives can you propose, i need to query registry data for hundreds of computers from a central server, do you have a WMI alternative? – naoru Sep 03 '19 at 21:22
  • I didn't suggest **not** using WMI, I asked why you aren't using .NET to query the Windows registry remotely. – Abra Sep 04 '19 at 08:48
  • @Abra all our code base and devs are java – naoru Sep 04 '19 at 11:11
  • What about using [ProcessBuilder](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) to run a [PowerShell](https://learn.microsoft.com/en-us/powershell/) script? Or [JNA](https://github.com/java-native-access/jna) to call .NET code? – Abra Sep 04 '19 at 18:05
  • @Abra i can do it with power shell but since its a a process that will runs thusands of times per day over many computers i prefer to do it directly with my java code and not open processes. power shell will be last resort – naoru Sep 05 '19 at 00:50
  • And my other suggestion: Launch a .NET, e.g. C#, program from java. One process. You provided a link to `StdRegProv` in your question. It is part of C#. You could even make your [C#] program a Windows Service. – Abra Sep 05 '19 at 01:55

1 Answers1

0

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();
        } 
naoru
  • 2,149
  • 5
  • 34
  • 58