Read registry

Read registry

I like to get a sample Visual Fortran code to read an entry in the Registry. Could anyone suply me with example code ?

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I wrote this routine to set an environment variable.
Shows you all the elements. If you just want to read things, you'll use RegOpenKeyEx followed by RegQueryValueEx
No guarantees!!!

Tim
===============================
subroutine setEnvVar(valName,valData,ierr)
use dfwin
implicit none
character*(*),intent(inout)::valName
character*(*),intent(inout)::valData
integer(2), intent(out):: ierr
character(100) subKey
integer(LONG) regCallStat, hNewOrOld, mainKey
integer(LONG) hKey , valLen,valType
integer i

ierr=-1
! use these for the current user environment
! "For the system level env variables there are 2 keys to set. Only different in their subKey"
! MSsupp says it is NOT necessary to change the "ControleSetxxx" as windows will write this on its own.
! hence the loop below could be disabled
mainKey= HKEY_LOCAL_MACHINE
subkey='SYSTEMCurrentControlSetControlSession ManagerEnvironment'C

valName=trim(valName)//char(0)
valLen=len_trim(valData)
valData=valData(:valLen)//char(0)
valType=REG_SZ

do i=1,2
regCallStat=RegCreateKeyEx(mainKey, loc(subKey), 0,'REG_SZ'C, &
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, loc(hKey), &
loc(hNewOrOld))
if(regCallStat)return

regCallStat=RegSetValueEx(hKey, valName, 0, valType, loc(valData), valLen)
if(regCallStat)ierr=-1

regCallStat=RegCloseKey(hKey)
if(regCallStat)return
subKey='SYSTEMControlSet001ControlSession ManagerEnvironment'C !first subkey
! l=len_trim(subKey)+1
! subKey(l:l)=char(0)
enddo
ierr=0
end subroutine setEnvVar

I tried the code below but it fails.

c I wrote this routine to set an environment variable.
c Shows you all the elements. If you just want to read things,
c you'll use RegOpenKeyEx followed by RegQueryValueEx
c No guarantees!!!

c
c ===============================
subroutine ReadRegVar(valName,valData,ierr)
use dfwin
implicit none
character*(*),intent(inout)::valName
character*(*),intent(inout)::valData
integer(2), intent(out):: ierr
character(100) subKey
integer(LONG) regCallStat, hNewOrOld, mainKey
integer(LONG) hKey , valLen,valType
integer i

ierr=-1
! use these for the current user environment
! "For the system level env variables there are 2 keys to set. Only different in their subKey"
! MSsupp says it is NOT necessary to change the "ControleSetxxx" as windows will write this on its own.
! hence the loop below could be disabled
mainKey= HKEY_LOCAL_MACHINE
subkey='SOFTWARESysdecoGINIS VisionPath'S

valName=trim(valName)//char(0)
valLen=len_trim(valData)
valData=valData(:valLen)//char(0)
valType=REG_SZ
regCallStat=RegOpenKeyEx(mainKey,subKey,0, KEY_ALL_ACCESS,
+ loc(hKey))

ierr=0
end subroutine ReadRegVar

The return value from RegOpenKeyEx is 2.
Please help

Jugoslav Dujic's picture

Error 2 is ERROR_FILE_NOT_FOUND, i.e. non-existing path. See "System errors - Numerical Order" help topic.

subkey='SOFTWARESysdecoGINIS VisionPath'S

What's the S on the end? It's supposed to be C? Besides, "Path" looks suspicious to me -- is it a key ("folder") or a value ("file")? If the latter, you have to use RegQueryValue[Ex] on it after.

Jugoslav

Jugoslav www.xeffort.com
Jugoslav Dujic's picture

Additional side note: do not use KEY_ALL_ACCESS on HKEY_LOCAL_MACHINE (HKLM). On most NT systems with decent security, HKLM is read-only for all users but administrators; it should be written by setup program and just read by applications. If you want just to read from it, use KEY_QUERY_VALUE access; or use HKEY_CURRENT_USER instead. (But that's not your current problem, since you would have got ERROR_ACCESS_DENIED=5).

Jugoslav www.xeffort.com

Finally I got it to work. I attach the code.

subroutine ReadRegVar(skey,valData,valname,mode,ll,ierr)
use dfwin
implicit none
character*(*),intent(inout)::skey
character*(*),intent(inout)::valdata
character*(*),intent(inout)::valname
integer*4, intent(inout):: mode
integer*4, intent(out):: ll
integer*4, intent(out):: ierr
character*255 subKey, vname, strx
integer*4 regCallStat, mainKey, hKey, l1, l2

ierr = 0
if (mode .eq. 1) then
mainKey= HKEY_LOCAL_MACHINE
else
ierr = -2
return
endif
subkey = skey

regCallStat=RegOpenKeyEx(mainKey,subKey,0, KEY_QUERY_VALUE,
+ loc(hKey))
if (regCallStat .ne. 0) then
ierr = regCallStat
return
endif
vname = valname ! value name
l1 = 0
l2 = 256
regCallStat = RegQueryValueEx (hKey, vname, 0, loc(l1),
+ loc(strx),loc(l2))
if (regCallStat .ne. 0) then
ierr = regCallStat
return
endif
ll = l2 - 1 ! strip away null termination
valdata = strx(1:ll)
end subroutine ReadRegVar

Many tanks

Login to leave a comment.