|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: Nürnberg, Germany
|
Posted: Wed Feb 02, 2011 10:41 am Post subject: RegOpenKey,HKEY_LOCAL_MACHINE,HKEY,Registry |
|
|
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
Code: | 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 |
|
|
Back to top |
|
|
wahorger
Joined: 13 Oct 2014 Posts: 1217 Location: Morrison, CO, USA
|
Posted: Thu May 21, 2015 3:13 am Post subject: |
|
|
Many many thanks!
I was looking for a solution and this is working perfectly! Thanks for sharing the code with us! |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Fri May 29, 2015 12:36 am Post subject: |
|
|
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. Code: | Program Get_Processor
character processor_Name*256
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 ? |
|
Back to top |
|
|
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2388 Location: Yateley, Hants, UK
|
Posted: Fri May 29, 2015 12:14 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1886
|
Posted: Fri May 29, 2015 1:26 pm Post subject: |
|
|
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
Code: | reg query hklm\HARDWARE\DESCRIPTION\System\CentralProcessor\0 /t REG_SZ |
and
Code: | reg query hklm\HARDWARE\DESCRIPTION\System\CentralProcessor\0 /v Identifier /t REG_SZ |
|
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Sat May 30, 2015 1:48 am Post subject: |
|
|
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 |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Sat May 30, 2015 7:00 am Post subject: |
|
|
It might be worth looking at GetPrivateProfileString etc. These functions are simpler to use and provide a mapping into the registry. |
|
Back to top |
|
|
wahorger
Joined: 13 Oct 2014 Posts: 1217 Location: Morrison, CO, USA
|
Posted: Sat May 30, 2015 5:40 pm Post subject: |
|
|
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! |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Sun May 31, 2015 8:38 am Post subject: |
|
|
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. |
|
Back to top |
|
|
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2388 Location: Yateley, Hants, UK
|
Posted: Sun May 31, 2015 11:38 am Post subject: |
|
|
Paul,
I presume that your 'no longer' relates to Windows 8 et seq. ?
Eddie |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Sun May 31, 2015 11:56 am Post subject: |
|
|
It was probably Windows 7 or maybe Windows Vista. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|