Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read the Windows Registry

API Functions

Read the Windows Registry

by  jimoo  Posted    (Edited  )
Here is the API code to read the Windows Registry. You will want to do this is you use 3rd party OCX or DLL files, or if you create your own DLLs using VFP.

By default, a VFP DLL is registered on the computer when it is created on. However, if you need to distribute the application it must be registered on the other computers. One way to handle this is by reading the registry, and register it using REGSRV32 if it does not exist.


Example of registering an OCX or DLL
RUN /n regsvr32 myDLL.DLL /S


Code to read the registry. In this example I am checking for the ôProduct IDö of Visual Foxpro 7.0. You can change the version in the program and try it for other version.

* To test this code: copy from here on down and paste into a program. Run it.


*-- Registry Read by Jim Osieczonek û www.deltabg.com
LOCAL lcVersion
lcVersion = "7.0"

PUBLIC goApp
goApp = CREATEOBJECT('capp')
MESSAGEBOX(goApp.ReadReg('HLM','Software\Microsoft\VisualFoxpro\' + lcVersion +;
'\Registration','ProductID'))

**************************************************
*
DEFINE CLASS capp AS container

*-- Reads the windows Registry.
PROCEDURE readreg
* Start of Code
LPARAMETERS tcKey,tcSubKey,tcValue
IF PCOUNT() = 2
tcValue = ""
ENDIF

* setup environment

LOCAL nKey, cSubKey, cValue, cValueRead

#DEFINE HKEY_USERS -2147483645
#DEFINE HKEY_LOCAL_MACHINE -2147483646
#DEFINE HKEY_CURRENT_USER -2147483647
#DEFINE HKEY_CLASSES_ROOT -2147483648

DO CASE
CASE m.tcKey == "HCR"
nKey = HKEY_CLASSES_ROOT
CASE m.tcKey == "HLM"
nKey = HKEY_LOCAL_MACHINE
CASE m.tcKey = "HCU"
nKey = HKEY_CURRENT_USER
CASE m.tcKey = "HCR"
nKey = HKEY_CLASSES_ROOT
OTHERWISE
nKey = m.tcKey
ENDCASE

cSubKey = m.tcSubKey
cValue = m.tcValue

* example 1
* nKey = HKEY_LOCAL_MACHINE
* cSubKey = "Software\VfpRegTest"
* cValue = "TestREG_SZ"

* example 2
* nKey = HKEY_CLASSES_ROOT
* cSubKey = "Spin.SpinButton\CLSID"
* cValue = ""


* here is where we will actually read the registry
cValueRead = THIS.ReadREG_SZ(nKey, cSubKey, cValue)

IF (EMPTY(cValueRead)) THEN
cValueRead = "REGISTRY KEY NOT FOUND"
* MESSAGEBOX("Function Not Successful.") && testing only
ELSE
* MESSAGEBOX("Function Successful. " + cValueRead) && testing only
ENDIF

RETURN cValueRead
ENDPROC


*-- API's used by the ReadReg method.
PROCEDURE readreg_sz
* This function reads a REG_SZ value from the registry. If successful,
* it will return the value read. If not successful, it will return an empty string.

PARAMETERS nKey, cSubKey, cValue
* nKey The root key to open. It can be any of the constants defined below.
* #DEFINE HKEY_CLASSES_ROOT -2147483648
* #DEFINE HKEY_CURRENT_USER -2147483647
* #DEFINE HKEY_LOCAL_MACHINE -2147483646
* #DEFINE HKEY_USERS -2147483645
* cSubKey The SubKey to open.
* cValue The value that is going to be read.

* Constants that are needed for Registry functions
#DEFINE REG_SZ 1

* WIN 32 API functions that are used
DECLARE Integer RegOpenKey IN Win32API ;
Integer nHKey, String @cSubKey, Integer @nResult
DECLARE Integer RegQueryValueEx IN Win32API ;
Integer nHKey, String lpszValueName, Integer dwReserved,;
Integer @lpdwType, String @lpbData, Integer @lpcbData
DECLARE Integer RegCloseKey IN Win32API Integer nHKey

* Local variables used
LOCAL nErrCode && Error Code returned from Registry functions
LOCAL nKeyHandle && Handle to Key that is opened in the Registry
LOCAL lpdwValueType && Type of Value that we are looking for
LOCAL lpbValue && The data stored in the value
LOCAL lpcbValueSize && Size of the variable
LOCAL lpdwReserved && Reserved Must be 0

* Initialize the variables
nKeyHandle = 0
lpdwReserved = 0
lpdwValueType = REG_SZ
lpbValue = ""

nErrCode = RegOpenKey(nKey, cSubKey, @nKeyHandle)
* If the error code isn't 0, then the key doesn't exist or can't be opened.
IF (nErrCode # 0) THEN
RETURN ""
ENDIF

lpcbValueSize = 1
* Get the size of the data in the value
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)

* Make the buffer big enough
lpbValue = SPACE(lpcbValueSize)
nErrCode=RegQueryValueEx(nKeyHandle, cValue, lpdwReserved, @lpdwValueType, @lpbValue, @lpcbValueSize)

=RegCloseKey(nKeyHandle)
IF (nErrCode # 0) THEN
RETURN ""
ENDIF

lpbValue = LEFT(lpbValue, lpcbValueSize - 1)
RETURN lpbValue
ENDPROC


ENDDEFINE
*
*-- EndDefine: capp
**************************************************


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top