How to get specific registry value in sikuli

Asked by pipboy

sorry for question again,

i have to get specific registry value from windows registry

and output to a *.txt file after value were returned successfully

those values are like product version,release day ..etc.

could you advise me how to get it ? thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

You can use the Windows command
reg query
to read your reg values.

import os
ret = os.popen("reg query /?").readlines() # run comand
# now ret contains the lines that are returned on stdout
for line in ret:
     print line.strip() # print each line
# strip removes leading/trailing whitespace

Play a little bit around with it by configuring the reg query parameters accordingly.
If the command needs " apostrophes, write them as \" in the command string of popen.

When you found out, what you get returned, you are left with extracting the things you need from the returned lines.

come back with real values and output if you need help.

Revision history for this message
RaiMan (raimund-hocke) said :
#2

ok, want to write them to a file:

out = file("path-to-your-outputfile", "w")
for line in ret:
    out.write(line)
out.close()

Revision history for this message
pipboy (peace03) said :
#3

hi,RaiMan,thanks for your fast reply.

Windows cmd:
c:\>REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0003" /f DriverVersion

result :
DriverVersion REG_SZ 6.1.7600.16385 #real values i need

------------------------------------------------------------------------------------------
 I tried to do the same thing at Sikuli,

import os

ret = os.popen('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0003" /f version').readlines()

for line in ret:
     print line.strip()

result :
....
Sound, video and game controllers
 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0000
...0001,0002
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0010

 I tried a variety of print() format/command
but still can't find DriverVersion in "{..318}\0003"

Revision history for this message
RaiMan (raimund-hocke) said :
#4

In command line you say:
/f DriverVersion

In Sikuli
/f version

??????????????????

Revision history for this message
pipboy (peace03) said :
#5

It's my neglect in typing,sorry.

import os

ret = os.popen('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0003" /f DriverVersion').readlines()

for line in ret:
     print line.strip()

result : as above

Revision history for this message
pipboy (peace03) said :
#6

I guess I may not describe clearly

In command line:
/f DriverVersion
result : 6.1.7600.16385 #real values i need

In Sikuli:
/f DriverVersion
result : Sound, video and game controllers....{..318}\000~{..318}\0010

Ps. Does _winreg function support in sikuli?

Revision history for this message
Best RaiMan (raimund-hocke) said :
#7

--- _winreg
Is not supported in Sikuli (not available in Jython, since it is a C-based module in Python, that are normally not available as ported JNI-modules in Jython).

If you know how to program in Java, you might use java.util.prefs.Preferences (look: http://alexradzin.blogspot.com/2011/01/access-windows-registry-with-pure-java.html), but it is restricted o string values.

--- your problem:
since the command contains \'s, these have either to be doubled or the string has to be a raw string r"some text with \ inside" (I prefer this):

ret = os.popen('reg query r"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E96C-E325-11CE-BFC1-08002BE10318}\0003" /f DriverVersion').readlines()

Revision history for this message
pipboy (peace03) said :
#8

Thanks RaiMan, that solved my question.

Revision history for this message
sdraganov (sdraganov) said :
#9

Does this still works? I am getting the right output from cmd but it gives me the following error from sikuli:
ERROR: The system was unable to find the specified registry key or value.

I'm on Microsoft Windows [Version 10.0.16299.431] , SikuliXIDE 1.1.2, Java Version 8 Update 171.
Here what I've tried:

In cmd:
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\V-Ray for 3dsmax 2014 for x64" /f DisplayVersion

Output:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\V-Ray for 3dsmax 2014 for x64
    DisplayVersion REG_SZ 3.60.04

In Sikuli(#1 - raw string):
ret = os.popen('reg query r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\V-Ray for 3dsmax 2014 for x64" /f DisplayVersion').readlines()
for line in ret:
     print line.strip()

Output:
ERROR: Invalid key name.
Type "REG QUERY /?" for usage.

In Sikuli(#2 - escaping \'s):
ret = os.popen('reg query "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\V-Ray for 3dsmax 2014 for x64" /f DisplayVersion').readlines()
for line in ret:
     print line.strip() # print each line

Output:
ERROR: The system was unable to find the specified registry key or value.

Any idea what's wrong?
Thanks in advance.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

@sdraganov
Sorry, but my raw string solution in comment #7 was bullshit, since the r"" is not at the Python level.
So I do not know, how the guy solved his problem.

Today my try would look so:
hkey = r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\V-Ray for 3dsmax 2014 for x64"
ret = os.popen('reg query "' + hkey + '" /f DisplayVersion').readlines()

Another option is to use subprocess.Popen() or subprocess.call(), since it is recommended instead of os.popen() since Python 2.6 (actual SikuliX is at Python language level 2.1).
see: http://www.pythonforbeginners.com/os/subprocess-for-system-administrators