IDL has never written a public routine to manipulate Windows registry entries. However, just about anything that one can do in a DOS command window can be done in IDL as well via IDL's SPAWN procedure. I am not an expert in all the DOS command options that universally all Windows O.S.'s have to update the registry, but I note that the following algorithm is one universally available option:
1. SPAWN a "regedit" command to "export" from the registry to the file system the current settings of a given registry key. That will be stored on the file system in a '.reg' file.
2. Import the '.reg' file into an IDL string array. I would have thought this would be an easy task for READF, but there is something about the format of my exported '.reg's that IDL's READF does not like, so I invoke a workaround SPAWN call that works around my READF problem.
3. Find with IDL's STRMATCH the line with the attribute whose value you want to change.
4. Use typical IDL string processing routines to modify that line.
5. Use OPENW and PRINTF to overwrite the file exported from the Registry with the text updated in IDL. Even though basic READF did not like the Registry's '.reg' text format, the Registry had no problems with IDL's text format.
6. SPAWN another "regedit" call to merge the updated file with the Windows registry.
Below is an example of a procedure that I tested which changes the cursor blink rate on my machine. ***IF YOU RUN THIS PROCEDURE AS IS, REMEMBER TO RESTORE THE ORIGINAL VALUE BACK AGAIN WHEN YOU ARE DONE!*** This example will print the original value in the output log during your first test.
; File: 'change_user_cursor_blink_rate.pro'
; Syntax Example: CHANGE_USER_CURSOR_BLINK_RATE, 400
; Purpose: Demonstrate how in IDL to execute commands (via
; SPAWN) that can manipulate the Windows registry. This
; particular example modifies one attribute in a common Win
; O.S. key named [HKEY_CURRENT_USER\Control Panel\Desktop]
PRO change_user_cursor_blink_rate, newRate
; Build a file path appropriate for a temporary file
tempDir = getenv('TEMP')
regFile = filepath('temp.reg', ROOT_DIR=tempDir)
if file_test(regFile) then file_delete, regFile
; Create a '.reg' file by importing current key values from the reg
spawn, /HIDE, 'regedit /e ' + regFile + $
' "HKEY_CURRENT_USER\Control Panel\Desktop"'
; For some reason a simple IDL READF call will not read the format of
; of the '.reg' file correctly. The below SPAWN is an easy workaround.
spawn, /HIDE, 'type ' + regfile, regFileText
cursorBlinkRateKeyLine = $
where(strmatch(regFileText, '"CursorBlinkRate"*'), count)
if count eq 0 then return
; Parse the current line and overwrite it with a new value
lineElements = $
strsplit(regFileText[cursorBlinkRateKeyLine], '=', /EXTRACT)
print, 'Original Cursor Blank Rate = ' , lineElements[1]
lineElements[1] = '"' + strtrim(newRate, 2) + '"'
regFileText[cursorBlinkRateKeyLine] = strjoin(lineElements, '=')
openw, lun, regFile, /GET_LUN
printf, lun, regFileText, FORMAT='(A)'
free_lun, lun
; "Merge" this updated key with the registry
spawn, /HIDE, 'regedit /s ' + regFile
file_delete, regFile ; Clean up
END
James Jones
|