function val = winqueryreg(varargin) % winqueryreg queries Microsoft Windows registry. % % value = winqueryreg(rootkey, subkey, valName) % returns the value of the specified key. % % value = winqueryreg(rootkey, subkey) % returns value that has no value name property. % % valNames = winqueryreg('name', rootkey, subkey) % returns all value names in rootkey\subkey in a cell array. % Xiangrui Li (address@hidden) % 20141211 wrote it for Octave narginchk(2, 3); if strcmp(varargin{1}, 'name') % case sensitive match for 'name' narginchk(3, 3); rootkey = full_rootkey(varargin{2}); key = [rootkey '\' varargin{3}]; % "/v /f *" trick makes it list only valueNames [err, str] = system(['reg query "' key '" /v /f * 2>&1']); % with /F, str contains "End of search: ? match(es) found." key1 = 'End of search:'; ind = strfind(str, key1); if err && isempty(ind) % like reg command missing or wrong syntax error('winqueryreg:REG', str); end n = sscanf(str(ind+length(key1):end), '%*c%g', 1); if n == 0 % no matches found val = cell(0,1); return; end str(ind:end) = ''; % remove "End of search" line i = strfind(lower(str), lower(key)); str(1:i(1)+length(key)) = []; % remove key from output del = char(10); % try '\n' first if isempty(strfind(str, del)), del = char(13); end % try '\r' next str = strsplit(str, del); val = {}; for i = 1:length(str) a = strtok(str{i}); if isempty(a), continue; end % in case of blank line val{end+1, 1} = a; %#ok end return; % done with 'name' as first argument end rootkey = full_rootkey(varargin{1}); key = [rootkey '\' varargin{2}]; if nargin > 2 % name specified name = varargin{3}; [err, str] = system(['reg query "' key '" /v "' name '" 2>&1']); if err error('winqueryreg:REG', str); end else % almost useless, Matlab winqueryreg seems problematic for this [err, str] = system(['reg query "' key '" /ve 2>&1']); if err error('winqueryreg:REG', str); end name = '(Default)'; i = strfind(lower(str), lower(name)); if isempty(i) val = ''; return; end end i = strfind(lower(str), lower(key)); % if isempty(i) % not needed since we take care of short rootkey like HKLM % error('winqueryreg:queryerror', 'Specified key is invalid.'); % end str(1:i(1)+length(key)) = []; % remove key from output i = strfind(lower(str), lower(name)); str(1:i(1)+length(name)) = []; % remove name % now str is like "REG_SZ 0x1" [typ, val] = strtok(strtrim(str)); val = strtrim(val); if strcmpi(val, '(value not set)'), val = ''; end switch typ case 'REG_SZ' return; case 'REG_EXPAND_SZ' a = strtok(val, '%'); val = strrep(val, ['%' a '%'], getenv(a)); case 'REG_DWORD' if strncmp(val, '0x', 2) % maybe always hex type? val = sscanf(val, '0x%x'); else val = str2num(val); %#ok str2double('') is NaN end val = int32(val); otherwise % REG_MULTI_SZ REG_QWORD REG_BINARY REG_NONE etc % This could be a warning, since val is available to user. % Now simply copy Matlab behavior. error('winqueryreg:queryerror', ... 'Cannot query value of type %s.', typ); end % In case rootkey in short form like HKLM | HKCU | HKCR | HKU | HKCC. function rootkey = full_rootkey(in) % rootkey = in; return; % Matlab behavior switch in case 'HKLM' rootkey = 'HKEY_LOCAL_MACHINE'; case 'HKCU' rootkey = 'HKEY_CURRENT_USER'; case 'HKCR' rootkey = 'HKEY_CLASSES_ROOT'; case 'HKU' rootkey = 'HKEY_USERS'; case 'HKCC' rootkey = 'HKEY_CURRENT_CONFIG'; otherwise rootkey = in; end