Silverfrost Forums

Welcome to our forums

RegOpenKey,HKEY_LOCAL_MACHINE,HKEY,Registry

2 Feb 2011 9:41 #7676

Recently I needed to read some information from the registry. Before seaching in older source code, I was sure I would find it in the forum. However, I did not - so here it is 😄

program test_win32api
  IMPLICIT NONE
  include 'win32api.ins'

  INTEGER,PARAMETER::HKEY_CLASSES_ROOT =Z'80000000'
  INTEGER,PARAMETER::HKEY_CURRENT_USER =Z'80000001'
  INTEGER,PARAMETER::HKEY_LOCAL_MACHINE=Z'80000002'
  INTEGER,PARAMETER::HKEY_USERS        =Z'80000003'
  INTEGER,PARAMETER::BUFSIZE=256
  INTEGER hKey,res,cbValue
  CHARACTER(LEN=BUFSIZE) InstallLocation
  CHARACTER(LEN=BUFSIZE), &
  PARAMETER::RegPath='Software\\Silverfrost\\Silverfrost FTN95\\6.00'

  cbValue = BUFSIZE
  res = RegOpenKey(HKEY_LOCAL_MACHINE,TRIM(RegPath),hKey)
  IF(res /= 0) STOP 'ERROR:Cannot open registry key' 
  res = RegQueryValueEx(hKey,'InstallLocation',CORE4(0),CORE4(0),InstallLocation,cbValue)
  IF(res /= 0)THEN
    PRINT*,'ERROR:Cannot read key'
  ELSE
    PRINT*,InstallLocation(1:LEN_TRIM(InstallLocation))
  ENDIF    
  res = RegCloseKey(hKey)
end test_win32api
21 May 2015 2:13 #16326

Many many thanks!

I was looking for a solution and this is working perfectly! Thanks for sharing the code with us!

28 May 2015 11:36 #16374

I have been provided with this example of similar use of the registry. It may be a useful addition to place with the example above. Program Get_Processor character processor_Name256 call get_processor_name ( processor_Name ) write (,*) trim ( processor_Name ) end Program Get_Processor

 subroutine get_processor_name ( processor_Name )
   character processor_name*(*)
!
   STDCALL RegOpenKey      'RegOpenKeyA'(VAL,STRING,REF):INTEGER
   STDCALL RegQueryValueEx 'RegQueryValueExA'(VAL,STRING,REF,REF,STRING,REF):INTEGER
   STDCALL RegCloseKey     'RegCloseKey'(VAL):INTEGER
!   
!   From the example I could get the desired result!
!   
!   What I did not know was the following:
!   80000000 -> HKEY_CLASSES_ROOT
!   80000001 -> HKEY_CURRENT_USER
!   80000002 -> HKEY_LOCAL_MACHINE
!   80000003 -> HKEY_USERS
!   
   INTEGER,PARAMETER :: HKEY_LOCAL_MACHINE = Z'80000002'
   INTEGER,PARAMETER :: BUFSIZE=256
   INTEGER hKey,res,cbValue
   CHARACTER(LEN=BUFSIZE) processorName
!
   cbValue = BUFSIZE
   res     = RegOpenKey      (HKEY_LOCAL_MACHINE,'HARDWARE\DESCRIPTION\System\CentralProcessor\0',hKey)
    IF (res /= 0) then
      processor_name = 'ERROR:Cannot open registry key' 
      return
    END IF
!
   res     = RegQueryValueEx (hKey,'ProcessorNameString',CORE4(0),CORE4(0),processorName,cbValue)
    IF (res /= 0) THEN
      processor_name = 'ERROR:Cannot read processor name'
    ELSE
      processor_name = processorName
    ENDIF 
!
   res     = RegCloseKey     (hKey)
!
 end subroutine get_processor_name

There is a lot of useful information in the registry and hopefully these examples may help others. Understanding what information exists in the registry is important, as I don't have a Silverfrost tree in HKEY_LOCAL_MACHINE for the first example to work. (My Plato3 uses HKEY_CURRENT_USER\Software\Salford Software.) I have been considering defining program configuration settings in the registry, which could be a good next step. It basically needs a few capabilities, such as put, get, create and delete. Does anyone have some documented examples plus guidelines for safe use ?

29 May 2015 11:14 #16375

My understanding is that the Registry was invented to solve a problem with a plethora of INI files - but you can create and read INI files with fairly standard Fortran facilities, and Clearwin+ even has a helper function, %ss. Accessing the Registry, and doing it safely, is much more involved.

%ss and the extensive support for the now abandoned helpfile format are hangovers from the norms of Windows when Clearwin+ was invented: Windows 3.1 if I remember correctly.

Eddie

29 May 2015 12:26 #16376

I realize that sometimes information from the registry needs to be queried and modified from inside a program, but most of the time I find the CLI program 'reg' sufficient. For example, to obtain the information listed in JohnCampbell's program, one can enter the commands

reg query hklm\HARDWARE\DESCRIPTION\System\CentralProcessor\0 /t REG_SZ

and

reg query hklm\HARDWARE\DESCRIPTION\System\CentralProcessor\0 /v Identifier /t REG_SZ
30 May 2015 12:48 #16383

After seeing how Plato uses the registry, it looks to be a good way of storing program settings in a standard and reliable way. It would certainly be an improvement from when Windows 7 stoped us using the .ini files in \windows. I am interested in seeing how easy it could be to create the settings tree and modify the values. It would be useful to have a library of routines to enable this, although I first need to understand what operations are needed to create the tree ( directory) then define the value type and store the values (file entries) The few examples I have seen recover text values from entries that are known to exist. Handling errors and creating values is the next important step. REG is an interesting alternative to the reporting program I have, although when I first obtained the program, I was probably more surprised to find out that all this information existed in the registry.

John

30 May 2015 6:00 #16384

It might be worth looking at GetPrivateProfileString etc. These functions are simpler to use and provide a mapping into the registry.

30 May 2015 4:40 #16386

My registry usage is for user identification for delivered SW.

The software deploy tool I use allows the user to enter some data, and also has the user input the specific product key and registration code for their copy of the SW.

The registry is updated as part of that. When/If the user upgrades the SW later on, these values are presented to the user (so they don't have to type them again). I use a couple of the reg keys on the splash screen at the start of the program, and internally to mark certain data as being 'owned' by this particular user.

I do not write, I only read the registry, and only once at the start of the program to minimize any overhead. If the read does not work (perhaps an illegal copy of the SW), I currently fill in the identifying information with a standard warning string. And, there are other options.....

As an aside, there are a few WIN32API calls I intend to check out (and use) to make the user's life easier. There is a LOT of good functions there!

Great product, Paul, et. al!

31 May 2015 7:38 #16389

Just to clarify, it is GetProfileString etc. that provide a mapping to the registry whilst GetPrivateProfileString etc. continue to use an ini file. ini files can no longer be written to the 'Program files' folder. If you write to this folder then the output is diverted to the equivalent 'roaming' folder.

31 May 2015 10:38 #16390

Paul,

I presume that your 'no longer' relates to Windows 8 et seq. ?

Eddie

31 May 2015 10:56 #16391

It was probably Windows 7 or maybe Windows Vista.

Please login to reply.