Can I read the Windows Registry from within elisp? How?
I just wanna do something like this
(defun my-fun (reg-path)
"reads the value from the given Windows registry path."
...??...
)
is there a built-in fn that does this?
or is there a command-line tool builtin to windows that I can run to retrieve a reg value?
The way I am imagining doing it, is to run a .js file in cscript.exe that does the work.
ANSWER
(defun my-reg-read (regpath)
"read a path in the Windows registry. This probably works for string
values only. If the path does not exist,开发者_开发技巧 it returns nil. "
(let ((reg.exe (concat (getenv "windir") "\\system32\\reg.exe"))
tokens last-token)
(setq reg-value (shell-command-to-string (concat reg.exe " query " regpath))
tokens (split-string reg-value nil t)
last-token (nth (1- (length tokens)) tokens))
(and (not (string= last-token "value.")) last-token)))
==> Thank you to Oleg.
Use reg command line utility.
Emacs command
(shell-command "REG QUERY KeyName" &optional OUTPUT-BUFFER ERROR-BUFFER)
allows you to run a shell command. The output is sent to the OUTPUT-BUFFER
.
Here's what I did:
(defun my-reg-read (regpath)
"read a path in the Windows registry"
(let ((temp-f (make-temp-file "regread_" nil ".js"))
(js-code "var WSHShell, value, regpath = '';try{ if (WScript.Arguments.length > 0){regpath = WScript.Arguments(0); WSHShell = WScript.CreateObject('WScript.Shell'); value = WSHShell.RegRead(regpath); WScript.Echo(value); }}catch (e1){ WScript.Echo('error reading registry: ' + e1);}")
reg-value)
(with-temp-file temp-f (insert js-code))
(setq reg-value (shell-command-to-string (concat temp-f " " regpath)))
(delete-file temp-f)
reg-value ))
The elisp function creates a temporary file, then writes a bit of javascript logic into it. the javascript reads the windows registry for a given path. The elisp fn then runs the temporary file, passing the registry path to read. It deletes the file, then returns the result of running it.
精彩评论