6 Replies Latest reply: May 28, 2013 5:02 PM by jschellSomeoneStoleMyAlias RSS

    Read registry value with ProcessBuilder

    932609
      Hi all,

      I am using the following to read the ProductID from the registry (64bit windows):

      -----------

      List<String> command = new ArrayList<String>();
      command.add("\"C:\\Users\\User1\\reg64.exe\"");
      command.add("query");
      command.add("\"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\"");
      command.add("/v");
      command.add("ProductID");
      command.add("/reg:64");
      productID = execute(command);

      ----------

      static private String execute(List<String> command) throws IOException, InterruptedException {
      ProcessBuilder pb = new ProcessBuilder(command);
      Process process = pb.start();
      StreamReader reader = new StreamReader(process.getInputStream());
      StreamReader errReader = new StreamReader(process.getErrorStream());
      reader.start();
      errReader.start();
      process.waitFor();
      reader.join();
      errReader.join();
      return reader.getResult();
      }

      -------------------------------------------------

      This should basically produce the following query

      "C:\Users\User1\reg64.exe" query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v ProductID /reg:64

      When this query is run directly in the command prompt it yields the desired result:
      HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion
      ProductID REG_SZ 0032-0RM-89989898989-12334

      However, the result via the processBuilder is an empty string.

      If I try the processBuilder with the following:
      command.add("reg"); instead of command.add("\"C:\\Users\\User1\\reg64.exe\"");
      then the result is non-empty: the same as from the command prompt. However, then the ProductID is missing.

      I tried the following variants of the argument:
      - with and without the full path to reg64.exe
      - with and without quotation marks around the path
      - with and without setting the working directory for the processBuilder

      but I was always getting an empty string as a result of the query.

      Any fresh ideas would be appreciated :)
        • 1. Re: Read registry value with ProcessBuilder
          EJP
          Have you tried reading the error output? Maybe the output goes to stderr, not stdout.
          • 2. Re: Read registry value with ProcessBuilder
            sabre150
            I have no ability to test this but in effect each element of the argument list applied to ProcessBuilder is already quoted so you should not need to add another quote. Therefore I would expect to see
            List<String> command = new ArrayList<String>();
            command.add("C:\\Users\\User1\\reg64.exe");
            command.add("query");
            command.add("HKLM\\Software\\Microsoft Windows NT\\CurrentVersion");
            command.add("/v");
            command.add("ProductID");
            command.add("/reg:64");
            I would also print the Process exit code and the content of both the stderr and stdout.
            • 3. Re: Read registry value with ProcessBuilder
              932609
              Thanks for the quick replies!

              When trying with the reg32.exe the error stream is non-empty indeed: ERROR: The system was unable to find the specified registry key or value. (The same error appears when trying the query from the command prompt)

              However, when trying with the reg64.exe the error stream is empty even when I supply wrong keys in the query...
              As I said, using the reg64.exe from the command prompt retrieves the ProductID correctly.

              In both cases (reg32 and reg64) the exit code of the process is 1.
              It seems the quotations in the path do not matter: with or without them the results are the same.

              It is quite strange that the error and input streams of the process are empty.

              Any further ideas out there?

              Edited by: 929606 on 28-mei-2013 4:26
              • 4. Re: Read registry value with ProcessBuilder
                EJP
                When trying with the reg32.exe the error stream is non-empty indeed: ERROR: The system was unable to find the specified registry key or value. (The same error appears when trying the query from the command prompt)
                Did you consider trying it with a valid key? It's all pretty pointless until then.

                HKLM is definitely wrong. That's a common shorthand for something like HKEY_LOCAL_MACHINE.
                • 5. Re: Read registry value with ProcessBuilder
                  932609
                  EJP wrote:
                  When trying with the reg32.exe the error stream is non-empty indeed: ERROR: The system was unable to find the specified registry key or value. (The same error appears when trying the query from the command prompt)
                  Did you consider trying it with a valid key? It's all pretty pointless until then.

                  HKLM is definitely wrong. That's a common shorthand for something like HOST_KEY_LOCAL_MACHINE.
                  My idea was to make sure that I correctly check the error stream.

                  Now I tried with a valid key using the reg32.exe:
                  "C:\Users\User1\reg32.exe" "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\LanguagePack" /v OpenType
                  With the proces builder the input stream of the process contains the expected result. Also the exit code is 0.

                  Here is the query I try with the reg64.exe:
                  "C:\Users\User1\reg64.exe" "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\LanguagePack" /v OpenType /reg:64
                  With the process builder the input and error streams are empty and the exit code is 1.
                  • 6. Re: Read registry value with ProcessBuilder
                    jschellSomeoneStoleMyAlias
                    929606 wrote:
                    Here is the query I try with the reg64.exe:
                    "C:\Users\User1\reg64.exe" "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\LanguagePack" /v OpenType /reg:64
                    With the process builder the input and error streams are empty and the exit code is 1.
                    Post the code.