**************************************************
*-- Class: kregistry (h:\systemcentre\intapps\pt_speed_awareness\development\vfp8\class\wcc_tools.vcx)
*-- ParentClass: custom
*-- BaseClass: custom
*-- Time Stamp: 02/16/01 01:31:01 PM
*
#INCLUDE "e:\vfp6\course\registry.h"
*
DEFINE CLASS kregistry AS custom
Height = 17
Width = 27
*-- a handle to the currently open key
PROTECTED hkeycurrent
hkeycurrent = 0
*-- contains the last error number returned by the registry functions
nlasterror = 0
Name = "kregistry"
multi = .F.
continue = .F.
*-- opens, gets value, closes a key
PROCEDURE getvalue
lparameters thkeyRoot, tcSubKey, ;
tcValueName, tcDefaultValue
local lcReturnValue
lcReturnValue = ""
THIS.OpenKey( thkeyRoot, tcSubKey )
lcReturnValue = THIS.ReadValue( ;
m.tcValueName, m.tcDefaultValue )
THIS.CloseKey()
return m.lcReturnValue
ENDPROC
*-- opens, sets value, closes a key
PROCEDURE setvalue
lparameters thkeyRoot, tcSubKey, ;
tcValueName, tcValueData
THIS.OpenKey( m.thkeyRoot, m.tcSubKey )
THIS.WriteValue( m.tcValueName, m.tcValueData )
THIS.CloseKey()
ENDPROC
*-- opens key, deletes value, closes key
PROCEDURE deletevalue
lparameters thkeyRoot, tcSubKey, tcValueName
local lhKey, lnResult
lnResult = 0
lhKey = THIS.GetKeyHandle( m.thkeyRoot, ;
m.tcSubKey )
*// Delete the value:
lnResult = RegDeleteValue( m.lhKey, ;
m.tcValueName )
THIS.CheckResult( m.lnResult, 'RegDeleteValue()')
*// Close the key:
THIS.CloseKey( m.lhKey )
return (m.lnResult=ERROR_SUCCESS)
ENDPROC
*-- Creates a registry key
PROCEDURE createkey
lparameters thkeyRoot, tcSubKey
local lhKey
lhKey = THIS.GetKeyHandle( m.thkeyRoot, ;
m.tcSubKey )
*// Close the key
THIS.CloseKey( m.lhKey )
ENDPROC
*-- deletes a registry key. Key must be empty
PROCEDURE deletekey
lparameters thkeyRoot, tcParentKey, tcKeyToDelete
local lhKey, lnResult
lhKey = THIS.GetKeyHandle( m.thkeyRoot, ;
m.tcParentKey)
*// Delete the key:
lnResult = RegDeleteKey( m.lhKey, ;
m.tcKeyToDelete )
THIS.CheckResult( m.lnResult, 'RegDeleteKey()')
*// Close the key:
THIS.CloseKey( m.lhKey )
return (m.lnResult=ERROR_SUCCESS)
ENDPROC
*-- opens a key and places handle in THIS.hkeyCurrent
PROCEDURE openkey
lparameters thkeyRoot, tcSubKey
*// Opens the requested key, and stores it
*// in the member variable .hkeyCurrent
local lhkey
lhkey = 0
if THIS.hkeyCurrent <> 0
*// Close the key:
THIS.CloseKey( THIS.hkeyCurrent )
endif
lhKey = THIS.GetKeyHandle( m.thkeyRoot, ;
m.tcSubKey )
THIS.hkeyCurrent = lhKey
ENDPROC
*-- reads a value from the currently open key
PROCEDURE readvalue
lparameters tcValueName, tcDefaultValue
*// Ensure that the key is currently open:
if THIS.hkeyCurrent = 0
return ""
endif
*// Check for non-string default value
if type( 'm.tcDefaultValue' ) <> 'C'
tcDefaultValue = ""
endif
*// Initialise variables:
local lcReturnValue, lnType, lnResult, ;
lcBuffer, lnBufferSize
store "" to lcReturnValue
store 0 to lnType, lnResult
store space(256) to lcBuffer
store len(lcBuffer) to lnBuffersize
*// Query the value of the key:
lnResult = RegQueryValueEx( ;
THIS.hkeyCurrent, ;
m.tcValueName, ;
REG_OPTION_RESERVED, ;
@lnType, ;
@lcBuffer, ;
@lnBufferSize )
if m.lnResult = ERROR_SUCCESS
*// Clean up the return value:
lcReturnValue = strtran( left( m.lcBuffer, ;
m.lnBufferSize), CHR(0), "")
else
*// Create the value with the default value:
THIS.WriteValue( tcValueName, tcDefaultValue )
lcReturnValue = m.tcDefaultValue
endif
return m.lcReturnValue
ENDPROC
*-- writes / creates a value in the currently open key
PROCEDURE writevalue
lparameters tcValueName, tcValueData
local lnResult
lnResult = 0
lnResult = RegSetValueEx( ;
THIS.hkeyCurrent, ;
m.tcValueName, ;
REG_OPTION_RESERVED, ;
REG_SZ, ;
m.tcValueData + CHR(0), ;
len(m.tcValueData) )
THIS.CheckResult( m.lnResult, 'RegSetValueEx()')
return (m.lnResult=ERROR_SUCCESS)
ENDPROC
*-- Closes the currently open key
PROCEDURE closekey
lparameters thkey
*// We must support implicit closing of the
*// current key THIS.hkeyCurrent, or a specific
*// key passed in via the parameter:
local lhkey
if pcount() = 1
lhkey = m.thkey
else
lhkey = THIS.hkeyCurrent
endif
*// Close the key:
lnResult = RegCloseKey( lhkey )
THIS.CheckResult( m.lnResult, 'RegCloseKey()')
if pcount() = 0
*// Clear our copy of the handle:
THIS.hkeyCurrent = 0
endif
return (m.lnResult=ERROR_SUCCESS)
ENDPROC
*-- returns a handle to the requested key
PROCEDURE getkeyhandle
lparameters thkeyRoot, tcSubKey
local lhKey, lnDisposition, lnResult
store 0 to lhKey, lnDisposition
lnResult = RegCreateKeyEx( ;
m.thkeyRoot, ;
m.tcSubKey, ;
REG_OPTION_RESERVED, ;
REG_CLASS_DEFAULT, ;
REG_OPTION_NON_VOLATILE, ;
KEY_ALL_ACCESS, ;
REG_SECURITY_DEFAULT, ;
@lhKey, ;
@lnDisposition )
*// Currently lnDisposition is not used, but
*// it will either be REG_OPENED_EXISTING_KEY
*// or REG_CREATED_NEW_KEY
THIS.CheckResult( m.lnResult, 'RegCreateKeyEx()')
return m.lhKey
ENDPROC
*-- checks the returned code from REgxxx() calls against ERROR_SUCCESS
PROTECTED PROCEDURE checkresult
lparameters tnResult, tcModule
if m.tnResult <> ERROR_SUCCESS
if _debug
=MessageBox("Error "+ ;
alltrim(str(m.tnResult))+ ;
" was returned from " + ;
m.tcModule +".")
else
THIS.nLastError = m.tnResult
endif
endif
ENDPROC
PROCEDURE Init
declare integer RegCreateKeyEx in Win32API ;
integer nhKey, ;
string @cSubKey, ;
integer nReserved, ;
string cKeyClass, ;
integer nOptions, ;
integer nSecurityAccessMask, ;
integer nSecurityAttributes, ;
integer @nKeyHandle, ;
integer @nDisposition
declare integer RegSetValueEx in Win32API ;
integer nKeyHandle, ;
string cValueName, ;
integer nReserved, ;
integer nType, ;
string cBuffer, ;
integer nBufferSize
declare integer RegQueryValueEx in Win32API ;
integer nhKey, ;
string cValueName, ;
integer nReserved, ;
integer @nType, ;
string @cBuffer, ;
integer @nBufferSize
declare integer RegCloseKey in Win32API ;
integer nKeyHandle
declare integer RegDeleteKey in Win32API ;
integer nKeyHandle, ;
string cSubKey
declare integer RegDeleteValue in Win32API ;
integer nKeyHandle, ;
string cValueName
ENDPROC
PROCEDURE Destroy
if THIS.hkeyCurrent <> 0
*// Close the key:
THIS.CloseKey( THIS.hkeyCurrent )
endif
ENDPROC
ENDDEFINE
*
*-- EndDefine: kregistry
**************************************************